[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Compiler/ -> AbstractRecursivePass.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\DependencyInjection\Compiler;
  13  
  14  use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
  15  use Symfony\Component\DependencyInjection\ContainerBuilder;
  16  use Symfony\Component\DependencyInjection\Definition;
  17  use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  18  use Symfony\Component\DependencyInjection\Reference;
  19  
  20  /**
  21   * @author Nicolas Grekas <p@tchwork.com>
  22   */
  23  abstract class AbstractRecursivePass implements CompilerPassInterface
  24  {
  25      /**
  26       * @var ContainerBuilder
  27       */
  28      protected $container;
  29      protected $currentId;
  30  
  31      /**
  32       * {@inheritdoc}
  33       */
  34      public function process(ContainerBuilder $container)
  35      {
  36          $this->container = $container;
  37  
  38          try {
  39              $this->processValue($container->getDefinitions(), true);
  40          } finally {
  41              $this->container = null;
  42          }
  43      }
  44  
  45      /**
  46       * Processes a value found in a definition tree.
  47       *
  48       * @param mixed $value
  49       * @param bool  $isRoot
  50       *
  51       * @return mixed The processed value
  52       */
  53      protected function processValue($value, $isRoot = false)
  54      {
  55          if (\is_array($value)) {
  56              foreach ($value as $k => $v) {
  57                  if ($isRoot) {
  58                      $this->currentId = $k;
  59                  }
  60                  if ($v !== $processedValue = $this->processValue($v, $isRoot)) {
  61                      $value[$k] = $processedValue;
  62                  }
  63              }
  64          } elseif ($value instanceof ArgumentInterface) {
  65              $value->setValues($this->processValue($value->getValues()));
  66          } elseif ($value instanceof Definition) {
  67              $value->setArguments($this->processValue($value->getArguments()));
  68              $value->setProperties($this->processValue($value->getProperties()));
  69              $value->setMethodCalls($this->processValue($value->getMethodCalls()));
  70  
  71              $changes = $value->getChanges();
  72              if (isset($changes['factory'])) {
  73                  $value->setFactory($this->processValue($value->getFactory()));
  74              }
  75              if (isset($changes['configurator'])) {
  76                  $value->setConfigurator($this->processValue($value->getConfigurator()));
  77              }
  78          }
  79  
  80          return $value;
  81      }
  82  
  83      /**
  84       * @param bool $required
  85       *
  86       * @return \ReflectionFunctionAbstract|null
  87       *
  88       * @throws RuntimeException
  89       */
  90      protected function getConstructor(Definition $definition, $required)
  91      {
  92          if ($definition->isSynthetic()) {
  93              return null;
  94          }
  95  
  96          if (\is_string($factory = $definition->getFactory())) {
  97              if (!\function_exists($factory)) {
  98                  throw new RuntimeException(sprintf('Invalid service "%s": function "%s" does not exist.', $this->currentId, $factory));
  99              }
 100              $r = new \ReflectionFunction($factory);
 101              if (false !== $r->getFileName() && file_exists($r->getFileName())) {
 102                  $this->container->fileExists($r->getFileName());
 103              }
 104  
 105              return $r;
 106          }
 107  
 108          if ($factory) {
 109              list($class, $method) = $factory;
 110              if ($class instanceof Reference) {
 111                  $class = $this->container->findDefinition((string) $class)->getClass();
 112              } elseif (null === $class) {
 113                  $class = $definition->getClass();
 114              }
 115              if ('__construct' === $method) {
 116                  throw new RuntimeException(sprintf('Invalid service "%s": "__construct()" cannot be used as a factory method.', $this->currentId));
 117              }
 118  
 119              return $this->getReflectionMethod(new Definition($class), $method);
 120          }
 121  
 122          $class = $definition->getClass();
 123  
 124          try {
 125              if (!$r = $this->container->getReflectionClass($class)) {
 126                  throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
 127              }
 128          } catch (\ReflectionException $e) {
 129              throw new RuntimeException(sprintf('Invalid service "%s": ', $this->currentId).lcfirst($e->getMessage()));
 130          }
 131          if (!$r = $r->getConstructor()) {
 132              if ($required) {
 133                  throw new RuntimeException(sprintf('Invalid service "%s": class%s has no constructor.', $this->currentId, sprintf($class !== $this->currentId ? ' "%s"' : '', $class)));
 134              }
 135          } elseif (!$r->isPublic()) {
 136              throw new RuntimeException(sprintf('Invalid service "%s": ', $this->currentId).sprintf($class !== $this->currentId ? 'constructor of class "%s"' : 'its constructor', $class).' must be public.');
 137          }
 138  
 139          return $r;
 140      }
 141  
 142      /**
 143       * @param string $method
 144       *
 145       * @throws RuntimeException
 146       *
 147       * @return \ReflectionFunctionAbstract
 148       */
 149      protected function getReflectionMethod(Definition $definition, $method)
 150      {
 151          if ('__construct' === $method) {
 152              return $this->getConstructor($definition, true);
 153          }
 154  
 155          if (!$class = $definition->getClass()) {
 156              throw new RuntimeException(sprintf('Invalid service "%s": the class is not set.', $this->currentId));
 157          }
 158  
 159          if (!$r = $this->container->getReflectionClass($class)) {
 160              throw new RuntimeException(sprintf('Invalid service "%s": class "%s" does not exist.', $this->currentId, $class));
 161          }
 162  
 163          if (!$r->hasMethod($method)) {
 164              throw new RuntimeException(sprintf('Invalid service "%s": method "%s()" does not exist.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
 165          }
 166  
 167          $r = $r->getMethod($method);
 168          if (!$r->isPublic()) {
 169              throw new RuntimeException(sprintf('Invalid service "%s": method "%s()" must be public.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
 170          }
 171  
 172          return $r;
 173      }
 174  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1