[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/ControllerMetadata/ -> ArgumentMetadataFactory.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpKernel\ControllerMetadata;
  13  
  14  /**
  15   * Builds {@see ArgumentMetadata} objects based on the given Controller.
  16   *
  17   * @author Iltar van der Berg <kjarli@gmail.com>
  18   */
  19  final class ArgumentMetadataFactory implements ArgumentMetadataFactoryInterface
  20  {
  21      /**
  22       * If the ...$arg functionality is available.
  23       *
  24       * Requires at least PHP 5.6.0 or HHVM 3.9.1
  25       *
  26       * @var bool
  27       */
  28      private $supportsVariadic;
  29  
  30      /**
  31       * If the reflection supports the getType() method to resolve types.
  32       *
  33       * Requires at least PHP 7.0.0 or HHVM 3.11.0
  34       *
  35       * @var bool
  36       */
  37      private $supportsParameterType;
  38  
  39      public function __construct()
  40      {
  41          $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
  42          $this->supportsParameterType = method_exists('ReflectionParameter', 'getType');
  43      }
  44  
  45      /**
  46       * {@inheritdoc}
  47       */
  48      public function createArgumentMetadata($controller)
  49      {
  50          $arguments = [];
  51  
  52          if (\is_array($controller)) {
  53              $reflection = new \ReflectionMethod($controller[0], $controller[1]);
  54          } elseif (\is_object($controller) && !$controller instanceof \Closure) {
  55              $reflection = (new \ReflectionObject($controller))->getMethod('__invoke');
  56          } else {
  57              $reflection = new \ReflectionFunction($controller);
  58          }
  59  
  60          foreach ($reflection->getParameters() as $param) {
  61              $arguments[] = new ArgumentMetadata($param->getName(), $this->getType($param, $reflection), $this->isVariadic($param), $this->hasDefaultValue($param), $this->getDefaultValue($param), $param->allowsNull());
  62          }
  63  
  64          return $arguments;
  65      }
  66  
  67      /**
  68       * Returns whether an argument is variadic.
  69       *
  70       * @return bool
  71       */
  72      private function isVariadic(\ReflectionParameter $parameter)
  73      {
  74          return $this->supportsVariadic && $parameter->isVariadic();
  75      }
  76  
  77      /**
  78       * Determines whether an argument has a default value.
  79       *
  80       * @return bool
  81       */
  82      private function hasDefaultValue(\ReflectionParameter $parameter)
  83      {
  84          return $parameter->isDefaultValueAvailable();
  85      }
  86  
  87      /**
  88       * Returns a default value if available.
  89       *
  90       * @return mixed|null
  91       */
  92      private function getDefaultValue(\ReflectionParameter $parameter)
  93      {
  94          return $this->hasDefaultValue($parameter) ? $parameter->getDefaultValue() : null;
  95      }
  96  
  97      /**
  98       * Returns an associated type to the given parameter if available.
  99       *
 100       * @return string|null
 101       */
 102      private function getType(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $function)
 103      {
 104          if ($this->supportsParameterType) {
 105              if (!$type = $parameter->getType()) {
 106                  return null;
 107              }
 108              $name = $type instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
 109              if ('array' === $name && !$type->isBuiltin()) {
 110                  // Special case for HHVM with variadics
 111                  return null;
 112              }
 113          } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $parameter, $name)) {
 114              $name = $name[1];
 115          } else {
 116              return null;
 117          }
 118          $lcName = strtolower($name);
 119  
 120          if ('self' !== $lcName && 'parent' !== $lcName) {
 121              return $name;
 122          }
 123          if (!$function instanceof \ReflectionMethod) {
 124              return null;
 125          }
 126          if ('self' === $lcName) {
 127              return $function->getDeclaringClass()->name;
 128          }
 129          if ($parent = $function->getDeclaringClass()->getParentClass()) {
 130              return $parent->name;
 131          }
 132  
 133          return null;
 134      }
 135  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1