<?php
 namespace League\OAuth2\Client\Grant; use League\OAuth2\Client\Grant\Exception\InvalidGrantException; class GrantFactory { protected $registry = []; public function setGrant($name, AbstractGrant $grant) { $this->registry[$name] = $grant; return $this; } public function getGrant($name) { if (empty($this->registry[$name])) { $this->registerDefaultGrant($name); } return $this->registry[$name]; } protected function registerDefaultGrant($name) { $class = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $name))); $class = 'League\\OAuth2\\Client\\Grant\\' . $class; $this->checkGrant($class); return $this->setGrant($name, new $class); } public function isGrant($class) { return is_subclass_of($class, AbstractGrant::class); } public function checkGrant($class) { if (!$this->isGrant($class)) { throw new InvalidGrantException(sprintf( 'Grant "%s" must extend AbstractGrant', is_object($class) ? get_class($class) : $class )); } } } 