8000 [DI] Allow autoconfiguring bindings by nicolas-grekas · Pull Request #27806 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Allow autoconfiguring bindings #27806

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

Merged
merged 1 commit into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
8000
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ final class BoundArgument implements ArgumentInterface
private $identifier;
private $used;

public function __construct($value)
public function __construct($value, bool $trackUsage = true)
{
$this->value = $value;
$this->identifier = ++self::$sequence;
if ($trackUsage) {
$this->identifier = ++self::$sequence;
} else {
$this->used = true;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
$parent = $shared = null;
$instanceofTags = array();
$instanceofCalls = array();
$instanceofBindings = array();

foreach ($conditionals as $interface => $instanceofDefs) {
if ($interface !== $class && (!$container->getReflectionClass($class, false))) {
Expand All @@ -79,13 +80,15 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
$parent = '.instanceof.'.$interface.'.'.$key.'.'.$id;
$container->setDefinition($parent, $instanceofDef);
$instanceofTags[] = $instanceofDef->getTags();
$instanceofBindings = $instanceofDef->getBindings() + $instanceofBindings;

foreach ($instanceofDef->getMethodCalls() as $methodCall) {
$instanceofCalls[] = $methodCall;
}

$instanceofDef->setTags(array());
$instanceofDef->setMethodCalls(array());
$instanceofDef->setBindings(array());

if (isset($instanceofDef->getChanges()['shared'])) {
$shared = $instanceofDef->isShared();
Expand Down Expand Up @@ -123,7 +126,7 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
}

$definition->setMethodCalls(array_merge($instanceofCalls, $definition->getMethodCalls()));
$definition->setBindings($bindings);
$definition->setBindings($bindings + $instanceofBindings);

// reset fields with "merge" behavior
$abstract
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class InstanceofConfigurator extends AbstractServiceConfigurator
use Traits\PublicTrait;
use Traits\ShareTrait;
use Traits\TagTrait;
use Traits\BindTrait;

/**
* Defines an instanceof-conditional to be applied to following service definitions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class YamlFileLoader extends FileLoader
'calls' => 'calls',
'tags' => 'tags',
'autowire' => 'autowire',
'bind' => 'bind',
);

private static $defaultsKeywords = array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<xsd:element name="call" type="call" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="tag" type="tag" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="property" type="property" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="bind" type="bind" minOccurs="0" maxOccurs="unbounded" />
</xsd:choice>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="shared" type="boolean" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class ResolveInstanceofConditionalsPassTest extends TestCase
{
Expand Down Expand Up @@ -270,7 +271,30 @@ public function testMergeReset()
$this->assertTrue($abstract->isAbstract());
}

public function testBindings()
public function testProcessForAutoconfiguredBindings()
{
$container = new ContainerBuilder();

$container->registerForAutoconfiguration(self::class)
->setBindings(array(
'$foo' => new BoundArgument(234, false),
parent::class => new BoundArgument(new Reference('foo'), false),
));

$container->register('foo', self::class)
->setAutoconfigured(true)
->setBindings(array('$foo' => new BoundArgument(123, false)));

(new ResolveInstanceofConditionalsPass())->process($container);

$expected = array(
'$foo' => new BoundArgument(123, false),
parent::class => new BoundArgument(new Reference('foo'), false),
);
$this->assertEquals($expected, $container->findDefinition('foo')->getBindings());
}

public function testBindingsOnInstanceofConditionals()
{
$container = new ContainerBuilder();
$def = $container->register('foo', self::class)->setBindings(array('$toto' => 123));
Expand Down
0