Skip to content

Commit 5ececce

Browse files
committed
[DependencyInjection][Routing] Deprecate XML configuration format
1 parent b494600 commit 5ececce

File tree

12 files changed

+105
-77
lines changed

12 files changed

+105
-77
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* Handle returning arrays and config-builders from config files
1414
* Handle declaring services using PHP arrays that follow the same shape as corresponding yaml files
1515
* Deprecate using `$this` or its internal scope from PHP config files; use the `$loader` variable instead
16+
* Deprecate XML configuration format, use YAML or PHP instead
1617

1718
7.3
1819
---

Loader/XmlFileLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
* XmlFileLoader loads XML files service definitions.
3535
*
3636
* @author Fabien Potencier <fabien@symfony.com>
37+
*
38+
* @deprecated since Symfony 7.4, use another loader instead
3739
*/
3840
class XmlFileLoader extends FileLoader
3941
{
@@ -43,6 +45,8 @@ class XmlFileLoader extends FileLoader
4345

4446
public function load(mixed $resource, ?string $type = null): mixed
4547
{
48+
trigger_deprecation('symfony/dependency-injection', '7.4', 'XML configuration format is deprecated, use YAML or PHP instead.');
49+
4650
$path = $this->locator->locate($resource);
4751

4852
$xml = $this->parseFileToDOM($path);

Tests/Compiler/AutowirePassTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,8 +989,12 @@ public function testExceptionWhenAliasDoesNotExist()
989989
}
990990
}
991991

992+
#[IgnoreDeprecations]
993+
#[Group('legacy')]
992994
public function testInlineServicesAreNotCandidates()
993995
{
996+
$this->expectUserDeprecationMessage('Since symfony/dependency-injection 7.4: XML configuration format is deprecated, use YAML or PHP instead.');
997+
994998
$container = new ContainerBuilder();
995999
$loader = new XmlFileLoader($container, new FileLocator(realpath(__DIR__.'/../Fixtures/xml')));
9961000
$loader->load('services_inline_not_candidate.xml');

Tests/CrossCheckTest.php

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
namespace Symfony\Component\DependencyInjection\Tests;
1313

1414
use PHPUnit\Framework\Attributes\DataProvider;
15+
use PHPUnit\Framework\Attributes\Group;
16+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1517
use PHPUnit\Framework\TestCase;
1618
use Symfony\Component\Config\FileLocator;
1719
use Symfony\Component\DependencyInjection\ContainerBuilder;
20+
use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
21+
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
22+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
23+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1824

1925
class CrossCheckTest extends TestCase
2026
{
@@ -28,25 +34,26 @@ public static function setUpBeforeClass(): void
2834
require_once self::$fixturesPath.'/includes/foo.php';
2935
}
3036

31-
#[DataProvider('crossCheckLoadersDumpers')]
32-
public function testCrossCheck($fixture, $type)
37+
#[IgnoreDeprecations]
38+
#[Group('legacy')]
39+
#[DataProvider('crossCheckXmlLoadersDumpers')]
40+
public function testXmlCrossCheck($fixture)
3341
{
34-
$loaderClass = 'Symfony\\Component\\DependencyInjection\\Loader\\'.ucfirst($type).'FileLoader';
35-
$dumperClass = 'Symfony\\Component\\DependencyInjection\\Dumper\\'.ucfirst($type).'Dumper';
42+
$this->expectUserDeprecationMessage('Since symfony/dependency-injection 7.4: XML configuration format is deprecated, use YAML or PHP instead.');
3643

3744
$tmp = tempnam(sys_get_temp_dir(), 'sf');
3845

39-
copy(self::$fixturesPath.'/'.$type.'/'.$fixture, $tmp);
46+
copy(self::$fixturesPath.'/xml/'.$fixture, $tmp);
4047

4148
$container1 = new ContainerBuilder();
42-
$loader1 = new $loaderClass($container1, new FileLocator());
49+
$loader1 = new XmlFileLoader($container1, new FileLocator());
4350
$loader1->load($tmp);
4451

45-
$dumper = new $dumperClass($container1);
52+
$dumper = new XmlDumper($container1);
4653
file_put_contents($tmp, $dumper->dump());
4754

4855
$container2 = new ContainerBuilder();
49-
$loader2 = new $loaderClass($container2, new FileLocator());
56+
$loader2 = new XmlFileLoader($container2, new FileLocator());
5057
$loader2->load($tmp);
5158

5259
unlink($tmp);
@@ -70,19 +77,64 @@ public function testCrossCheck($fixture, $type)
7077
$this->assertEquals($services1, $services2, 'Iterator on the containers returns the same services');
7178
}
7279

73-
public static function crossCheckLoadersDumpers()
80+
#[DataProvider('crossCheckYamlLoadersDumpers')]
81+
public function testYamlCrossCheck($fixture)
82+
{
83+
$tmp = tempnam(sys_get_temp_dir(), 'sf');
84+
85+
copy(self::$fixturesPath.'/yaml/'.$fixture, $tmp);
86+
87+
$container1 = new ContainerBuilder();
88+
$loader1 = new YamlFileLoader($container1, new FileLocator());
89+
$loader1->load($tmp);
90+
91+
$dumper = new YamlDumper($container1);
92+
file_put_contents($tmp, $dumper->dump());
93+
94+
$container2 = new ContainerBuilder();
95+
$loader2 = new YamlFileLoader($container2, new FileLocator());
96+
$loader2->load($tmp);
97+
98+
unlink($tmp);
99+
100+
$this->assertEquals($container2->getAliases(), $container1->getAliases(), 'loading a dump from a previously loaded container returns the same container');
101+
$this->assertEquals($container2->getDefinitions(), $container1->getDefinitions(), 'loading a dump from a previously loaded container returns the same container');
102+
$this->assertEquals($container2->getParameterBag()->all(), $container1->getParameterBag()->all(), '->getParameterBag() returns the same value for both containers');
103+
$this->assertEquals(serialize($container2), serialize($container1), 'loading a dump from a previously loaded container returns the same container');
104+
105+
$services1 = [];
106+
foreach ($container1 as $id => $service) {
107+
$services1[$id] = serialize($service);
108+
}
109+
$services2 = [];
110+
foreach ($container2 as $id => $service) {
111+
$services2[$id] = serialize($service);
112+
}
113+
114+
unset($services1['service_container'], $services2['service_container']);
115+
116+
$this->assertEquals($services2, $services1, 'Iterator on the containers returns the same services');
117+
}
118+
119+
public static function crossCheckXmlLoadersDumpers()
120+
{
121+
return [
122+
['services1.xml'],
123+
['services2.xml'],
124+
['services6.xml'],
125+
['services8.xml'],
126+
['services9.xml'],
127+
];
128+
}
129+
130+
public static function crossCheckYamlLoadersDumpers()
74131
{
75132
return [
76-
['services1.xml', 'xml'],
77-
['services2.xml', 'xml'],
78-
['services6.xml', 'xml'],
79-
['services8.xml', 'xml'],
80-
['services9.xml', 'xml'],
81-
['services1.yml', 'yaml'],
82-
['services2.yml', 'yaml'],
83-
['services6.yml', 'yaml'],
84-
['services8.yml', 'yaml'],
85-
['services9.yml', 'yaml'],
133+
['services1.yml'],
134+
['services2.yml'],
135+
['services6.yml'],
136+
['services8.yml'],
137+
['services9.yml'],
86138
];
87139
}
88140
}

Tests/Dumper/XmlDumperTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Dumper;
1313

1414
use PHPUnit\Framework\Attributes\DataProvider;
15+
use PHPUnit\Framework\Attributes\Group;
16+
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1517
use PHPUnit\Framework\TestCase;
1618
use Symfony\Component\Config\FileLocator;
1719
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
@@ -189,8 +191,12 @@ public function testDumpAutowireData()
189191
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services24.xml'), $dumper->dump());
190192
}
191193

194+
#[IgnoreDeprecations]
195+
#[Group('legacy')]
192196
public function testDumpLoad()
193197
{
198+
$this->expectUserDeprecationMessage('Since symfony/dependency-injection 7.4: XML configuration format is deprecated, use YAML or PHP instead.');
199+
194200
$container = new ContainerBuilder();
195201
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
196202
$loader->load('services_dump_load.xml');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[parameters]
2+
with_wrong_ext = from ini

Tests/Fixtures/php/services2.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$container->setParameter('a_string', 'a string');
4+
$container->setParameter('foo', 'bar');
5+
$container->setParameter('values', []);
6+
$container->setParameter('mixedcase', []);
7+
$container->setParameter('constant', PHP_EOL);

Tests/Fixtures/xml/xml_with_wrong_ext.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

Tests/Fixtures/yaml/services4.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ imports:
44
- { resource: "../php/simple.php" }
55
- { resource: "../ini/parameters.ini", class: Symfony\Component\DependencyInjection\Loader\IniFileLoader }
66
- { resource: "../ini/parameters2.ini" }
7-
- { resource: "../xml/services13.xml" }
8-
- { resource: "../xml/xml_with_wrong_ext.php", type: xml }
7+
- { resource: "../ini/ini_with_wrong_ext.php", type: ini }

Tests/Loader/FileLoaderTest.php

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@
2323
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2424
use Symfony\Component\DependencyInjection\Exception\LogicException;
2525
use Symfony\Component\DependencyInjection\Loader\FileLoader;
26-
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
2726
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
28-
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2927
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
30-
use Symfony\Component\DependencyInjection\Reference;
3128
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\AbstractClass;
3229
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\BadClasses\MissingParent;
3330
use Symfony\Component\DependencyInjection\Tests\Fixtures\Prototype\Foo;
@@ -66,41 +63,17 @@ public function testImportWithGlobPattern()
6663
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath));
6764

6865
$resolver = new LoaderResolver([
69-
new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/ini')),
70-
new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
7166
new PhpFileLoader($container, new FileLocator(self::$fixturesPath.'/php')),
7267
new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
7368
]);
7469

7570
$loader->setResolver($resolver);
76-
$loader->import('{F}ixtures/{xml,yaml}/services2.{yml,xml}');
71+
$loader->import('{F}ixtures/{php,yaml}/services2.{php,yml}');
7772

7873
$actual = $container->getParameterBag()->all();
79-
$expected = [
80-
'a_string' => 'a string',
81-
'foo' => 'bar',
82-
'values' => [
83-
0,
84-
'integer' => 4,
85-
100 => null,
86-
'true',
87-
true,
88-
false,
89-
'on',
90-
'off',
91-
'float' => 1.3,
92-
1000.3,
93-
'a string',
94-
['foo', 'bar'],
95-
],
96-
'mixedcase' => ['MixedCaseKey' => 'value'],
97-
'constant' => \PHP_EOL,
98-
'bar' => '%foo%',
99-
'escape' => '@escapeme',
100-
'foo_bar' => new Reference('foo_bar'),
101-
];
102-
103-
$this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
74+
$expectedKeys = ['a_string', 'foo', 'values', 'mixedcase', 'constant', 'bar', 'escape', 'foo_bar'];
75+
76+
$this->assertEquals($expectedKeys, array_keys($actual), '->load() imports and merges imported files');
10477
}
10578

10679
public function testRegisterClasses()

0 commit comments

Comments
 (0)