Skip to content

Commit 5946ef9

Browse files
committed
[DI] FileLoaders: Allow to explicit type to load
1 parent 7fff820 commit 5946ef9

16 files changed

+139
-11
lines changed

Loader/IniFileLoader.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ public function load($resource, $type = null)
5252
*/
5353
public function supports($resource, $type = null)
5454
{
55-
return is_string($resource) && 'ini' === pathinfo($resource, PATHINFO_EXTENSION);
55+
if (!is_string($resource)) {
56+
return false;
57+
}
58+
59+
if (null === $type && 'ini' === pathinfo($resource, PATHINFO_EXTENSION)) {
60+
return true;
61+
}
62+
63+
return 'ini' === $type;
5664
}
5765

5866
/**

Loader/PhpFileLoader.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ public function load($resource, $type = null)
4444
*/
4545
public function supports($resource, $type = null)
4646
{
47-
return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION);
47+
if (!is_string($resource)) {
48+
return false;
49+
}
50+
51+
if (null === $type && 'php' === pathinfo($resource, PATHINFO_EXTENSION)) {
52+
return true;
53+
}
54+
55+
return 'php' === $type;
4856
}
4957
}

Loader/XmlFileLoader.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ public function load($resource, $type = null)
6363
*/
6464
public function supports($resource, $type = null)
6565
{
66-
return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION);
66+
if (!is_string($resource)) {
67+
return false;
68+
}
69+
70+
if (null === $type && 'xml' === pathinfo($resource, PATHINFO_EXTENSION)) {
71+
return true;
72+
}
73+
74+
return 'xml' === $type;
6775
}
6876

6977
/**
@@ -96,7 +104,7 @@ private function parseImports(\DOMDocument $xml, $file)
96104
$defaultDirectory = dirname($file);
97105
foreach ($imports as $import) {
98106
$this->setCurrentDir($defaultDirectory);
99-
$this->import($import->getAttribute('resource'), null, (bool) XmlUtils::phpize($import->getAttribute('ignore-errors')), $file);
107+
$this->import($import->getAttribute('resource'), XmlUtils::phpize($import->getAttribute('type')) ?: null, (bool) XmlUtils::phpize($import->getAttribute('ignore-errors')), $file);
100108
}
101109
}
102110

Loader/YamlFileLoader.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,15 @@ public function load($resource, $type = null)
101101
*/
102102
public function supports($resource, $type = null)
103103
{
104-
return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true);
104+
if (!is_string($resource)) {
105+
return false;
106+
}
107+
108+
if (null === $type && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yaml', 'yml'), true)) {
109+
return true;
110+
}
111+
112+
return in_array($type, array('yaml', 'yml'), true);
105113
}
106114

107115
/**
@@ -127,7 +135,7 @@ private function parseImports($content, $file)
127135
}
128136

129137
$this->setCurrentDir($defaultDirectory);
130-
$this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
138+
$this->import($import['resource'], isset($import['type']) ? $import['type'] : null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
131139
}
132140
}
133141

Loader/schema/dic/services/services-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
</xsd:annotation>
7777
<xsd:attribute name="resource" type="xsd:string" use="required" />
7878
<xsd:attribute name="ignore-errors" type="boolean" />
79+
<xsd:attribute name="type" type="xsd:string" />
7980
</xsd:complexType>
8081

8182
<xsd:complexType name="callable">
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'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$container->setParameter('with_wrong_ext', 'from php');

Tests/Fixtures/xml/services4.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
<import resource="../ini/parameters.ini" />
1010
<import resource="../ini/parameters2.ini" />
1111
<import resource="../yaml/services13.yml" />
12+
<import resource="../yaml/yaml_with_wrong_ext.ini" type="yaml"/>
1213
</imports>
1314
</container>
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://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
<parameters>
7+
<parameter key="with_wrong_ext">from xml</parameter>
8+
</parameters>
9+
</container>

Tests/Fixtures/yaml/services4.yml

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

0 commit comments

Comments
 (0)