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();
34 public function process(ContainerBuilder $container)
37 parent::process($container);
39 foreach ($this->unusedBindings as list($key, $serviceId)) {
40 throw new InvalidArgumentException(sprintf('Unused binding "%s" in service "%s".', $key, $serviceId));
43 $this->usedBindings = array();
44 $this->unusedBindings = array();
51 protected function processValue($value, $isRoot = false)
53 if ($value instanceof TypedReference && $value->getType() === $this->container->normalizeId($value)) {
55 $bindings = $this->container->getDefinition($this->currentId)->getBindings();
57 if (isset($bindings[$value->getType()])) {
58 return $this->getBindingValue($bindings[$value->getType()]);
61 return parent::processValue($value, $isRoot);
64 if (!$value instanceof Definition || !$bindings = $value->getBindings()) {
65 return parent::processValue($value, $isRoot);
68 foreach ($bindings as $key => $binding) {
69 list($bindingValue, $bindingId, $used) = $binding->getValues();
71 $this->usedBindings[$bindingId] = true;
72 unset($this->unusedBindings[$bindingId]);
73 } elseif (!isset($this->usedBindings[$bindingId])) {
74 $this->unusedBindings[$bindingId] = array($key, $this->currentId);
77 if (isset($key[0]) && '$' === $key[0]) {
81 if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition) {
82 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)));
86 if ($value->isAbstract()) {
87 return parent::processValue($value, $isRoot);
90 $calls = $value->getMethodCalls();
93 if ($constructor = $this->getConstructor($value, false)) {
94 $calls[] = array($constructor, $value->getArguments());
96 } catch (RuntimeException $e) {
97 $this->container->getDefinition($this->currentId)->addError($e->getMessage());
99 return parent::processValue($value, $isRoot);
102 foreach ($calls as $i => $call) {
103 list($method, $arguments) = $call;
105 if ($method instanceof \ReflectionFunctionAbstract) {
106 $reflectionMethod = $method;
108 $reflectionMethod = $this->getReflectionMethod($value, $method);
111 foreach ($reflectionMethod->getParameters() as $key => $parameter) {
112 if (array_key_exists($key, $arguments) && '' !== $arguments[$key]) {
116 if (array_key_exists('$'.$parameter->name, $bindings)) {
117 $arguments[$key] = $this->getBindingValue($bindings['$'.$parameter->name]);
122 $typeHint = ProxyHelper::getTypeHint($reflectionMethod, $parameter, true);
124 if (!isset($bindings[$typeHint])) {
128 $arguments[$key] = $this->getBindingValue($bindings[$typeHint]);
131 if ($arguments !== $call[1]) {
133 $calls[$i][1] = $arguments;
138 list(, $arguments) = array_pop($calls);
140 if ($arguments !== $value->getArguments()) {
141 $value->setArguments($arguments);
145 if ($calls !== $value->getMethodCalls()) {
146 $value->setMethodCalls($calls);
149 return parent::processValue($value, $isRoot);
152 private function getBindingValue(BoundArgument $binding)
154 list($bindingValue, $bindingId) = $binding->getValues();
156 $this->usedBindings[$bindingId] = true;
157 unset($this->unusedBindings[$bindingId]);
159 return $bindingValue;