ArgumentMetadataFactory.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * If the ...$arg functionality is available.
  20. *
  21. * Requires at least PHP 5.6.0 or HHVM 3.9.1
  22. *
  23. * @var bool
  24. */
  25. private $supportsVariadic;
  26. /**
  27. * If the reflection supports the getType() method to resolve types.
  28. *
  29. * Requires at least PHP 7.0.0 or HHVM 3.11.0
  30. *
  31. * @var bool
  32. */
  33. private $supportsParameterType;
  34. public function __construct()
  35. {
  36. $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
  37. $this->supportsParameterType = method_exists('ReflectionParameter', 'getType');
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function createArgumentMetadata($controller)
  43. {
  44. $arguments = array();
  45. if (is_array($controller)) {
  46. $reflection = new \ReflectionMethod($controller[0], $controller[1]);
  47. } elseif (is_object($controller) && !$controller instanceof \Closure) {
  48. $reflection = (new \ReflectionObject($controller))->getMethod('__invoke');
  49. } else {
  50. $reflection = new \ReflectionFunction($controller);
  51. }
  52. foreach ($reflection->getParameters() as $param) {
  53. $arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param), $this->isVariadic($param), $this->hasDefaultValue($param), $this->getDefaultValue($param), $param->allowsNull());
  54. }
  55. return $arguments;
  56. }
  57. /**
  58. * Returns whether an argument is variadic.
  59. *
  60. * @param \ReflectionParameter $parameter
  61. *
  62. * @return bool
  63. */
  64. private function isVariadic(\ReflectionParameter $parameter)
  65. {
  66. return $this->supportsVariadic && $parameter->isVariadic();
  67. }
  68. /**
  69. * Determines whether an argument has a default value.
  70. *
  71. * @param \ReflectionParameter $parameter
  72. *
  73. * @return bool
  74. */
  75. private function hasDefaultValue(\ReflectionParameter $parameter)
  76. {
  77. return $parameter->isDefaultValueAvailable();
  78. }
  79. /**
  80. * Returns a default value if available.
  81. *
  82. * @param \ReflectionParameter $parameter
  83. *
  84. * @return mixed|null
  85. */
  86. private function getDefaultValue(\ReflectionParameter $parameter)
  87. {
  88. return $this->hasDefaultValue($parameter) ? $parameter->getDefaultValue() : null;
  89. }
  90. /**
  91. * Returns an associated type to the given parameter if available.
  92. *
  93. * @param \ReflectionParameter $parameter
  94. *
  95. * @return null|string
  96. */
  97. private function getType(\ReflectionParameter $parameter)
  98. {
  99. if ($this->supportsParameterType) {
  100. if (!$type = $parameter->getType()) {
  101. return;
  102. }
  103. $typeName = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString();
  104. if ('array' === $typeName && !$type->isBuiltin()) {
  105. // Special case for HHVM with variadics
  106. return;
  107. }
  108. return $typeName;
  109. }
  110. if (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $parameter, $info)) {
  111. return $info[1];
  112. }
  113. }
  114. }