Skip to content

Commit b25451c

Browse files
committed
feature #20611 [DI] FileLoaders: Allow to explicit type to load (ogizanagi)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] FileLoaders: Allow to explicit type to load | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony/symfony#20308 | License | MIT | Doc PR | Not yet (fabbot will scream out regarding the PR fixtures) Commits ------- 6b660c2114 [DI] FileLoaders: Allow to explicit type to load
2 parents c1cf3e9 + 5946ef9 commit b25451c

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
@@ -65,7 +65,15 @@ public function load($resource, $type = null)
6565
*/
6666
public function supports($resource, $type = null)
6767
{
68-
return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION);
68+
if (!is_string($resource)) {
69+
return false;
70+
}
71+
72+
if (null === $type && 'xml' === pathinfo($resource, PATHINFO_EXTENSION)) {
73+
return true;
74+
}
75+
76+
return 'xml' === $type;
6977
}
7078

7179
/**
@@ -98,7 +106,7 @@ private function parseImports(\DOMDocument $xml, $file)
98106
$defaultDirectory = dirname($file);
99107
foreach ($imports as $import) {
100108
$this->setCurrentDir($defaultDirectory);
101-
$this->import($import->getAttribute('resource'), null, (bool) XmlUtils::phpize($import->getAttribute('ignore-errors')), $file);
109+
$this->import($import->getAttribute('resource'), XmlUtils::phpize($import->getAttribute('type')) ?: null, (bool) XmlUtils::phpize($import->getAttribute('ignore-errors')), $file);
102110
}
103111
}
104112

Loader/YamlFileLoader.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,15 @@ public function load($resource, $type = null)
104104
*/
105105
public function supports($resource, $type = null)
106106
{
107-
return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true);
107+
if (!is_string($resource)) {
108+
return false;
109+
}
110+
111+
if (null === $type && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yaml', 'yml'), true)) {
112+
return true;
113+
}
114+
115+
return in_array($type, array('yaml', 'yml'), true);
108116
}
109117

110118
/**
@@ -130,7 +138,7 @@ private function parseImports($content, $file)
130138
}
131139

132140
$this->setCurrentDir($defaultDirectory);
133-
$this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
141+
$this->import($import['resource'], isset($import['type']) ? $import['type'] : null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
134142
}
135143
}
136144

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

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

8283
<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)