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

[DependencyInjection] Support local binding #22187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixes
  • Loading branch information
GuilhemN committed Jul 23, 2017
commit 24b29d9dc14b51b745cf29587b9a97c54d4fc1ea
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
final class BoundArgument implements ArgumentInterface
{
private static $count = 0;
private static $sequence = 0;

private $value;
private $identifier;
Expand All @@ -25,7 +25,7 @@ final class BoundArgument implements ArgumentInterface
public function __construct($value)
{
$this->value = $value;
$this->identifier = ++self::$count;
$this->identifier = ++self::$sequence;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ protected function processValue($value, $isRoot = false)
}

foreach ($parameters as $j => $p) {
$typeHint = ProxyHelper::getTypeHint($r, $p, true);

if ($typeHint === $key) {
if (ProxyHelper::getTypeHint($r, $p, true) === $key) {
$resolvedArguments[$j] = $argument;

continue 2;
Expand Down
4 changes: 4 additions & 0 deletions 4 src/Symfony/Component/DependencyInjection/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,10 @@ public function getBindings()
/**
* Sets bindings.
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add some explanations, eg:
Bindings map $named arguments or type-hints to values that should be injected in the matching parameters of the constructor and of setters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adapted a bit your proposal to:

Bindings map $named or FQCN arguments to values that should be
injected in the matching parameters (of the constructor, of methods
called and of controller actions).

Ok for you?

* Bindings map $named or FQCN arguments to values that should be
* injected in the matching parameters (of the constructor, of methods
* called and of controller actions).
*
* @param array $bindings
*
* @return $this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ private function getServiceDefaults(\DOMDocument $xml, $file)
}
$defaults = array(
'tags' => $this->getChildren($defaultsNode, 'tag'),
'bind' => array_map(function ($v) {
return new BoundArgument($v);
}, $this->getArgumentsAsPhp($defaultsNode, 'bind', $file)),
'bind' => array_map(function ($v) { return new BoundArgument($v); }, $this->getArgumentsAsPhp($defaultsNode, 'bind', $file)),
);

foreach ($defaults['tags'] as $tag) {
Expand Down Expand Up @@ -358,6 +356,8 @@ private function parseDefinition(\DOMElement $service, $file, array $defaults)

$bindings = $this->getArgumentsAsPhp($service, 'bind', $file);
if (isset($defaults['bind'])) {
// deep clone, to avoid multiple process of the same instance in the
// passes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment should be on one line
// deep clone, to avoid processing the same definitions several times in compiler passes

$bindings = array_merge(unserialize(serialize($defaults['bind'])), $bindings);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small comment needed to explain the deep clone

}
if ($bindings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,7 @@ private function parseDefaults(array &$content, $file)
throw new InvalidArgumentException(sprintf('Parameter "bind" in "_defaults" must be an array in %s. Check your YAML syntax.', $file));
}

$defaults['bind'] = array_map(function ($v) {
return new BoundArgument($v);
}, $this->resolveServices($defaults['bind'], $file));
$defaults['bind'] = array_map(function ($v) { return new BoundArgument($v); }, $this->resolveServices($defaults['bind'], $file));
}

return $defaults;
Expand Down Expand Up @@ -537,7 +535,10 @@ private function parseDefinition($id, $service, $file, array $defaults)
}

if (isset($defaults['bind']) || isset($service['bind'])) {
// deep clone, to avoid multiple process of the same instance in the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above

// passes
$bindings = isset($defaults['bind']) ? unserialize(serialize($defaults['bind'])) : array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment need


if (isset($service['bind'])) {
if (!is_array($service['bind'])) {
throw new InvalidArgumentException(sprintf('Parameter "bind" must be an array for service "%s" in %s. Check your YAML syntax.', $id, $file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,15 @@ public function testArgumentWithNoTypeHintIsOk()
}

/**
* @dataProvider argumentNameProvider
* @dataProvider provideBindings
*/
public function testBindings($argumentName)
public function testBindings($bindingName)
{
$container = new ContainerBuilder();
$resolver = $container->register('argument_resolver.service')->addArgument(array());

$container->register('foo', RegisterTestController::class)
->setBindings(array($argumentName => new Reference('foo')))
->setBindings(array($bindingName => new Reference('foo')))
->addTag('controller.service_arguments');

$pass = new RegisterControllerArgumentLocatorsPass();
Expand All @@ -291,7 +291,7 @@ public function testBindings($argumentName)
$this->assertEquals($expected, $locator->getArgument(0));
}

public function argumentNameProvider()
public function provideBindings()
{
return array(array(ControllerDummy::class), array('$bar'));
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.