4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\ControllerMetadata;
15 * Builds {@see ArgumentMetadata} objects based on the given Controller.
17 * @author Iltar van der Berg <kjarli@gmail.com>
19 final class ArgumentMetadataFactory implements ArgumentMetadataFactoryInterface
22 * If the ...$arg functionality is available.
24 * Requires at least PHP 5.6.0 or HHVM 3.9.1
28 private $supportsVariadic;
31 * If the reflection supports the getType() method to resolve types.
33 * Requires at least PHP 7.0.0 or HHVM 3.11.0
37 private $supportsParameterType;
39 public function __construct()
41 $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
42 $this->supportsParameterType = method_exists('ReflectionParameter', 'getType');
48 public function createArgumentMetadata($controller)
52 if (\is_array($controller)) {
53 $reflection = new \ReflectionMethod($controller[0], $controller[1]);
54 } elseif (\is_object($controller) && !$controller instanceof \Closure) {
55 $reflection = (new \ReflectionObject($controller))->getMethod('__invoke');
57 $reflection = new \ReflectionFunction($controller);
60 foreach ($reflection->getParameters() as $param) {
61 $arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $reflection), $this->isVariadic($param), $this->hasDefaultValue($param), $this->getDefaultValue($param), $param->allowsNull());
68 * Returns whether an argument is variadic.
70 * @param \ReflectionParameter $parameter
74 private function isVariadic(\ReflectionParameter $parameter)
76 return $this->supportsVariadic && $parameter->isVariadic();
80 * Determines whether an argument has a default value.
82 * @param \ReflectionParameter $parameter
86 private function hasDefaultValue(\ReflectionParameter $parameter)
88 return $parameter->isDefaultValueAvailable();
92 * Returns a default value if available.
94 * @param \ReflectionParameter $parameter
98 private function getDefaultValue(\ReflectionParameter $parameter)
100 return $this->hasDefaultValue($parameter) ? $parameter->getDefaultValue() : null;
104 * Returns an associated type to the given parameter if available.
106 * @param \ReflectionParameter $parameter
108 * @return string|null
110 private function getType(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function)
112 if ($this->supportsParameterType) {
113 if (!$type = $parameter->getType()) {
116 $name = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString();
117 if ('array' === $name && !$type->isBuiltin()) {
118 // Special case for HHVM with variadics
121 } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $parameter, $name)) {
126 $lcName = strtolower($name);
128 if ('self' !== $lcName && 'parent' !== $lcName) {
131 if (!$function instanceof \ReflectionMethod) {
134 if ('self' === $lcName) {
135 return $function->getDeclaringClass()->name;
137 if ($parent = $function->getDeclaringClass()->getParentClass()) {
138 return $parent->name;