4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
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\TypedReference;
21 use Symfony\Component\DependencyInjection\Reference;
24 * @author Guilhem Niot <guilhem.niot@gmail.com>
26 class ResolveBindingsPass extends AbstractRecursivePass
28 private $usedBindings = array();
29 private $unusedBindings = array();
30 private $errorMessages = array();
35 public function process(ContainerBuilder $container)
38 parent::process($container);
40 foreach ($this->unusedBindings as list($key, $serviceId)) {
41 $message = sprintf('Unused binding "%s" in service "%s".', $key, $serviceId);
42 if ($this->errorMessages) {
43 $message .= sprintf("\nCould be related to%s:", 1 < \count($this->errorMessages) ? ' one of' : '');
45 foreach ($this->errorMessages as $m) {
46 $message .= "\n - ".$m;
48 throw new InvalidArgumentException($message);
51 $this->usedBindings = array();
52 $this->unusedBindings = array();
53 $this->errorMessages = array();
60 protected function processValue($value, $isRoot = false)
62 if ($value instanceof TypedReference && $value->getType() === $this->container->normalizeId($value)) {
64 $bindings = $this->container->getDefinition($this->currentId)->getBindings();
66 if (isset($bindings[$value->getType()])) {
67 return $this->getBindingValue($bindings[$value->getType()]);
70 return parent::processValue($value, $isRoot);
73 if (!$value instanceof Definition || !$bindings = $value->getBindings()) {
74 return parent::processValue($value, $isRoot);
77 foreach ($bindings as $key => $binding) {
78 list($bindingValue, $bindingId, $used) = $binding->getValues();
80 $this->usedBindings[$bindingId] = true;
81 unset($this->unusedBindings[$bindingId]);
82 } elseif (!isset($this->usedBindings[$bindingId])) {
83 $this->unusedBindings[$bindingId] = array($key, $this->currentId);
86 if (isset($key[0]) && '$' === $key[0]) {
90 if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition) {
91 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)));
95 if ($value->isAbstract()) {
96 return parent::processValue($value, $isRoot);
99 $calls = $value->getMethodCalls();
102 if ($constructor = $this->getConstructor($value, false)) {
103 $calls[] = array($constructor, $value->getArguments());
105 } catch (RuntimeException $e) {
106 $this->errorMessages[] = $e->getMessage();
107 $this->container->getDefinition($this->currentId)->addError($e->getMessage());
109 return parent::processValue($value, $isRoot);
112 foreach ($calls as $i => $call) {
113 list($method, $arguments) = $call;
115 if ($method instanceof \ReflectionFunctionAbstract) {
116 $reflectionMethod = $method;
118 $reflectionMethod = $this->getReflectionMethod($value, $method);
121 foreach ($reflectionMethod->getParameters() as $key => $parameter) {
122 if (array_key_exists($key, $arguments) && '' !== $arguments[$key]) {
126 if (array_key_exists('$'.$parameter->name, $bindings)) {
127 $arguments[$key] = $this->getBindingValue($bindings['$'.$parameter->name]);
132 $typeHint = ProxyHelper::getTypeHint($reflectionMethod, $parameter, true);
134 if (!isset($bindings[$typeHint])) {
138 $arguments[$key] = $this->getBindingValue($bindings[$typeHint]);
141 if ($arguments !== $call[1]) {
143 $calls[$i][1] = $arguments;
148 list(, $arguments) = array_pop($calls);
150 if ($arguments !== $value->getArguments()) {
151 $value->setArguments($arguments);
155 if ($calls !== $value->getMethodCalls()) {
156 $value->setMethodCalls($calls);
159 return parent::processValue($value, $isRoot);
162 private function getBindingValue(BoundArgument $binding)
164 list($bindingValue, $bindingId) = $binding->getValues();
166 $this->usedBindings[$bindingId] = true;
167 unset($this->unusedBindings[$bindingId]);
169 return $bindingValue;