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

This commit is contained in:
Nicolas Grekas 2017-03-16 15:19:51 +01:00
parent 18bbf3741a
commit 5d230b5871
68 changed files with 142 additions and 536 deletions

View File

@ -17,7 +17,6 @@ use Symfony\Component\DependencyInjection\Alias;
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;
@ -332,8 +331,6 @@ class TextDescriptor extends Descriptor
$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);

View File

@ -15,7 +15,6 @@ use Symfony\Component\DependencyInjection\Alias;
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;
@ -436,12 +435,6 @@ class XmlDescriptor extends Descriptor
} 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);
}

View File

@ -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
{
@ -33,12 +35,12 @@ class AddConstraintValidatorsPass implements CompilerPassInterface
}
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'));
}
}

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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">

View File

@ -192,7 +192,7 @@ abstract class AbstractDescriptorTest extends TestCase
$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())));
}

View File

@ -14,7 +14,6 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
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;
@ -121,10 +120,6 @@ class ObjectsProvider
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)

View File

@ -13,9 +13,11 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
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
{
@ -23,7 +25,7 @@ class AddConstraintValidatorsPassTest extends TestCase
{
$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'));
@ -36,11 +38,11 @@ class AddConstraintValidatorsPassTest extends TestCase
$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()

View File

@ -14,10 +14,11 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
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;
/**
@ -49,10 +50,10 @@ class FormPassTest extends TestCase
$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)
);
}
@ -196,7 +197,7 @@ class FormPassTest extends TestCase
{
$definition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension');
$definition->setArguments(array(
new ServiceLocatorArgument(array()),
array(),
array(),
new IteratorArgument(array()),
));

View File

@ -15,6 +15,7 @@ use Doctrine\Common\Annotations\Annotation;
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;
@ -907,7 +908,7 @@ abstract class FrameworkExtensionTest extends TestCase
$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;

View File

@ -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",

View File

@ -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>

View File

@ -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",

View File

@ -18,6 +18,5 @@
Array (3 element(s))
Iterator (2 element(s))
ClosureProxy(Service(definition1)::get())
ServiceLocator (2 service(s))
---------------- -------------------------------------------

View File

@ -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>

View File

@ -17,12 +17,14 @@ use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
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;
@ -260,10 +262,10 @@ class SecurityExtension extends Extension
->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

View File

@ -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.
@ -36,9 +38,9 @@ class RuntimeLoaderPass implements CompilerPassInterface
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'));
}
}

View File

@ -244,11 +244,11 @@ class TwigExtensionTest extends TestCase
$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()

View File

@ -18,8 +18,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
* Represents a service wrapped in a memoizing closure.
*
* @author Nicolas Grekas <p@tchwork.com>
*
* @experimental in version 3.3
*/
class ServiceClosureArgument implements ArgumentInterface
{

View File

@ -1,49 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Argument;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
/**
* Represents a service locator able to lazy load a given range of services.
*
* @author Robin Chalas <robin.chalas@gmail.com>
*/
class ServiceLocatorArgument implements ArgumentInterface
{
private $values;
/**
* @param Reference[] $values An array of references indexed by identifier
*/
public function __construct(array $values)
{
$this->setValues($values);
}
public function getValues()
{
return $this->values;
}
public function setValues(array $values)
{
foreach ($values as $v) {
if (!$v instanceof Reference && null !== $v) {
throw new InvalidArgumentException('Values of a ServiceLocatorArgument must be Reference objects.');
}
}
$this->values = $values;
}
}

View File

@ -4,10 +4,10 @@ CHANGELOG
3.3.0
-----
* added "container.service_locator" tag for defining service-locator services
* added anonymous services support in YAML configuration files using the `!service` tag.
* [EXPERIMENTAL] added "TypedReference" and "ServiceClosureArgument" for creating service-locator services
* added "TypedReference" and "ServiceClosureArgument" for creating service-locator services
* [EXPERIMENTAL] added "instanceof" section for local interface-defined configs
* added "service-locator" argument for lazy loading a set of identified values and services
* [EXPERIMENTAL] added prototype services for PSR4-based discovery and registration
* added `ContainerBuilder::getReflectionClass()` for retrieving and tracking reflection class info
* deprecated `ContainerBuilder::getClassResource()`, use `ContainerBuilder::getReflectionClass()` or `ContainerBuilder::addObjectResource()` instead

View File

@ -49,6 +49,7 @@ class PassConfig
$this->optimizationPasses = array(array(
new ExtensionCompilerPass(),
new ResolveDefinitionTemplatesPass(),
new ServiceLocatorTagPass(),
new DecoratorServicePass(),
new ResolveParameterPlaceHoldersPass(),
new ResolveFactoryClassPass(),

View File

@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* Applies the "container.service_locator" tag by wrapping references into ServiceClosureArgument instances.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class ServiceLocatorTagPass extends AbstractRecursivePass
{
protected function processValue($value, $isRoot = false)
{
if (!$value instanceof Definition || !$value->hasTag('container.service_locator')) {
return parent::processValue($value, $isRoot);
}
if (!$value->getClass()) {
$value->setClass(ServiceLocator::class);
}
$arguments = $value->getArguments();
if (!isset($arguments[0]) || !is_array($arguments[0])) {
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set.', $this->currentId));
}
foreach ($arguments[0] as $k => $v) {
if ($v instanceof ServiceClosureArgument) {
continue;
}
if (!$v instanceof Reference) {
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set, "%s" found for key "%s".', $this->currentId, is_object($v) ? get_class($v) : gettype($v), $k));
}
$arguments[0][$k] = new ServiceClosureArgument($v);
}
return $value->setArguments($arguments);
}
}

View File

@ -16,7 +16,6 @@ use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
@ -1146,18 +1145,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
$value = function () use ($reference) {
return $this->resolveServices($reference);
};
} elseif ($value instanceof ServiceLocatorArgument) {
$parameterBag = $this->getParameterBag();
$services = array();
foreach ($value->getValues() as $k => $v) {
if ($v && $v->getInvalidBehavior() === ContainerInterface::IGNORE_ON_INVALID_REFERENCE && !$this->has((string) $v)) {
continue;
}
$services[$k] = function () use ($v, $parameterBag) {
return $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($v)));
};
}
$value = new ServiceLocator($services);
} elseif ($value instanceof IteratorArgument) {
$parameterBag = $this->getParameterBag();
$value = new RewindableGenerator(function () use ($value, $parameterBag) {

View File

@ -15,7 +15,6 @@ use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
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\Variable;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
@ -907,7 +906,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
$bagClass
use Symfony\Component\DependencyInjection\ServiceLocator;
/*{$this->docStar}
* $class.
@ -1548,15 +1546,16 @@ EOF;
return sprintf('array(%s)', implode(', ', $code));
} elseif ($value instanceof ServiceClosureArgument) {
return $this->dumpServiceClosure($value->getValues()[0], $interpolate, false);
} elseif ($value instanceof ServiceLocatorArgument) {
$code = "\n";
foreach ($value->getValues() as $k => $v) {
$code .= sprintf(" %s => %s,\n", $this->dumpValue($k, $interpolate), $this->dumpServiceClosure($v, $interpolate, true));
}
$code .= ' ';
$value = $value->getValues()[0];
$code = $this->dumpValue($value, $interpolate);
return sprintf('new ServiceLocator(array(%s))', $code);
if ($value instanceof TypedReference) {
$code = sprintf('$f = function (\\%s $v%s) { return $v; }; return $f(%s);', $value->getType(), ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $value->getInvalidBehavior() ? ' = null' : '', $code);
} else {
$code = sprintf('return %s;', $code);
}
return sprintf("function () {\n %s\n }", $code);
} elseif ($value instanceof IteratorArgument) {
$countCode = array();
$countCode[] = 'function () {';
@ -1692,23 +1691,6 @@ EOF;
return $this->export($value);
}
private function dumpServiceClosure(Reference $reference = null, $interpolate, $oneLine)
{
$code = $this->dumpValue($reference, $interpolate);
if ($reference instanceof TypedReference) {
$code = sprintf('$f = function (\\%s $v%s) { return $v; }; return $f(%s);', $reference->getType(), ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior() ? ' = null' : '', $code);
} else {
$code = sprintf('return %s;', $code);
}
if ($oneLine) {
return sprintf('function () { %s }', $code);
}
return sprintf("function () {\n %s\n }", $code);
}
/**
* Dumps a string to a literal (aka PHP Code) class value.
*

View File

@ -14,7 +14,6 @@ namespace Symfony\Component\DependencyInjection\Dumper;
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\ContainerInterface;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
@ -296,9 +295,6 @@ class XmlDumper extends Dumper
if (is_array($value)) {
$element->setAttribute('type', 'collection');
$this->convertParameters($value, $type, $element, 'key');
} elseif ($value instanceof ServiceLocatorArgument) {
$element->setAttribute('type', 'service-locator');
$this->convertParameters($value->getValues(), $type, $element);
} elseif ($value instanceof IteratorArgument) {
$element->setAttribute('type', 'iterator');
$this->convertParameters($value->getValues(), $type, $element, 'key');

View File

@ -18,7 +18,6 @@ use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
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\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Parameter;
@ -263,8 +262,6 @@ class YamlDumper extends Dumper
$tag = 'iterator';
} elseif ($value instanceof ClosureProxyArgument) {
$tag = 'closure_proxy';
} elseif ($value instanceof ServiceLocatorArgument) {
$tag = 'service_locator';
} else {
throw new RuntimeException(sprintf('Unspecified Yaml tag for type "%s".', get_class($value)));
}

View File

@ -16,7 +16,6 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
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\Definition;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Reference;
@ -488,15 +487,6 @@ class XmlFileLoader extends FileLoader
case 'iterator':
$arguments[$key] = new IteratorArgument($this->getArgumentsAsPhp($arg, $name, false));
break;
case 'service-locator':
$values = $this->getArgumentsAsPhp($arg, $name, false);
foreach ($values as $v) {
if (!$v instanceof Reference) {
throw new InvalidArgumentException('"service-locator" argument values must be services.');
}
}
$arguments[$key] = new ServiceLocatorArgument($values);
break;
case 'string':
$arguments[$key] = $arg->nodeValue;
break;

View File

@ -15,7 +15,6 @@ use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
@ -671,19 +670,6 @@ class YamlFileLoader extends FileLoader
return new IteratorArgument($this->resolveServices($argument, $file, $isParameter));
}
if ('service_locator' === $value->getTag()) {
if (!is_array($argument)) {
throw new InvalidArgumentException('"!service_locator" tag only accepts mappings.');
}
foreach ($argument as $v) {
if (!is_string($v) || 0 !== strpos($v[0], '@') || 0 === strpos($v[0], '@@')) {
throw new InvalidArgumentException('"!service_locator" tagged values must be {key: @service} mappings.');
}
}
return new ServiceLocatorArgument($this->resolveServices($argument, $file, $isParameter));
}
if ('closure_proxy' === $value->getTag()) {
if (!is_array($argument) || array(0, 1) !== array_keys($argument) || !is_string($argument[0]) || !is_string($argument[1]) || 0 !== strpos($argument[0], '@') || 0 === strpos($argument[0], '@@')) {
throw new InvalidArgumentException('"!closure_proxy" tagged values must be arrays of [@service, method].');

View File

@ -262,7 +262,6 @@
<xsd:enumeration value="string" />
<xsd:enumeration value="constant" />
<xsd:enumeration value="iterator" />
<xsd:enumeration value="service-locator" />
<xsd:enumeration value="closure-proxy" />
</xsd:restriction>
</xsd:simpleType>

View File

@ -1,35 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Tests\Argument;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Reference;
class ServiceLocatorArgumentTest extends TestCase
{
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Values of a ServiceLocatorArgument must be Reference objects.
*/
public function testThrowsOnNonReferenceValues()
{
new ServiceLocatorArgument(array('foo' => 'bar'));
}
public function testAcceptsReferencesOrNulls()
{
$locator = new ServiceLocatorArgument($values = array('foo' => new Reference('bar'), 'bar' => null));
$this->assertSame($values, $locator->getValues());
}
}

View File

@ -24,7 +24,6 @@ use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\Container;
@ -471,24 +470,6 @@ class ContainerBuilderTest extends TestCase
$this->assertEquals(1, $i);
}
public function testCreateServiceWithServiceLocatorArgument()
{
$builder = new ContainerBuilder();
$builder->register('bar', 'stdClass');
$builder
->register('lazy_context', 'LazyContext')
->setArguments(array(new ServiceLocatorArgument(array('bar' => new Reference('bar'), 'invalid' => new Reference('invalid', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))))
;
$lazyContext = $builder->get('lazy_context');
$locator = $lazyContext->lazyValues;
$this->assertInstanceOf(ServiceLocator::class, $locator);
$this->assertInstanceOf('stdClass', $locator->get('bar'));
$this->assertFalse($locator->has('invalid'));
$this->assertSame($locator->get('bar'), $locator('bar'), '->get() should be used when invoking ServiceLocator');
}
/**
* @expectedException \RuntimeException
*/

View File

@ -17,7 +17,6 @@ use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
@ -527,35 +526,6 @@ class PhpDumperTest extends TestCase
$dumper->dump();
}
public function testServiceLocatorArgumentProvideServiceLocator()
{
require_once self::$fixturesPath.'/includes/classes.php';
$container = new ContainerBuilder();
$container->register('lazy_referenced', 'stdClass');
$container
->register('lazy_context', 'LazyContext')
->setArguments(array(new ServiceLocatorArgument(array('lazy1' => new Reference('lazy_referenced'), 'lazy2' => new Reference('lazy_referenced'), 'container' => new Reference('service_container')))))
;
$container->compile();
$dumper = new PhpDumper($container);
$dump = $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Locator_Argument_Provide_Service_Locator'));
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_locator_argument.php', $dump);
require_once self::$fixturesPath.'/php/services_locator_argument.php';
$container = new \Symfony_DI_PhpDumper_Test_Locator_Argument_Provide_Service_Locator();
$lazyContext = $container->get('lazy_context');
$this->assertInstanceOf(ServiceLocator::class, $lazyContext->lazyValues);
$this->assertSame($container, $lazyContext->lazyValues->get('container'));
$this->assertInstanceOf('stdClass', $lazy1 = $lazyContext->lazyValues->get('lazy1'));
$this->assertInstanceOf('stdClass', $lazy2 = $lazyContext->lazyValues->get('lazy2'));
$this->assertSame($lazy1, $lazy2);
$this->assertFalse($lazyContext->lazyValues->has('lazy_referenced'), '->has() returns false for the original service id, only the key can be used');
}
public function testLazyArgumentProvideGenerator()
{
require_once self::$fixturesPath.'/includes/classes.php';

View File

@ -5,7 +5,6 @@ require_once __DIR__.'/../includes/foo.php';
use Symfony\Component\DependencyInjection\Argument\ClosureProxyArgument;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
@ -145,13 +144,5 @@ $container
->register('closure_proxy', 'BarClass')
->setArguments(array(new ClosureProxyArgument('closure_proxy', 'getBaz')))
;
$container
->register('service_locator', 'Bar')
->setArguments(array(new ServiceLocatorArgument(array(
'bar' => new Reference('bar'),
'invalid' => new Reference('invalid', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
'container' => new Reference('service_container'),
))))
;
return $container;

View File

@ -30,7 +30,6 @@ digraph sc {
node_lazy_context [label="lazy_context\nLazyContext\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_lazy_context_ignore_invalid_ref [label="lazy_context_ignore_invalid_ref\nLazyContext\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_closure_proxy [label="closure_proxy\nBarClass\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_service_locator [label="service_locator\nBar\n", shape=record, fillcolor="#eeeeee", style="filled"];
node_foo2 [label="foo2\n\n", shape=record, fillcolor="#ff9999", style="filled"];
node_foo3 [label="foo3\n\n", shape=record, fillcolor="#ff9999", style="filled"];
node_foobaz [label="foobaz\n\n", shape=record, fillcolor="#ff9999", style="filled"];
@ -53,7 +52,4 @@ digraph sc {
node_lazy_context_ignore_invalid_ref -> node_foo_baz [label="" style="filled" color="#9999ff"];
node_lazy_context_ignore_invalid_ref -> node_invalid [label="" style="filled" color="#9999ff"];
node_closure_proxy -> node_closure_proxy [label="" style="filled" color="#9999ff"];
node_service_locator -> node_bar [label="" style="filled" color="#9999ff"];
node_service_locator -> node_invalid [label="" style="filled" color="#9999ff"];
node_service_locator -> node_service_container [label="" style="filled" color="#9999ff"];
}

View File

@ -8,7 +8,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* Container.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* Symfony_DI_PhpDumper_Test_Overriden_Getters.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.
@ -59,7 +58,6 @@ class ProjectServiceContainer extends Container
'new_factory' => 'getNewFactoryService',
'new_factory_service' => 'getNewFactoryServiceService',
'service_from_static_method' => 'getServiceFromStaticMethodService',
'service_locator' => 'getServiceLocatorService',
);
$this->privates = array(
'configurator_service' => true,
@ -410,23 +408,6 @@ class ProjectServiceContainer extends Container
return $this->services['service_from_static_method'] = \Bar\FooClass::getInstance();
}
/**
* Gets the 'service_locator' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return \Bar A Bar instance
*/
protected function getServiceLocatorService()
{
return $this->services['service_locator'] = new \Bar(new ServiceLocator(array(
'bar' => function () { return ${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->get('bar')) && false ?: '_'}; },
'invalid' => function () { return $this->get('invalid', ContainerInterface::NULL_ON_INVALID_REFERENCE); },
'container' => function () { return $this; },
)));
}
/**
* Gets the 'configurator_service' service.
*

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.
@ -55,7 +54,6 @@ class ProjectServiceContainer extends Container
'method_call1' => 'getMethodCall1Service',
'new_factory_service' => 'getNewFactoryServiceService',
'service_from_static_method' => 'getServiceFromStaticMethodService',
'service_locator' => 'getServiceLocatorService',
);
$this->aliases = array(
'alias_for_alias' => 'foo',
@ -398,22 +396,6 @@ class ProjectServiceContainer extends Container
return $this->services['service_from_static_method'] = \Bar\FooClass::getInstance();
}
/**
* Gets the 'service_locator' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return \Bar A Bar instance
*/
protected function getServiceLocatorService()
{
return $this->services['service_locator'] = new \Bar(new ServiceLocator(array(
'bar' => function () { return ${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->get('bar')) && false ?: '_'}; },
'container' => function () { return $this; },
)));
}
/**
* {@inheritdoc}
*/

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* Symfony_DI_PhpDumper_Test_Overriden_Getters_With_Constructor.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -1,89 +0,0 @@
<?php
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* Symfony_DI_PhpDumper_Test_Locator_Argument_Provide_Service_Locator.
*
* This class has been auto-generated
* by the Symfony Dependency Injection Component.
*
* @final since Symfony 3.3
*/
class Symfony_DI_PhpDumper_Test_Locator_Argument_Provide_Service_Locator extends Container
{
private $parameters;
private $targetDirs = array();
/**
* Constructor.
*/
public function __construct()
{
$this->services = array();
$this->normalizedIds = array(
'psr\\container\\containerinterface' => 'Psr\\Container\\ContainerInterface',
'symfony\\component\\dependencyinjection\\container' => 'Symfony\\Component\\DependencyInjection\\Container',
'symfony\\component\\dependencyinjection\\containerinterface' => 'Symfony\\Component\\DependencyInjection\\ContainerInterface',
);
$this->methodMap = array(
'lazy_context' => 'getLazyContextService',
'lazy_referenced' => 'getLazyReferencedService',
);
$this->aliases = array();
}
/**
* {@inheritdoc}
*/
public function compile()
{
throw new LogicException('You cannot compile a dumped frozen container.');
}
/**
* {@inheritdoc}
*/
public function isFrozen()
{
return true;
}
/**
* Gets the 'lazy_context' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return \LazyContext A LazyContext instance
*/
protected function getLazyContextService()
{
return $this->services['lazy_context'] = new \LazyContext(new ServiceLocator(array(
'lazy1' => function () { return ${($_ = isset($this->services['lazy_referenced']) ? $this->services['lazy_referenced'] : $this->get('lazy_referenced')) && false ?: '_'}; },
'lazy2' => function () { return ${($_ = isset($this->services['lazy_referenced']) ? $this->services['lazy_referenced'] : $this->get('lazy_referenced')) && false ?: '_'}; },
'container' => function () { return $this; },
)));
}
/**
* Gets the 'lazy_referenced' service.
*
* This service is shared.
* This method always returns the same instance of the service.
*
* @return \stdClass A stdClass instance
*/
protected function getLazyReferencedService()
{
return $this->services['lazy_referenced'] = new \stdClass();
}
}

View File

@ -7,7 +7,6 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* ProjectServiceContainer.

View File

@ -137,13 +137,6 @@
<service id="closure_proxy" class="BarClass">
<argument type="closure-proxy" id="closure_proxy" method="getBaz"/>
</service>
<service id="service_locator" class="Bar">
<argument type="service-locator">
<argument key="bar" type="service" id="bar"/>
<argument key="invalid" type="service" id="invalid" on-invalid="ignore"/>
<argument key="container" type="service" id="service_container"/>
</argument>
</service>
<service id="Psr\Container\ContainerInterface" alias="service_container" public="false"/>
<service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" public="false"/>
<service id="Symfony\Component\DependencyInjection\Container" alias="service_container" public="false"/>

View File

@ -1,25 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="baz_class">BazClass</parameter>
<parameter key="foo">bar</parameter>
</parameters>
<services>
<service id="foo.baz" class="%baz_class%">
<factory class="%baz_class%" method="getInstance"/>
<configurator class="%baz_class%" method="configureStatic1"/>
</service>
<service id="lazy_context" class="LazyContext">
<argument type="service-locator">
<argument key="foo_baz" type="service" id="foo.baz"/>
<argument key="container" type="service" id="service_container"/>
</argument>
</service>
<service id="lazy_context_ignore_invalid_ref" class="LazyContext">
<argument type="service-locator">
<argument key="foo_baz" type="service" id="foo.baz"/>
<argument key="invalid" type="service" id="invalid" on-invalid="ignore"/>
</argument>
</service>
</services>
</container>

View File

@ -121,9 +121,6 @@ services:
arguments: [!closure_proxy ['@closure_proxy', getBaz]]
alias_for_foo: '@foo'
alias_for_alias: '@foo'
service_locator:
class: Bar
arguments: [!service_locator { bar: '@bar', invalid: '@?invalid', container: '@service_container' }]
Psr\Container\ContainerInterface:
alias: service_container
public: false

View File

@ -1,15 +0,0 @@
parameters:
baz_class: BazClass
foo: bar
services:
foo.baz:
class: '%baz_class%'
factory: ['%baz_class%', getInstance]
configurator: ['%baz_class%', configureStatic1]
lazy_context:
class: LazyContext
arguments: [!service_locator {foo_baz: '@foo.baz', container: '@service_container'} ]
lazy_context_ignore_invalid_ref:
class: LazyContext
arguments: [!service_locator {foo_baz: '@foo.baz', invalid: '@?invalid'}]

View File

@ -13,7 +13,6 @@ namespace Symfony\Component\DependencyInjection\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
@ -278,16 +277,6 @@ class XmlFileLoaderTest extends TestCase
$this->assertEquals(array(new IteratorArgument(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'foobar' => '%foo%'), true, new Reference('service_container')))), $lazyDefinition->getArguments(), '->load() parses lazy arguments');
}
public function testParsesServiceLocatorArgument()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services_locator_argument.xml');
$this->assertEquals(array(new ServiceLocatorArgument(array('foo_baz' => new Reference('foo.baz'), 'container' => new Reference('service_container')))), $container->getDefinition('lazy_context')->getArguments(), '->load() parses service-locator arguments');
$this->assertEquals(array(new ServiceLocatorArgument(array('foo_baz' => new Reference('foo.baz'), 'invalid' => new Reference('invalid', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))), $container->getDefinition('lazy_context_ignore_invalid_ref')->getArguments(), '->load() parses service-locator arguments');
}
public function testParsesTags()
{
$container = new ContainerBuilder();

View File

@ -13,7 +13,6 @@ namespace Symfony\Component\DependencyInjection\Tests\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@ -356,16 +355,6 @@ class YamlFileLoaderTest extends TestCase
$this->assertEquals(array(new IteratorArgument(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'foobar' => '%foo%'), true, new Reference('service_container')))), $lazyDefinition->getArguments(), '->load() parses lazy arguments');
}
public function testParsesServiceLocatorArgument()
{
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('services_locator_argument.yml');
$this->assertEquals(array(new ServiceLocatorArgument(array('foo_baz' => new Reference('foo.baz'), 'container' => new Reference('service_container')))), $container->getDefinition('lazy_context')->getArguments(), '->load() parses service-locator arguments');
$this->assertEquals(array(new ServiceLocatorArgument(array('foo_baz' => new Reference('foo.baz'), 'invalid' => new Reference('invalid', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))), $container->getDefinition('lazy_context_ignore_invalid_ref')->getArguments(), '->load() parses service-locator arguments');
}
public function testAutowire()
{
$container = new ContainerBuilder();

View File

@ -15,8 +15,6 @@ namespace Symfony\Component\DependencyInjection;
* Represents a PHP type-hinted service reference.
*
* @author Nicolas Grekas <p@tchwork.com>
*
* @experimental in version 3.3
*/
class TypedReference extends Reference
{

View File

@ -12,13 +12,14 @@
namespace Symfony\Component\Form\DependencyInjection;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* Adds all services with the tags "form.type", "form.type_extension" and
@ -60,19 +61,16 @@ class FormPass implements CompilerPassInterface
// Get service locator argument
$servicesMap = array();
$locator = $definition->getArgument(0);
if ($locator instanceof ServiceLocatorArgument) {
$servicesMap = $locator->getValues();
}
// Builds an array with fully-qualified type class names as keys and service IDs as values
foreach ($container->findTaggedServiceIds($this->formTypeTag) as $serviceId => $tag) {
$serviceDefinition = $container->getDefinition($serviceId);
// Add form type service to the service locator
$servicesMap[$serviceDefinition->getClass()] = new Reference($serviceId);
$servicesMap[$serviceDefinition->getClass()] = new ServiceClosureArgument(new Reference($serviceId));
}
return new ServiceLocatorArgument($servicesMap);
return (new Definition(ServiceLocator::class, array($servicesMap)))->addTag('container.service_locator');
}
private function processFormTypeExtensions(ContainerBuilder $container)

View File

@ -13,11 +13,12 @@ namespace Symfony\Component\Form\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\Form\DependencyInjection\FormPass;
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;
/**
@ -47,10 +48,10 @@ class FormPassTest extends TestCase
$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)
);
}
@ -185,7 +186,7 @@ class FormPassTest extends TestCase
{
$definition = new Definition('Symfony\Component\Form\Extension\DependencyInjection\DependencyInjectionExtension');
$definition->setArguments(array(
new ServiceLocatorArgument(array()),
array(),
array(),
new IteratorArgument(array()),
));

View File

@ -11,11 +11,13 @@
namespace Symfony\Component\HttpKernel\DependencyInjection;
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\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
/**
@ -62,10 +64,10 @@ class FragmentRendererPass implements CompilerPassInterface
}
foreach ($tags as $tag) {
$renderers[$tag['alias']] = new Reference($id);
$renderers[$tag['alias']] = new ServiceClosureArgument(new Reference($id));
}
}
$definition->replaceArgument(0, new ServiceLocatorArgument($renderers));
$definition->replaceArgument(0, (new Definition(ServiceLocator::class, array($renderers)))->addTag('container.service_locator'));
}
}

View File

@ -12,8 +12,10 @@
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\DependencyInjection\FragmentRendererPass;
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
@ -63,7 +65,7 @@ class FragmentRendererPassTest extends TestCase
$renderer
->expects($this->once())
->method('replaceArgument')
->with(0, $this->equalTo(new ServiceLocatorArgument(array('foo' => new Reference('my_content_renderer')))));
->with(0, $this->equalTo((new Definition(ServiceLocator::class, array(array('foo' => new ServiceClosureArgument(new Reference('my_content_renderer'))))))->addTag('container.service_locator')));
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
$definition->expects($this->atLeastOnce())