Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0c7717a

Browse filesBrowse files
committed
[DependencyInjection] Support local binding
1 parent 47740ce commit 0c7717a
Copy full SHA for 0c7717a

23 files changed

+673
-70
lines changed
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\Argument;
13+
14+
/**
15+
* @author Guilhem Niot <guilhem.niot@gmail.com>
16+
*/
17+
final class BindingArgument implements ArgumentInterface
18+
{
19+
private static $count = 0;
20+
21+
private $value;
22+
private $identifier;
23+
24+
public function __construct($value)
25+
{
26+
$this->value = $value;
27+
$this->identifier = ++self::$count;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function getValues()
34+
{
35+
return array($this->value, $this->identifier);
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
public function setValues(array $values)
42+
{
43+
list($this->value, $this->identifier) = $values;
44+
}
45+
}

‎src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AbstractRecursivePass.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ protected function processValue($value, $isRoot = false)
6262
$value->setArguments($this->processValue($value->getArguments()));
6363
$value->setProperties($this->processValue($value->getProperties()));
6464
$value->setMethodCalls($this->processValue($value->getMethodCalls()));
65+
$value->setBindings($this->processValue($value->getBindings()));
6566

6667
if ($v = $value->getFactory()) {
6768
$value->setFactory($this->processValue($v));

‎src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function __construct()
5757
new CheckDefinitionValidityPass(),
5858
new RegisterServiceSubscribersPass(),
5959
new ResolveNamedArgumentsPass(),
60+
new ResolveBindingsPass(),
6061
new AutowirePass(),
6162
new ResolveReferencesToAliasesPass(),
6263
new ResolveInvalidReferencesPass(),
+188Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Definition;
16+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17+
use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
20+
/**
21+
* @author Guilhem Niot <guilhem.niot@gmail.com>
22+
*/
23+
class ResolveBindingsPass extends AbstractRecursivePass
24+
{
25+
private $usedBindings = array();
26+
private $unusedBindings = array();
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function process(ContainerBuilder $container)
32+
{
33+
try {
34+
parent::process($container);
35+
36+
foreach ($this->unusedBindings as list($key, $serviceId)) {
37+
throw new InvalidArgumentException(sprintf('Unused binding "%s" in service "%s".', $key, $serviceId));
38+
}
39+
} finally {
40+
$this->usedBindings = array();
41+
$this->unusedBindings = array();
42+
}
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
protected function processValue($value, $isRoot = false)
49+
{
50+
if (!$value instanceof Definition || $value->isAbstract() || !$bindings = $value->getBindings()) {
51+
return parent::processValue($value, $isRoot);
52+
}
53+
54+
foreach ($bindings as $key => $binding) {
55+
list($bindingValue, $bindingId) = $binding->getValues();
56+
if (!isset($this->usedBindings[$bindingId])) {
57+
$this->unusedBindings[$bindingId] = array($key, $this->currentId);
58+
}
59+
60+
if (isset($key[0]) && '$' === $key[0]) {
61+
continue;
62+
}
63+
64+
if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition) {
65+
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)));
66+
}
67+
}
68+
69+
$class = $value->getClass();
70+
if (!$reflectionClass = $this->container->getReflectionClass($class)) {
71+
throw new InvalidArgumentException(sprintf('Unable to resolve service "%s": class "%s" does not exist.', $this->currentId, $class));
72+
}
73+
74+
$calls = $value->getMethodCalls();
75+
76+
if ($value->getFactory()) {
77+
$calls[] = array(null, $value->getArguments());
78+
} elseif (null !== $constructor = $reflectionClass->getConstructor()) {
79+
$calls[] = array($constructor->name, $value->getArguments());
80+
}
81+
82+
foreach ($calls as $i => $call) {
83+
list($method, $arguments) = $call;
84+
85+
if (null === $method) {
86+
$reflectionMethod = $this->getFactory($value);
87+
} else {
88+
$reflectionMethod = $this->getReflectionMethod($reflectionClass, $method);
89+
}
90+
91+
foreach ($reflectionMethod->getParameters() as $key => $parameter) {
92+
if (array_key_exists($key, $arguments) && '' !== $arguments[$key]) {
93+
continue;
94+
}
95+
96+
if (array_key_exists('$'.$parameter->name, $bindings)) {
97+
list($bindingValue, $bindingId) = $bindings['$'.$parameter->name]->getValues();
98+
$arguments[$key] = $bindingValue;
99+
100+
$this->usedBindings[$bindingId] = true;
101+
unset($this->unusedBindings[$bindingId]);
102+
103+
continue;
104+
}
105+
106+
$typeHint = ProxyHelper::getTypeHint($reflectionMethod, $parameter, true);
107+
108+
if (!isset($bindings[$typeHint])) {
109+
continue;
110+
}
111+
112+
list($bindingValue, $bindingId) = $bindings[$typeHint]->getValues();
113+
$arguments[$key] = $bindingValue;
114+
115+
$this->usedBindings[$bindingId] = true;
116+
unset($this->unusedBindings[$bindingId]);
117+
}
118+
119+
if ($arguments !== $call[1]) {
120+
ksort($arguments);
121+
$calls[$i][1] = $arguments;
122+
}
123+
}
124+
125+
if ($value->getFactory() || null !== $constructor) {
126+
list(, $arguments) = array_pop($calls);
127+
128+
if ($arguments !== $value->getArguments()) {
129+
$value->setArguments($arguments);
130+
}
131+
}
132+
133+
if ($calls !== $value->getMethodCalls()) {
134+
$value->setMethodCalls($calls);
135+
}
136+
137+
return parent::processValue($value, $isRoot);
138+
}
139+
140+
/**
141+
* @param \ReflectionClass $class
142+
* @param string $method
143+
*
144+
* @throws InvalidArgumentException
145+
*
146+
* @return \ReflectionMethod
147+
*/
148+
private function getReflectionMethod(\ReflectionClass $reflectionClass, $method)
149+
{
150+
if (!$reflectionClass->hasMethod($method)) {
151+
throw new InvalidArgumentException(sprintf('Unable to resolve service "%s": method "%s::%s" does not exist.', $this->currentId, $reflectionClass->name, $method));
152+
}
153+
154+
$method = $reflectionClass->getMethod($method);
155+
156+
if (!$method->isPublic()) {
157+
throw new InvalidArgumentException(sprintf('Unable to resolve service "%s": method "%s::%s" must be public.', $this->currentId, $class, $method->name));
158+
}
159+
160+
return $method;
161+
}
162+
163+
private function getFactory(Definition $definition)
164+
{
165+
$factory = $definition->getFactory();
166+
if (is_string($factory)) {
167+
$m = new \ReflectionFunction($factory);
168+
if (false !== $m->getFileName() && file_exists($m->getFileName())) {
169+
$this->container->fileExists($m->getFileName());
170+
}
171+
172+
return $m;
173+
}
174+
175+
list($class, $method) = $factory;
176+
if ($class instanceof Reference) {
177+
$class = $this->container->findDefinition((string) $class)->getClass();
178+
} elseif (null === $class) {
179+
$class = $definition->getClass();
180+
}
181+
182+
if (!$reflectionClass = $this->container->getReflectionClass($class)) {
183+
throw new InvalidArgumentException(sprintf('Unable to resolve service "%s": class "%s" does not exist.', $this->currentId, $class));
184+
}
185+
186+
return $this->getReflectionMethod($reflectionClass, $method);
187+
}
188+
}

‎src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/ResolveDefinitionTemplatesPass.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ private function doResolveDefinition(ChildDefinition $definition)
101101
$def->setPublic($parentDef->isPublic());
102102
$def->setLazy($parentDef->isLazy());
103103
$def->setAutowired($parentDef->getAutowired());
104+
$def->setBindings($parentDef->getBindings());
104105

105106
self::mergeDefinition($def, $definition);
106107

@@ -156,6 +157,7 @@ public static function mergeDefinition(Definition $def, ChildDefinition $definit
156157
$def->setDecoratedService($decoratedService[0], $decoratedService[1], $decoratedService[2]);
157158
}
158159
}
160+
$def->setBindings(array_merge($def->getBindings(), $definition->getBindings()));
159161

160162
// merge arguments
161163
foreach ($definition->getArguments() as $k => $v) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.