관리-도구
편집 파일: ControllerMetadata.tar
ArgumentMetadata.php 0000604 00000005316 15224656340 0010510 0 ustar 00 <?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\HttpKernel\ControllerMetadata; /** * Responsible for storing metadata of an argument. * * @author Iltar van der Berg <kjarli@gmail.com> */ class ArgumentMetadata { private $name; private $type; private $isVariadic; private $hasDefaultValue; private $defaultValue; private $isNullable; /** * @param string $name * @param string $type * @param bool $isVariadic * @param bool $hasDefaultValue * @param mixed $defaultValue * @param bool $isNullable */ public function __construct($name, $type, $isVariadic, $hasDefaultValue, $defaultValue, $isNullable = false) { $this->name = $name; $this->type = $type; $this->isVariadic = $isVariadic; $this->hasDefaultValue = $hasDefaultValue; $this->defaultValue = $defaultValue; $this->isNullable = $isNullable || null === $type || ($hasDefaultValue && null === $defaultValue); } /** * Returns the name as given in PHP, $foo would yield "foo". * * @return string */ public function getName() { return $this->name; } /** * Returns the type of the argument. * * The type is the PHP class in 5.5+ and additionally the basic type in PHP 7.0+. * * @return string */ public function getType() { return $this->type; } /** * Returns whether the argument is defined as "...$variadic". * * @return bool */ public function isVariadic() { return $this->isVariadic; } /** * Returns whether the argument has a default value. * * Implies whether an argument is optional. * * @return bool */ public function hasDefaultValue() { return $this->hasDefaultValue; } /** * Returns whether the argument accepts null values. * * @return bool */ public function isNullable() { return $this->isNullable; } /** * Returns the default value of the argument. * * @throws \LogicException if no default value is present; {@see self::hasDefaultValue()} * * @return mixed */ public function getDefaultValue() { if (!$this->hasDefaultValue) { throw new \LogicException(sprintf('Argument $%s does not have a default value. Use %s::hasDefaultValue() to avoid this exception.', $this->name, __CLASS__)); } return $this->defaultValue; } } ArgumentMetadataFactoryInterface.php 0000604 00000001163 15224656340 0013655 0 ustar 00 <?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\HttpKernel\ControllerMetadata; /** * Builds method argument data. * * @author Iltar van der Berg <kjarli@gmail.com> */ interface ArgumentMetadataFactoryInterface { /** * @param mixed $controller The controller to resolve the arguments for * * @return ArgumentMetadata[] */ public function createArgumentMetadata($controller); } ArgumentMetadataFactory.php 0000604 00000007106 15224656340 0012037 0 ustar 00 <?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\HttpKernel\ControllerMetadata; /** * Builds {@see ArgumentMetadata} objects based on the given Controller. * * @author Iltar van der Berg <kjarli@gmail.com> */ final class ArgumentMetadataFactory implements ArgumentMetadataFactoryInterface { /** * If the ...$arg functionality is available. * * Requires at least PHP 5.6.0 or HHVM 3.9.1 * * @var bool */ private $supportsVariadic; /** * If the reflection supports the getType() method to resolve types. * * Requires at least PHP 7.0.0 or HHVM 3.11.0 * * @var bool */ private $supportsParameterType; public function __construct() { $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic'); $this->supportsParameterType = method_exists('ReflectionParameter', 'getType'); } /** * {@inheritdoc} */ public function createArgumentMetadata($controller) { $arguments = array(); if (is_array($controller)) { $reflection = new \ReflectionMethod($controller[0], $controller[1]); } elseif (is_object($controller) && !$controller instanceof \Closure) { $reflection = (new \ReflectionObject($controller))->getMethod('__invoke'); } else { $reflection = new \ReflectionFunction($controller); } foreach ($reflection->getParameters() as $param) { $arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param), $this->isVariadic($param), $this->hasDefaultValue($param), $this->getDefaultValue($param), $param->allowsNull()); } return $arguments; } /** * Returns whether an argument is variadic. * * @param \ReflectionParameter $parameter * * @return bool */ private function isVariadic(\ReflectionParameter $parameter) { return $this->supportsVariadic && $parameter->isVariadic(); } /** * Determines whether an argument has a default value. * * @param \ReflectionParameter $parameter * * @return bool */ private function hasDefaultValue(\ReflectionParameter $parameter) { return $parameter->isDefaultValueAvailable(); } /** * Returns a default value if available. * * @param \ReflectionParameter $parameter * * @return mixed|null */ private function getDefaultValue(\ReflectionParameter $parameter) { return $this->hasDefaultValue($parameter) ? $parameter->getDefaultValue() : null; } /** * Returns an associated type to the given parameter if available. * * @param \ReflectionParameter $parameter * * @return null|string */ private function getType(\ReflectionParameter $parameter) { if ($this->supportsParameterType) { if (!$type = $parameter->getType()) { return; } $typeName = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString(); if ('array' === $typeName && !$type->isBuiltin()) { // Special case for HHVM with variadics return; } return $typeName; } if (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $parameter, $info)) { return $info[1]; } } }