Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpKernel\ControllerMetadata;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Builds {@see ArgumentMetadata} objects based on the given Controller.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @author Iltar van der Berg <kjarli@gmail.com>
|
Chris@0
|
18 */
|
Chris@0
|
19 final class ArgumentMetadataFactory implements ArgumentMetadataFactoryInterface
|
Chris@0
|
20 {
|
Chris@0
|
21 /**
|
Chris@0
|
22 * If the ...$arg functionality is available.
|
Chris@0
|
23 *
|
Chris@0
|
24 * Requires at least PHP 5.6.0 or HHVM 3.9.1
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var bool
|
Chris@0
|
27 */
|
Chris@0
|
28 private $supportsVariadic;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * If the reflection supports the getType() method to resolve types.
|
Chris@0
|
32 *
|
Chris@0
|
33 * Requires at least PHP 7.0.0 or HHVM 3.11.0
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var bool
|
Chris@0
|
36 */
|
Chris@0
|
37 private $supportsParameterType;
|
Chris@0
|
38
|
Chris@0
|
39 public function __construct()
|
Chris@0
|
40 {
|
Chris@0
|
41 $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
|
Chris@0
|
42 $this->supportsParameterType = method_exists('ReflectionParameter', 'getType');
|
Chris@0
|
43 }
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * {@inheritdoc}
|
Chris@0
|
47 */
|
Chris@0
|
48 public function createArgumentMetadata($controller)
|
Chris@0
|
49 {
|
Chris@17
|
50 $arguments = [];
|
Chris@0
|
51
|
Chris@17
|
52 if (\is_array($controller)) {
|
Chris@0
|
53 $reflection = new \ReflectionMethod($controller[0], $controller[1]);
|
Chris@17
|
54 } elseif (\is_object($controller) && !$controller instanceof \Closure) {
|
Chris@0
|
55 $reflection = (new \ReflectionObject($controller))->getMethod('__invoke');
|
Chris@0
|
56 } else {
|
Chris@0
|
57 $reflection = new \ReflectionFunction($controller);
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 foreach ($reflection->getParameters() as $param) {
|
Chris@16
|
61 $arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $reflection), $this->isVariadic($param), $this->hasDefaultValue($param), $this->getDefaultValue($param), $param->allowsNull());
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 return $arguments;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Returns whether an argument is variadic.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @param \ReflectionParameter $parameter
|
Chris@0
|
71 *
|
Chris@0
|
72 * @return bool
|
Chris@0
|
73 */
|
Chris@0
|
74 private function isVariadic(\ReflectionParameter $parameter)
|
Chris@0
|
75 {
|
Chris@0
|
76 return $this->supportsVariadic && $parameter->isVariadic();
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Determines whether an argument has a default value.
|
Chris@0
|
81 *
|
Chris@0
|
82 * @param \ReflectionParameter $parameter
|
Chris@0
|
83 *
|
Chris@0
|
84 * @return bool
|
Chris@0
|
85 */
|
Chris@0
|
86 private function hasDefaultValue(\ReflectionParameter $parameter)
|
Chris@0
|
87 {
|
Chris@0
|
88 return $parameter->isDefaultValueAvailable();
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Returns a default value if available.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @param \ReflectionParameter $parameter
|
Chris@0
|
95 *
|
Chris@0
|
96 * @return mixed|null
|
Chris@0
|
97 */
|
Chris@0
|
98 private function getDefaultValue(\ReflectionParameter $parameter)
|
Chris@0
|
99 {
|
Chris@0
|
100 return $this->hasDefaultValue($parameter) ? $parameter->getDefaultValue() : null;
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Returns an associated type to the given parameter if available.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @param \ReflectionParameter $parameter
|
Chris@0
|
107 *
|
Chris@17
|
108 * @return string|null
|
Chris@0
|
109 */
|
Chris@16
|
110 private function getType(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function)
|
Chris@0
|
111 {
|
Chris@0
|
112 if ($this->supportsParameterType) {
|
Chris@0
|
113 if (!$type = $parameter->getType()) {
|
Chris@0
|
114 return;
|
Chris@0
|
115 }
|
Chris@16
|
116 $name = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString();
|
Chris@16
|
117 if ('array' === $name && !$type->isBuiltin()) {
|
Chris@0
|
118 // Special case for HHVM with variadics
|
Chris@0
|
119 return;
|
Chris@0
|
120 }
|
Chris@16
|
121 } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $parameter, $name)) {
|
Chris@16
|
122 $name = $name[1];
|
Chris@16
|
123 } else {
|
Chris@16
|
124 return;
|
Chris@16
|
125 }
|
Chris@16
|
126 $lcName = strtolower($name);
|
Chris@0
|
127
|
Chris@16
|
128 if ('self' !== $lcName && 'parent' !== $lcName) {
|
Chris@16
|
129 return $name;
|
Chris@0
|
130 }
|
Chris@16
|
131 if (!$function instanceof \ReflectionMethod) {
|
Chris@16
|
132 return;
|
Chris@16
|
133 }
|
Chris@16
|
134 if ('self' === $lcName) {
|
Chris@16
|
135 return $function->getDeclaringClass()->name;
|
Chris@16
|
136 }
|
Chris@16
|
137 if ($parent = $function->getDeclaringClass()->getParentClass()) {
|
Chris@16
|
138 return $parent->name;
|
Chris@0
|
139 }
|
Chris@0
|
140 }
|
Chris@0
|
141 }
|