[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/dependency-injection/Compiler/ -> ResolveBindingsPass.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\BoundArgument;
  15  use Symfony\Component\DependencyInjection\ContainerBuilder;
  16  use Symfony\Component\DependencyInjection\Definition;
  17  use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  18  use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  19  use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
  20  use Symfony\Component\DependencyInjection\Reference;
  21  use Symfony\Component\DependencyInjection\TypedReference;
  22  
  23  /**
  24   * @author Guilhem Niot <guilhem.niot@gmail.com>
  25   */
  26  class ResolveBindingsPass extends AbstractRecursivePass
  27  {
  28      private $usedBindings = [];
  29      private $unusedBindings = [];
  30      private $errorMessages = [];
  31  
  32      /**
  33       * {@inheritdoc}
  34       */
  35      public function process(ContainerBuilder $container)
  36      {
  37          $this->usedBindings = $container->getRemovedBindingIds();
  38  
  39          try {
  40              parent::process($container);
  41  
  42              foreach ($this->unusedBindings as list($key, $serviceId)) {
  43                  $message = sprintf('Unused binding "%s" in service "%s".', $key, $serviceId);
  44                  if ($this->errorMessages) {
  45                      $message .= sprintf("\nCould be related to%s:", 1 < \count($this->errorMessages) ? ' one of' : '');
  46                  }
  47                  foreach ($this->errorMessages as $m) {
  48                      $message .= "\n - ".$m;
  49                  }
  50                  throw new InvalidArgumentException($message);
  51              }
  52          } finally {
  53              $this->usedBindings = [];
  54              $this->unusedBindings = [];
  55              $this->errorMessages = [];
  56          }
  57      }
  58  
  59      /**
  60       * {@inheritdoc}
  61       */
  62      protected function processValue($value, $isRoot = false)
  63      {
  64          if ($value instanceof TypedReference && $value->getType() === $this->container->normalizeId($value)) {
  65              // Already checked
  66              $bindings = $this->container->getDefinition($this->currentId)->getBindings();
  67  
  68              if (isset($bindings[$value->getType()])) {
  69                  return $this->getBindingValue($bindings[$value->getType()]);
  70              }
  71  
  72              return parent::processValue($value, $isRoot);
  73          }
  74  
  75          if (!$value instanceof Definition || !$bindings = $value->getBindings()) {
  76              return parent::processValue($value, $isRoot);
  77          }
  78  
  79          foreach ($bindings as $key => $binding) {
  80              list($bindingValue, $bindingId, $used) = $binding->getValues();
  81              if ($used) {
  82                  $this->usedBindings[$bindingId] = true;
  83                  unset($this->unusedBindings[$bindingId]);
  84              } elseif (!isset($this->usedBindings[$bindingId])) {
  85                  $this->unusedBindings[$bindingId] = [$key, $this->currentId];
  86              }
  87  
  88              if (isset($key[0]) && '$' === $key[0]) {
  89                  continue;
  90              }
  91  
  92              if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition) {
  93                  throw new InvalidArgumentException(sprintf('Invalid value for binding key "%s" for service "%s": expected null, an instance of "%s" or an instance of "%s", "%s" given.', $key, $this->currentId, Reference::class, Definition::class, \gettype($bindingValue)));
  94              }
  95          }
  96  
  97          if ($value->isAbstract()) {
  98              return parent::processValue($value, $isRoot);
  99          }
 100  
 101          $calls = $value->getMethodCalls();
 102  
 103          try {
 104              if ($constructor = $this->getConstructor($value, false)) {
 105                  $calls[] = [$constructor, $value->getArguments()];
 106              }
 107          } catch (RuntimeException $e) {
 108              $this->errorMessages[] = $e->getMessage();
 109              $this->container->getDefinition($this->currentId)->addError($e->getMessage());
 110  
 111              return parent::processValue($value, $isRoot);
 112          }
 113  
 114          foreach ($calls as $i => $call) {
 115              list($method, $arguments) = $call;
 116  
 117              if ($method instanceof \ReflectionFunctionAbstract) {
 118                  $reflectionMethod = $method;
 119              } else {
 120                  try {
 121                      $reflectionMethod = $this->getReflectionMethod($value, $method);
 122                  } catch (RuntimeException $e) {
 123                      if ($value->getFactory()) {
 124                          continue;
 125                      }
 126                      throw $e;
 127                  }
 128              }
 129  
 130              foreach ($reflectionMethod->getParameters() as $key => $parameter) {
 131                  if (\array_key_exists($key, $arguments) && '' !== $arguments[$key]) {
 132                      continue;
 133                  }
 134  
 135                  if (\array_key_exists('$'.$parameter->name, $bindings)) {
 136                      $arguments[$key] = $this->getBindingValue($bindings['$'.$parameter->name]);
 137  
 138                      continue;
 139                  }
 140  
 141                  $typeHint = ProxyHelper::getTypeHint($reflectionMethod, $parameter, true);
 142  
 143                  if (!isset($bindings[$typeHint])) {
 144                      continue;
 145                  }
 146  
 147                  $arguments[$key] = $this->getBindingValue($bindings[$typeHint]);
 148              }
 149  
 150              if ($arguments !== $call[1]) {
 151                  ksort($arguments);
 152                  $calls[$i][1] = $arguments;
 153              }
 154          }
 155  
 156          if ($constructor) {
 157              list(, $arguments) = array_pop($calls);
 158  
 159              if ($arguments !== $value->getArguments()) {
 160                  $value->setArguments($arguments);
 161              }
 162          }
 163  
 164          if ($calls !== $value->getMethodCalls()) {
 165              $value->setMethodCalls($calls);
 166          }
 167  
 168          return parent::processValue($value, $isRoot);
 169      }
 170  
 171      private function getBindingValue(BoundArgument $binding)
 172      {
 173          list($bindingValue, $bindingId) = $binding->getValues();
 174  
 175          $this->usedBindings[$bindingId] = true;
 176          unset($this->unusedBindings[$bindingId]);
 177  
 178          return $bindingValue;
 179      }
 180  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1