Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace Symfony\Component\DependencyInjection\Compiler; Chris@14: Chris@14: use Symfony\Component\DependencyInjection\Argument\BoundArgument; Chris@14: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@14: use Symfony\Component\DependencyInjection\Definition; Chris@14: use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; Chris@16: use Symfony\Component\DependencyInjection\Exception\RuntimeException; Chris@14: use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper; Chris@17: use Symfony\Component\DependencyInjection\Reference; Chris@14: use Symfony\Component\DependencyInjection\TypedReference; Chris@14: Chris@14: /** Chris@14: * @author Guilhem Niot Chris@14: */ Chris@14: class ResolveBindingsPass extends AbstractRecursivePass Chris@14: { Chris@17: private $usedBindings = []; Chris@17: private $unusedBindings = []; Chris@17: private $errorMessages = []; Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function process(ContainerBuilder $container) Chris@14: { Chris@18: $this->usedBindings = $container->getRemovedBindingIds(); Chris@18: Chris@14: try { Chris@14: parent::process($container); Chris@14: Chris@14: foreach ($this->unusedBindings as list($key, $serviceId)) { Chris@16: $message = sprintf('Unused binding "%s" in service "%s".', $key, $serviceId); Chris@16: if ($this->errorMessages) { Chris@16: $message .= sprintf("\nCould be related to%s:", 1 < \count($this->errorMessages) ? ' one of' : ''); Chris@16: } Chris@16: foreach ($this->errorMessages as $m) { Chris@16: $message .= "\n - ".$m; Chris@16: } Chris@16: throw new InvalidArgumentException($message); Chris@14: } Chris@14: } finally { Chris@17: $this->usedBindings = []; Chris@17: $this->unusedBindings = []; Chris@17: $this->errorMessages = []; Chris@14: } Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: protected function processValue($value, $isRoot = false) Chris@14: { Chris@14: if ($value instanceof TypedReference && $value->getType() === $this->container->normalizeId($value)) { Chris@14: // Already checked Chris@14: $bindings = $this->container->getDefinition($this->currentId)->getBindings(); Chris@14: Chris@14: if (isset($bindings[$value->getType()])) { Chris@14: return $this->getBindingValue($bindings[$value->getType()]); Chris@14: } Chris@14: Chris@14: return parent::processValue($value, $isRoot); Chris@14: } Chris@14: Chris@14: if (!$value instanceof Definition || !$bindings = $value->getBindings()) { Chris@14: return parent::processValue($value, $isRoot); Chris@14: } Chris@14: Chris@14: foreach ($bindings as $key => $binding) { Chris@14: list($bindingValue, $bindingId, $used) = $binding->getValues(); Chris@14: if ($used) { Chris@14: $this->usedBindings[$bindingId] = true; Chris@14: unset($this->unusedBindings[$bindingId]); Chris@14: } elseif (!isset($this->usedBindings[$bindingId])) { Chris@17: $this->unusedBindings[$bindingId] = [$key, $this->currentId]; Chris@14: } Chris@14: Chris@14: if (isset($key[0]) && '$' === $key[0]) { Chris@14: continue; Chris@14: } Chris@14: Chris@14: if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition) { Chris@17: 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))); Chris@14: } Chris@14: } Chris@14: Chris@14: if ($value->isAbstract()) { Chris@14: return parent::processValue($value, $isRoot); Chris@14: } Chris@14: Chris@14: $calls = $value->getMethodCalls(); Chris@14: Chris@16: try { Chris@16: if ($constructor = $this->getConstructor($value, false)) { Chris@17: $calls[] = [$constructor, $value->getArguments()]; Chris@16: } Chris@16: } catch (RuntimeException $e) { Chris@16: $this->errorMessages[] = $e->getMessage(); Chris@16: $this->container->getDefinition($this->currentId)->addError($e->getMessage()); Chris@16: Chris@16: return parent::processValue($value, $isRoot); Chris@14: } Chris@14: Chris@14: foreach ($calls as $i => $call) { Chris@14: list($method, $arguments) = $call; Chris@14: Chris@14: if ($method instanceof \ReflectionFunctionAbstract) { Chris@14: $reflectionMethod = $method; Chris@14: } else { Chris@18: try { Chris@18: $reflectionMethod = $this->getReflectionMethod($value, $method); Chris@18: } catch (RuntimeException $e) { Chris@18: if ($value->getFactory()) { Chris@18: continue; Chris@18: } Chris@18: throw $e; Chris@18: } Chris@14: } Chris@14: Chris@14: foreach ($reflectionMethod->getParameters() as $key => $parameter) { Chris@18: if (\array_key_exists($key, $arguments) && '' !== $arguments[$key]) { Chris@14: continue; Chris@14: } Chris@14: Chris@18: if (\array_key_exists('$'.$parameter->name, $bindings)) { Chris@14: $arguments[$key] = $this->getBindingValue($bindings['$'.$parameter->name]); Chris@14: Chris@14: continue; Chris@14: } Chris@14: Chris@14: $typeHint = ProxyHelper::getTypeHint($reflectionMethod, $parameter, true); Chris@14: Chris@14: if (!isset($bindings[$typeHint])) { Chris@14: continue; Chris@14: } Chris@14: Chris@14: $arguments[$key] = $this->getBindingValue($bindings[$typeHint]); Chris@14: } Chris@14: Chris@14: if ($arguments !== $call[1]) { Chris@14: ksort($arguments); Chris@14: $calls[$i][1] = $arguments; Chris@14: } Chris@14: } Chris@14: Chris@14: if ($constructor) { Chris@14: list(, $arguments) = array_pop($calls); Chris@14: Chris@14: if ($arguments !== $value->getArguments()) { Chris@14: $value->setArguments($arguments); Chris@14: } Chris@14: } Chris@14: Chris@14: if ($calls !== $value->getMethodCalls()) { Chris@14: $value->setMethodCalls($calls); Chris@14: } Chris@14: Chris@14: return parent::processValue($value, $isRoot); Chris@14: } Chris@14: Chris@14: private function getBindingValue(BoundArgument $binding) Chris@14: { Chris@14: list($bindingValue, $bindingId) = $binding->getValues(); Chris@14: Chris@14: $this->usedBindings[$bindingId] = true; Chris@14: unset($this->unusedBindings[$bindingId]); Chris@14: Chris@14: return $bindingValue; Chris@14: } Chris@14: }