ArgumentMetadataFactory.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\ControllerMetadata;
  11. use Symfony\Component\HttpKernel\Attribute\ArgumentInterface;
  12. use Symfony\Component\HttpKernel\Exception\InvalidMetadataException;
  13. /**
  14. * Builds {@see ArgumentMetadata} objects based on the given Controller.
  15. *
  16. * @author Iltar van der Berg <kjarli@gmail.com>
  17. */
  18. final class ArgumentMetadataFactory implements ArgumentMetadataFactoryInterface
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public function createArgumentMetadata($controller): array
  24. {
  25. $arguments = [];
  26. if (\is_array($controller)) {
  27. $reflection = new \ReflectionMethod($controller[0], $controller[1]);
  28. } elseif (\is_object($controller) && !$controller instanceof \Closure) {
  29. $reflection = (new \ReflectionObject($controller))->getMethod('__invoke');
  30. } else {
  31. $reflection = new \ReflectionFunction($controller);
  32. }
  33. foreach ($reflection->getParameters() as $param) {
  34. $attribute = null;
  35. if (\PHP_VERSION_ID >= 80000) {
  36. $reflectionAttributes = $param->getAttributes(ArgumentInterface::class, \ReflectionAttribute::IS_INSTANCEOF);
  37. if (\count($reflectionAttributes) > 1) {
  38. $representative = $controller;
  39. if (\is_array($representative)) {
  40. $representative = sprintf('%s::%s()', \get_class($representative[0]), $representative[1]);
  41. } elseif (\is_object($representative)) {
  42. $representative = \get_class($representative);
  43. }
  44. throw new InvalidMetadataException(sprintf('Controller "%s" has more than one attribute for "$%s" argument.', $representative, $param->getName()));
  45. }
  46. if (isset($reflectionAttributes[0])) {
  47. $attribute = $reflectionAttributes[0]->newInstance();
  48. }
  49. }
  50. $arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $reflection), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull(), $attribute);
  51. }
  52. return $arguments;
  53. }
  54. /**
  55. * Returns an associated type to the given parameter if available.
  56. */
  57. private function getType(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function): ?string
  58. {
  59. if (!$type = $parameter->getType()) {
  60. return null;
  61. }
  62. $name = $type instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
  63. if ($function instanceof \ReflectionMethod) {
  64. $lcName = strtolower($name);
  65. switch ($lcName) {
  66. case 'self':
  67. return $function->getDeclaringClass()->name;
  68. case 'parent':
  69. return ($parent = $function->getDeclaringClass()->getParentClass()) ? $parent->name : null;
  70. }
  71. }
  72. return $name;
  73. }
  74. }