123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace Symfony\Component\HttpKernel\ControllerMetadata;
- final class ArgumentMetadataFactory implements ArgumentMetadataFactoryInterface
- {
-
- public function createArgumentMetadata($controller): array
- {
- $arguments = [];
- if (\is_array($controller)) {
- $reflection = new \ReflectionMethod($controller[0], $controller[1]);
- $class = $reflection->class;
- } elseif (\is_object($controller) && !$controller instanceof \Closure) {
- $reflection = new \ReflectionMethod($controller, '__invoke');
- $class = $reflection->class;
- } else {
- $reflection = new \ReflectionFunction($controller);
- if ($class = str_contains($reflection->name, '{closure}') ? null : (\PHP_VERSION_ID >= 80111 ? $reflection->getClosureCalledClass() : $reflection->getClosureScopeClass())) {
- $class = $class->name;
- }
- }
- foreach ($reflection->getParameters() as $param) {
- $attributes = [];
- if (\PHP_VERSION_ID >= 80000) {
- foreach ($param->getAttributes() as $reflectionAttribute) {
- if (class_exists($reflectionAttribute->getName())) {
- $attributes[] = $reflectionAttribute->newInstance();
- }
- }
- }
- $arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $class), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull(), $attributes);
- }
- return $arguments;
- }
-
- private function getType(\ReflectionParameter $parameter, ?string $class): ?string
- {
- if (!$type = $parameter->getType()) {
- return null;
- }
- $name = $type instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
- if (null !== $class) {
- switch (strtolower($name)) {
- case 'self':
- return $class;
- case 'parent':
- return get_parent_class($class) ?: null;
- }
- }
- return $name;
- }
- }
|