8000 feature #20611 [DI] FileLoaders: Allow to explicit type to load (ogiz… · symfony/symfony@4d916c6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d916c6

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 | #20308 | License | MIT | Doc PR | Not yet (fabbot will scream out regarding the PR fixtures) Commits ------- 6b660c2 [DI] FileLoaders: Allow to explicit type to load
2 parents 629de96 + 6b660c2 commit 4d916c6

16 files changed

+139
-11
lines changed

src/Symfony/Component/DependencyInjection/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
/**

src/Symfony/Component/DependencyInjection/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
}

src/Symfony/Component/DependencyInjection/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

src/Symfony/Component/DependencyInjection/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

src/Symfony/Component/DependencyInjection/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');

src/Symfony/Component/DependencyInjection/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>

src/Symfony/Component/DependencyInjection/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 }
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 yaml

src/Symfony/Component/DependencyInjection/Tests/Loader/IniFileLoaderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public function testSupports()
126126
$loader = new IniFileLoader(new ContainerBuilder(), new FileLocator());
127127

128128
$this->assertTrue($loader->supports('foo.ini'), '->supports() returns true if the resource is loadable');
129-
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
129+
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
130+
$this->assertTrue($loader->supports('with_wrong_ext.yml', 'ini'), '->supports() returns true if the resource with forced type is loadable');
130131
}
131132
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Loader;
13+
14+
use Symfony\Component\Config\FileLocator;
15+
use Symfony\Component\Config\Loader\LoaderResolver;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
18+
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
19+
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
20+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
21+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
22+
23+
class LoaderResolverTest extends \PHPUnit_Framework_TestCase
24+
{
25+
private static $fixturesPath;
26+
27+
/** @var LoaderResolver */
28+
private $resolver;
29+
30+
protected function setUp()
31+
{
32+
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
33+
34+
$container = new ContainerBuilder();
35+
$this->resolver = new LoaderResolver(array(
36+
new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
37+
new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
38+
new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/ini')),
39+
new PhpFileLoader($container, new FileLocator(self::$fixturesPath.'/php')),
40+
new ClosureLoader($container),
41+
));
42+
}
43+
44+
public function provideResourcesToLoad()
45+
{
46+
return array(
47+
array('ini_with_wrong_ext.xml', 'ini', IniFileLoader::class),
48+
array('xml_with_wrong_ext.php', 'xml', XmlFileLoader::class),
49+
array('php_with_wrong_ext.yml', 'php', PhpFileLoader::class),
50+
array('yaml_with_wrong_ext.ini', 'yaml', YamlFileLoader::class),
51+
);
52+
}
53+
54+
/**
55+
* @dataProvider provideResourcesToLoad
56+
*/
57+
public function testResolvesForcedType($resource, $type, $expectedClass)
58+
{
59+
$this->assertInstanceOf($expectedClass, $this->resolver->resolve($resource, $type));
60+
}
61+
}

src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public function testSupports()
2222
$loader = new PhpFileLoader(new ContainerBuilder(), new FileLocator());
2323

2424
$this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
25-
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
25+
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
26+
$this->assertTrue($loader->supports('with_wrong_ext.yml', 'php'), '->supports() returns true if the resource with forced type is loadable');
2627
}
2728

2829
public function testLoad()

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ public function testLoadImports()
163163
'bar' => '%foo%',
164164
'imported_from_ini' => true,
165165
'imported_from_yaml' => true,
166+
'with_wrong_ext' => 'from yaml',
166167
);
167168

168169
$this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
@@ -456,7 +457,8 @@ public function testSupports()
456457
$loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator());
457458

458459
$this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
459-
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
460+
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
461+
$this->assertTrue($loader->supports('with_wrong_ext.yml', 'xml'), '->supports() returns true if the resource with forced type is loadable');
460462
}
461463

462464
public function testNoNamingConflictsForAnonymousServices()

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,17 @@ public function testLoadImports()
115115
$loader->load('services4.yml');
116116

117117
$actual = $container->getParameterBag()->all();
118-
$expected = array('foo' => 'bar', 'values' => array(true, false, PHP_INT_MAX), 'bar' => '%foo%', 'escape' => '@escapeme', 'foo_bar' => new Reference('foo_bar'), 'mixedcase' => array('MixedCaseKey' => 'value'), 'imported_from_ini' => true, 'imported_from_xml' => true);
118+
$expected = array(
119+
'foo' => 'bar',
120+
'values' => array(true, false, PHP_INT_MAX),
121+
'bar' => '%foo%',
122+
'escape' => '@escapeme',
123+
'foo_bar' => new Reference('foo_bar'),
124+
'mixedcase' => array('MixedCaseKey' => 'value'),
125+
'imported_from_ini' => true,
126+
'imported_from_xml' => true,
127+
'with_wrong_ext' => 'from yaml',
128+
);
119129
$this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
120130
$this->assertTrue($actual['imported_from_ini']);
121131

@@ -213,7 +223,9 @@ public function testSupports()
213223

214224
$this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
215225
$this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');
216-
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
226+
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
227+
$this->assertTrue($loader->supports('with_wrong_ext.xml', 'yml'), '->supports() returns true if the resource with forced type is loadable');
228+
$this->assertTrue($loader->supports('with_wrong_ext.xml', 'yaml'), '->supports() returns true if the resource with forced type is loadable');
217229
}
218230

219231
public function testNonArrayTagsThrowsException()

0 commit comments

Comments
 (0)
0