Skip to content

[DI] Deprecate XML services without ID #22903

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
May 25, 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
11 changes: 8 additions & 3 deletions UPGRADE-3.4.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
UPGRADE FROM 3.3 to 3.4
=======================

DependencyInjection
-------------------

* Top-level anonymous services in XML are deprecated and will throw an exception in Symfony 4.0.

Finder
------

* The `Symfony\Component\Finder\Iterator\FilterIterator` class has been
deprecated and will be removed in 4.0 as it used to fix a bug which existed
before version 5.5.23/5.6.7
before version 5.5.23/5.6.7.

Validator
---------

* not setting the `strict` option of the `Choice` constraint to `true` is
deprecated and will throw an exception in Symfony 4.0
* Not setting the `strict` option of the `Choice` constraint to `true` is
deprecated and will throw an exception in Symfony 4.0.
2 changes: 2 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ DependencyInjection
* The ``strict`` attribute in service arguments has been removed.
The attribute is ignored since 3.0, so you can simply remove it.

* Top-level anonymous services in XML are no longer supported.

EventDispatcher
---------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@
<argument /> <!-- resource checkers -->
</service>

<service class="Symfony\Component\DependencyInjection\Config\ContainerParametersResourceChecker">
<service id="Symfony\Component\DependencyInjection\Config\ContainerParametersResourceChecker">
<argument type="service" id="service_container" />
<tag name="config_cache.resource_checker" priority="-980" />
</service>

<service class="Symfony\Component\Config\Resource\SelfCheckingResourceChecker">
<service id="Symfony\Component\Config\Resource\SelfCheckingResourceChecker">
<tag name="config_cache.resource_checker" priority="-990" />
</service>
</services>
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* deprecated the ability to check for the initialization of a private service with the `Container::initialized()` method
* deprecated support for top-level anonymous services in XML

3.3.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ private function processAnonymousServices(\DOMDocument $xml, $file, $defaults)
// anonymous services "in the wild"
if (false !== $nodes = $xpath->query('//container:services/container:service[not(@id)]')) {
foreach ($nodes as $node) {
@trigger_error(sprintf('Top-level anonymous services are deprecated since Symfony 3.4, the "id" attribute will be required in version 4.0 in %s at line %d.', $file, $node->getLineNo()), E_USER_DEPRECATED);

// give it a unique name
$id = sprintf('%d_%s', ++$count, hash('sha256', $file));
$node->setAttribute('id', $id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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">
<services>
<service id="FooClass">
<argument type="service">
<service class="BarClass" />
</argument>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<tag name="foo" />
</defaults>

<service class="Bar" public="true" />
<service id="bar" class="Bar" public="true" />
<service id="with_defaults" class="Foo" />
<service id="no_defaults" class="Foo" public="true" autowire="false" />
<service id="child_def" parent="with_defaults" public="true" autowire="false" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</property>
</service>
<service id="bar" parent="foo" />
<service class="BizClass">
<service id="biz" class="BizClass">
<tag name="biz_tag" />
</service>
</services>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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">
<services>
<service class="FooClass"/>
<service id="FooClass">
<argument type="service">
<service class="BarClass" />
</argument>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,28 @@ public function testLoadAnonymousServices()
$this->assertSame($fooArgs[0], $barArgs[0]);
}

/**
* @group legacy
* @expectedDeprecation Top-level anonymous services are deprecated since Symfony 3.4, the "id" attribute will be required in version 4.0 in %sservices_without_id.xml at line 4.
*/
public function testLoadAnonymousServicesWithoutId()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services_without_id.xml');
}

public function testLoadAnonymousNestedServices()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('nested_service_without_id.xml');

$this->assertTrue($container->hasDefinition('FooClass'));
$arguments = $container->getDefinition('FooClass')->getArguments();
$this->assertInstanceOf(Reference::class, array_shift($arguments));
}

public function testLoadServices()
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -667,9 +689,8 @@ public function testDefaults()
$this->assertSame('service_container', key($definitions));

array_shift($definitions);
$this->assertStringStartsWith('1_', key($definitions));

$anonymous = current($definitions);
$this->assertSame('bar', key($definitions));
$this->assertTrue($anonymous->isPublic());
$this->assertTrue($anonymous->isAutowired());
$this->assertSame(array('foo' => array(array())), $anonymous->getTags());
Expand Down