10000 [DependencyInjection] added autodetection of resource format when imp… · Dahipster/symfony@bb3340d · GitHub
[go: up one dir, main page]

Skip to content

Commit bb3340d

Browse files
committed
[DependencyInjection] added autodetection of resource format when importing a resource in an XML or YAML file
1 parent 53980bb commit bb3340d

File tree

9 files changed

+46
-6
lines changed

9 files changed

+46
-6
lines changed

src/Symfony/Components/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,27 @@ protected function parseImports(BuilderConfiguration $configuration, $xml, $file
8686

8787
protected function parseImport($import, $file)
8888
{
89+
$class = null;
8990
if (isset($import['class']) && $import['class'] !== get_class($this))
9091
{
9192
$class = (string) $import['class'];
92-
$loader = new $class($this->paths);
9393
}
9494
else
9595
{
96-
$loader = $this;
96+
// try to detect loader with the extension
97+
switch (pathinfo((string) $import['resource'], PATHINFO_EXTENSION))
98+
{
99+
case 'yml':
100+
$class = 'Symfony\\Components\\DependencyInjection\\Loader\\YamlFileLoader';
101+
break;
102+
case 'ini':
103+
$class = 'Symfony\\Components\\DependencyInjection\\Loader\\IniFileLoader';
104+
break;
105+
}
97106
}
98107

108+
$loader = null === $class ? $this : new $class($this->paths);
109+
99110
$importedFile = $this->getAbsolutePath((string) $import['resource'], dirname($file));
100111

101112
return $loader->load($importedFile);

src/Symfony/Components/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,27 @@ protected function parseImports(BuilderConfiguration $configuration, $content, $
8787

8888
protected function parseImport($import, $file)
8989
{
90+
$class = null;
< 10000 /td>
9091
if (isset($import['class']) && $import['class'] !== get_class($this))
9192
{
9293
$class = $import['class'];
93-
$loader = new $class($this->paths);
9494
}
9595
else
9696
{
97-
$loader = $this;
97+
// try to detect loader with the extension
98+
switch (pathinfo((string) $import['resource'], PATHINFO_EXTENSION))
99+
{
100+
case 'xml':
101+
$class = 'Symfony\\Components\\DependencyInjection\\Loader\\XmlFileLoader';
102+
break;
103+
case 'ini':
104+
$class = 'Symfony\\Components\\DependencyInjection\\Loader\\IniFileLoader';
105+
break;
106+
}
98107
}
99108

109+
$loader = null === $class ? $this : new $class($this->paths);
110+
100111
$importedFile = $this->getAbsolutePath($import['resource'], dirname($file));
101112

102113
return $loader->load($importedFile);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[parameters]
2+
imported_from_ini = true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://www.symfony-project.org/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">
6+
<parameters>
7+
<parameter key="imported_from_xml">true</parameter>
8+
</parameters>
9+
</container>

tests/fixtures/Symfony/Components/DependencyInjection/xml/services4.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
77
<import resource="services2.xml" />
88
<import resource="services3.xml" />
99
<import resource="../ini/parameters.ini" class="Symfony\Components\DependencyInjection\Loader\IniFileLoader" />
10+
<import resource="../ini/parameters2.ini" />
11+
<import resource="../yaml/services13.yml" />
1012
</imports>
1113
</container>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# used to test imports in XML
2+
parameters:
3+
imported_from_yaml: true

tests/fixtures/Symfony/Components/DependencyInjection/yaml/services4.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ imports:
22
- { resource: services2.yml }
33
- { resource: services3.yml }
44
- { resource: "../ini/parameters.ini", class: Symfony\Components\DependencyInjection\Loader\IniFileLoader }
5+
- { resource: "../ini/parameters2.ini" }
6+
- { resource: "../xml/services13.xml" }

tests/unit/Symfony/Components/DependencyInjection/Loader/XmlFileLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function parseFile($file)
8282
// ->load() # imports
8383
$t->diag('->load() # imports');
8484
$config = $loader->load('services4.xml');
85-
$t->is($config->getParameters(), array('a string', 'foo' => 'bar', 'bar' => '%foo%', 'values' => array(true, false), 'foo_bar' => new Reference('foo_bar')), '->load() imports and merges imported files');
85+
$t->is($config->getParameters(), array('a string', 'foo' => 'bar', 'bar' => '%foo%', 'values' => array(true, false), 'foo_bar' => new Reference('foo_bar'), 'imported_from_yaml' => true, 'imported_from_ini' => true), '->load() imports and merges imported files');
8686

8787
// ->load() # anonymous services
8888
$t->diag('->load() # anonymous services');

tests/unit/Symfony/Components/DependencyInjection/Loader/YamlFileLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function loadFile($file)
7979
// ->load() # imports
8080
$t->diag('->load() # imports');
8181
$config = $loader->load('services4.yml');
82-
$t->is($config->getParameters(), array('foo' => 'bar', 'bar' => '%foo%', 'values' => array(true, false), 'foo_bar' => new Reference('foo_bar')), '->load() imports and merges imported files');
82+
$t->is($config->getParameters(), array('foo' => 'bar', 'bar' => '%foo%', 'values' => array(true, false), 'foo_bar' => new Reference('foo_bar'), 'imported_from_xml' => true, 'imported_from_ini' => true), '->load() imports and merges imported files');
8383

8484
// ->load() # services
8585
$t->diag('->load() # services');

0 commit comments

Comments
 (0)
0