Skip to content

[DI] Introduce "container.service_locator" tag, replaces ServiceLocatorArgument #22024

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
Mar 17, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand Down Expand Up @@ -332,8 +331,6 @@ protected function describeContainerDefinition(Definition $definition, array $op
$argumentsInformation[] = sprintf('Service(%s)', (string) $argument);
} elseif ($argument instanceof IteratorArgument) {
$argumentsInformation[] = sprintf('Iterator (%d element(s))', count($argument->getValues()));
} elseif ($argument instanceof ServiceLocatorArgument) {
$argumentsInformation[] = sprintf('ServiceLocator (%d service(s))', count($argument->getValues()));
} elseif ($argument instanceof ClosureProxyArgument) {
list($reference, $method) = $argument->getValues();
$argumentsInformation[] = sprintf('ClosureProxy(Service(%s)::%s())', $reference, $method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand Down Expand Up @@ -436,12 +435,6 @@ private function getArgumentNodes(array $arguments, \DOMDocument $dom)
} elseif ($argument instanceof IteratorArgument) {
$argumentXML->setAttribute('type', 'iterator');

foreach ($this->getArgumentNodes($argument->getValues(), $dom) as $childArgumentXML) {
$argumentXML->appendChild($childArgumentXML);
}
} elseif ($argument instanceof ServiceLocatorArgument) {
$argumentXML->setAttribute('type', 'service-locator');

foreach ($this->getArgumentNodes($argument->getValues(), $dom) as $childArgumentXML) {
$argumentXML->appendChild($childArgumentXML);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;

class AddConstraintValidatorsPass implements CompilerPassInterface
{
Expand All @@ -33,12 +35,12 @@ public function process(ContainerBuilder $container)
}

if (isset($attributes[0]['alias'])) {
$validators[$attributes[0]['alias']] = new Reference($id);
$validators[$attributes[0]['alias']] = new ServiceClosureArgument(new Reference($id));
}

$validators[$definition->getClass()] = new Reference($id);
$validators[$definition->getClass()] = new ServiceClosureArgument(new Reference($id));
}

$container->getDefinition('validator.validator_factory')->replaceArgument(0, new ServiceLocatorArgument($validators));
$container->getDefinition('validator.validator_factory')->replaceArgument(0, (new Definition(ServiceLocator::class, array($validators)))->addTag('container.service_locator'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<!-- DependencyInjectionExtension -->
<service id="form.extension" class="Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension" public="false">
<argument type="service-locator" /><!-- All services with tag "form.type" are stored in a service locator by FormPass -->
<argument /><!-- All services with tag "form.type" are stored in a service locator by FormPass -->
<argument type="collection" /><!-- All services with tag "form.type_extension" are stored here by FormPass -->
<argument type="iterator" /><!-- All services with tag "form.type_guesser" are stored here by FormPass -->
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@

<service id="session_listener" class="Symfony\Component\HttpKernel\EventListener\SessionListener">
<tag name="kernel.event_subscriber" />
<argument type="service-locator">
<argument key="session" type="service" id="session" on-invalid="null" />
<argument type="service">
<service class="Symfony\Component\DependencyInjection\ServiceLocator">
<tag name="container.service_locator" />
<argument type="collection">
<argument key="session" type="service" id="session" on-invalid="ignore" />
</argument>
</service>
</argument>
</service>

Expand Down
9 changes: 7 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@

<service id="test.session.listener" class="Symfony\Component\HttpKernel\EventListener\TestSessionListener">
<tag name="kernel.event_subscriber" />
<argument type="service-locator">
<argument key="session" type="service" id="session" on-invalid="null" />
<argument type="service">
<service class="Symfony\Component\DependencyInjection\ServiceLocator">
<tag name="container.service_locator" />
<argument type="collection">
<argument key="session" type="service" id="session" on-invalid="ignore" />
</argument>
</service>
</argument>
</service>
</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</service>

<service id="validator.validator_factory" class="Symfony\Bundle\FrameworkBundle\Validator\ConstraintValidatorFactory" public="false">
<argument type="service-locator" /> <!-- Constraint validators locator -->
<argument /> <!-- Constraint validators locator -->
</service>

<service id="validator.expression" class="Symfony\Component\Validator\Constraints\ExpressionValidator">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private function assertDescription($expectedDescription, $describedObject, array
$this->getDescriptor()->describe($output, $describedObject, $options);

if ('json' === $this->getFormat()) {
$this->assertEquals(json_decode($expectedDescription), json_decode($output->fetch()));
$this->assertEquals(json_encode(json_decode($expectedDescription), JSON_PRETTY_PRINT), json_encode(json_decode($output->fetch()), JSON_PRETTY_PRINT));
} else {
$this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $output->fetch())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
Expand Down Expand Up @@ -121,10 +120,6 @@ public static function getContainerDefinitions()
new Reference('definition_2'),
)))
->addArgument(new ClosureProxyArgument('definition1', 'get'))
->addArgument(new ServiceLocatorArgument(array(
'def1' => new Reference('definition_1'),
'def2' => new Reference('definition_2'),
)))
->setFactory(array('Full\\Qualified\\FactoryClass', 'get')),
'definition_2' => $definition2
->setPublic(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;

class AddConstraintValidatorsPassTest extends TestCase
{
public function testThatConstraintValidatorServicesAreProcessed()
{
$container = new ContainerBuilder();
$validatorFactory = $container->register('validator.validator_factory')
->setArguments(array(new ServiceLocatorArgument(array())));
->addArgument(array());

$container->register('my_constraint_validator_service1', Validator1::class)
->addTag('validator.constraint_validator', array('alias' => 'my_constraint_validator_alias1'));
Expand All @@ -36,11 +38,11 @@ public function testThatConstraintValidatorServicesAreProcessed()
$addConstraintValidatorsPass = new AddConstraintValidatorsPass();
$addConstraintValidatorsPass->process($container);

$this->assertEquals(new ServiceLocatorArgument(array(
Validator1::class => new Reference('my_constraint_validator_service1'),
'my_constraint_validator_alias1' => new Reference('my_constraint_validator_service1'),
Validator2::class => new Reference('my_constraint_validator_service2'),
)), $validatorFactory->getArgument(0));
$this->assertEquals((new Definition(ServiceLocator::class, array(array(
Validator1::class => new ServiceClosureArgument(new Reference('my_constraint_validator_service1')),
'my_constraint_validator_alias1' => new ServiceClosureArgument(new Reference('my_constraint_validator_service1')),
Validator2::class => new ServiceClosureArgument(new Reference('my_constraint_validator_service2')),
))))->addTag('container.service_locator'), $validatorFactory->getArgument(0));
}

public function testThatCompilerPassIsIgnoredIfThereIsNoConstraintValidatorFactoryDefinition()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\Form\AbstractType;

/**
Expand Down Expand Up @@ -49,10 +50,10 @@ public function testAddTaggedTypes()
$extDefinition = $container->getDefinition('form.extension');

$this->assertEquals(
new ServiceLocatorArgument(array(
__CLASS__.'_Type1' => new Reference('my.type1'),
__CLASS__.'_Type2' => new Reference('my.type2'),
)),
(new Definition(ServiceLocator::class, array(array(
__CLASS__.'_Type1' => new ServiceClosureArgument(new Reference('my.type1')),
__CLASS__.'_Type2' => new ServiceClosureArgument(new Reference('my.type2')),
))))->addTag('container.service_locator'),
$extDefinition->getArgument(0)
);
}
Expand Down Expand Up @@ -196,7 +197,7 @@ private function createExtensionDefinition()
{
$definition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension');
$definition->setArguments(array(
new ServiceLocatorArgument(array()),
array(),
array(),
new IteratorArgument(array()),
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bundle\FullStack;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
Expand Down Expand Up @@ -907,7 +908,7 @@ protected function createContainerFromFile($file, $data = array(), $resetCompile
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
}
$container->getCompilerPassConfig()->setBeforeRemovingPasses(array(new AddAnnotationsCachedReaderPass()));
$container->getCompilerPassConfig()->setBeforeRemovingPasses(array(new AddAnnotationsCachedReaderPass(), new AddConstraintValidatorsPass()));
$container->compile();

return self::$containerCache[$cacheKey] = $container;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,7 @@
"id": "definition1"
},
"get"
],
{
"def1": {
"type": "service",
"id": "definition_1"
},
"def2": {
"type": "service",
"id": "definition_2"
}
}
]
],
"file": null,
"factory_class": "Full\\Qualified\\FactoryClass",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
<argument type="service" id="definition_2"/>
</argument>
<argument type="closure-proxy" id="definition1" method="get"/>
<argument type="service-locator">
<argument key="def1" type="service" id="definition_1"/>
<argument key="def2" type="service" id="definition_2"/>
</argument>
</definition>
<service id="service_container" class="Symfony\Component\DependencyInjection\ContainerBuilder"/>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,7 @@
"id": "definition1"
},
"get"
],
{
"def1": {
"type": "service",
"id": "definition_1"
},
"def2": {
"type": "service",
"id": "definition_2"
}
}
]
],
"file": null,
"factory_class": "Full\\Qualified\\FactoryClass",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@
Array (3 element(s))
Iterator (2 element(s))
ClosureProxy(Service(definition1)::get())
ServiceLocator (2 service(s))
---------------- -------------------------------------------

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@
<argument type="service" id="definition_2"/>
</argument>
<argument type="closure-proxy" id="definition1" method="get"/>
<argument type="service-locator">
<argument key="def1" type="service" id="definition_1"/>
<argument key="def2" type="service" id="definition_2"/>
</argument>
</definition>
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
use Symfony\Component\Console\Application;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;

Expand Down Expand Up @@ -260,10 +262,10 @@ private function createFirewalls($config, ContainerBuilder $container)
->replaceArgument(2, new Reference($configId))
;

$contextRefs[$contextId] = new Reference($contextId);
$contextRefs[$contextId] = new ServiceClosureArgument(new Reference($contextId));
$map[$contextId] = $matcher;
}
$mapDef->replaceArgument(0, new ServiceLocatorArgument($contextRefs));
$mapDef->replaceArgument(0, (new Definition(ServiceLocator::class, array($contextRefs)))->addTag('container.service_locator'));
$mapDef->replaceArgument(1, new IteratorArgument($map));

// add authentication providers to authentication manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ServiceLocator;

/**
* Registers Twig runtime services.
Expand All @@ -36,9 +38,9 @@ public function process(ContainerBuilder $container)
continue;
}

$mapping[$def->getClass()] = new Reference($id);
$mapping[$def->getClass()] = new ServiceClosureArgument(new Reference($id));
}

$definition->replaceArgument(0, new ServiceLocatorArgument($mapping));
$definition->replaceArgument(0, (new Definition(ServiceLocator::class, array($mapping)))->addTag('container.service_locator'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ public function testRuntimeLoader()
$container->compile();

$loader = $container->getDefinition('twig.runtime_loader');
$args = $loader->getArgument(0)->getValues();
$args = $loader->getArgument(0)->getArgument(0);
$this->assertArrayHasKey('Symfony\Bridge\Twig\Form\TwigRenderer', $args);
$this->assertArrayHasKey('FooClass', $args);
$this->assertContains('twig.form.renderer', $args);
$this->assertContains('foo', $args);
$this->assertEquals('twig.form.renderer', $args['Symfony\Bridge\Twig\Form\TwigRenderer']->getValues()[0]);
$this->assertEquals('foo', $args['FooClass']->getValues()[0]);
}

private function createContainer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
* Represents a service wrapped in a memoizing closure.
*
* @author Nicolas Grekas <p@tchwork.com>
*
* @experimental in version 3.3
*/
class ServiceClosureArgument implements ArgumentInterface
{
Expand Down
Loading