ORIENT/vendor/symfony/http-kernel/ControllerMetadata/ArgumentMetadataFactory.php

70 lines
2.2 KiB
PHP
Raw Normal View History

2021-04-08 08:08:59 +00: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
{
/**
* {@inheritdoc}
*/
2022-12-14 15:55:13 +00:00
public function createArgumentMetadata($controller): array
2021-04-08 08:08:59 +00:00
{
$arguments = [];
if (\is_array($controller)) {
$reflection = new \ReflectionMethod($controller[0], $controller[1]);
2022-12-14 15:55:13 +00:00
$class = $reflection->class;
2021-04-08 08:08:59 +00:00
} elseif (\is_object($controller) && !$controller instanceof \Closure) {
2022-12-14 15:55:13 +00:00
$reflection = new \ReflectionMethod($controller, '__invoke');
$class = $reflection->class;
2021-04-08 08:08:59 +00:00
} else {
$reflection = new \ReflectionFunction($controller);
2022-12-14 15:55:13 +00:00
if ($class = str_contains($reflection->name, '{closure}') ? null : $reflection->getClosureScopeClass()) {
$class = $class->name;
}
2021-04-08 08:08:59 +00:00
}
foreach ($reflection->getParameters() as $param) {
2022-12-14 15:55:13 +00:00
$arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $class), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull());
2021-04-08 08:08:59 +00:00
}
return $arguments;
}
/**
* Returns an associated type to the given parameter if available.
*/
2022-12-14 15:55:13 +00:00
private function getType(\ReflectionParameter $parameter, ?string $class): ?string
2021-04-08 08:08:59 +00:00
{
2022-12-14 15:55:13 +00:00
if (!$type = $parameter->getType()) {
2021-04-08 08:08:59 +00:00
return null;
}
2022-12-14 15:55:13 +00:00
$name = $type instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
2021-04-08 08:08:59 +00:00
2022-12-14 15:55:13 +00:00
if (null !== $class) {
switch (strtolower($name)) {
case 'self':
return $class;
case 'parent':
return get_parent_class($class) ?: null;
}
2021-04-08 08:08:59 +00:00
}
2022-12-14 15:55:13 +00:00
return $name;
2021-04-08 08:08:59 +00:00
}
}