ArgumentMetadataFactory.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /**
  12. * Builds {@see ArgumentMetadata} objects based on the given Controller.
  13. *
  14. * @author Iltar van der Berg <kjarli@gmail.com>
  15. */
  16. final class ArgumentMetadataFactory implements ArgumentMetadataFactoryInterface
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function createArgumentMetadata($controller): array
  22. {
  23. $arguments = [];
  24. if (\is_array($controller)) {
  25. $reflection = new \ReflectionMethod($controller[0], $controller[1]);
  26. } elseif (\is_object($controller) && !$controller instanceof \Closure) {
  27. $reflection = (new \ReflectionObject($controller))->getMethod('__invoke');
  28. } else {
  29. $reflection = new \ReflectionFunction($controller);
  30. }
  31. foreach ($reflection->getParameters() as $param) {
  32. $arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $reflection), $param->isVariadic(), $param->isDefaultValueAvailable(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null, $param->allowsNull());
  33. }
  34. return $arguments;
  35. }
  36. /**
  37. * Returns an associated type to the given parameter if available.
  38. */
  39. private function getType(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function): ?string
  40. {
  41. if (!$type = $parameter->getType()) {
  42. return null;
  43. }
  44. $name = $type instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
  45. if ($function instanceof \ReflectionMethod) {
  46. $lcName = strtolower($name);
  47. switch ($lcName) {
  48. case 'self':
  49. return $function->getDeclaringClass()->name;
  50. case 'parent':
  51. return ($parent = $function->getDeclaringClass()->getParentClass()) ? $parent->name : null;
  52. }
  53. }
  54. return $name;
  55. }
  56. }