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 | https://github.com/symfony/symfony/issues/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
This commit is contained in:
commit
4d916c63dd
@ -52,7 +52,15 @@ class IniFileLoader extends FileLoader
|
||||
*/
|
||||
public function supports($resource, $type = null)
|
||||
{
|
||||
return is_string($resource) && 'ini' === pathinfo($resource, PATHINFO_EXTENSION);
|
||||
if (!is_string($resource)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null === $type && 'ini' === pathinfo($resource, PATHINFO_EXTENSION)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return 'ini' === $type;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -44,6 +44,14 @@ class PhpFileLoader extends FileLoader
|
||||
*/
|
||||
public function supports($resource, $type = null)
|
||||
{
|
||||
return is_string($resource) && 'php' === pathinfo($resource, PATHINFO_EXTENSION);
|
||||
if (!is_string($resource)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null === $type && 'php' === pathinfo($resource, PATHINFO_EXTENSION)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return 'php' === $type;
|
||||
}
|
||||
}
|
||||
|
@ -65,7 +65,15 @@ class XmlFileLoader extends FileLoader
|
||||
*/
|
||||
public function supports($resource, $type = null)
|
||||
{
|
||||
return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION);
|
||||
if (!is_string($resource)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null === $type && 'xml' === pathinfo($resource, PATHINFO_EXTENSION)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return 'xml' === $type;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,7 +106,7 @@ class XmlFileLoader extends FileLoader
|
||||
$defaultDirectory = dirname($file);
|
||||
foreach ($imports as $import) {
|
||||
$this->setCurrentDir($defaultDirectory);
|
||||
$this->import($import->getAttribute('resource'), null, (bool) XmlUtils::phpize($import->getAttribute('ignore-errors')), $file);
|
||||
$this->import($import->getAttribute('resource'), XmlUtils::phpize($import->getAttribute('type')) ?: null, (bool) XmlUtils::phpize($import->getAttribute('ignore-errors')), $file);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -104,7 +104,15 @@ class YamlFileLoader extends FileLoader
|
||||
*/
|
||||
public function supports($resource, $type = null)
|
||||
{
|
||||
return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true);
|
||||
if (!is_string($resource)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null === $type && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yaml', 'yml'), true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return in_array($type, array('yaml', 'yml'), true);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -130,7 +138,7 @@ class YamlFileLoader extends FileLoader
|
||||
}
|
||||
|
||||
$this->setCurrentDir($defaultDirectory);
|
||||
$this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
|
||||
$this->import($import['resource'], isset($import['type']) ? $import['type'] : null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,6 +77,7 @@
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="resource" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="ignore-errors" type="boolean" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="callable">
|
||||
|
@ -0,0 +1,2 @@
|
||||
[parameters]
|
||||
with_wrong_ext = 'from ini'
|
@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
$container->setParameter('with_wrong_ext', 'from php');
|
@ -9,5 +9,6 @@
|
||||
<import resource="../ini/parameters.ini" />
|
||||
<import resource="../ini/parameters2.ini" />
|
||||
<import resource="../yaml/services13.yml" />
|
||||
<import resource="../yaml/yaml_with_wrong_ext.ini" type="yaml"/>
|
||||
</imports>
|
||||
</container>
|
||||
|
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" ?>
|
||||
|
||||
<container xmlns="http://symfony.com/schema/dic/services"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
|
||||
<parameters>
|
||||
<parameter key="with_wrong_ext">from xml</parameter>
|
||||
</parameters>
|
||||
</container>
|
@ -5,3 +5,4 @@ imports:
|
||||
- { resource: "../ini/parameters.ini", class: Symfony\Component\DependencyInjection\Loader\IniFileLoader }
|
||||
- { resource: "../ini/parameters2.ini" }
|
||||
- { resource: "../xml/services13.xml" }
|
||||
- { resource: "../xml/xml_with_wrong_ext.php", type: xml }
|
||||
|
@ -0,0 +1,2 @@
|
||||
parameters:
|
||||
with_wrong_ext: from yaml
|
@ -126,6 +126,7 @@ class IniFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$loader = new IniFileLoader(new ContainerBuilder(), new FileLocator());
|
||||
|
||||
$this->assertTrue($loader->supports('foo.ini'), '->supports() returns true if the resource is loadable');
|
||||
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
|
||||
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
|
||||
$this->assertTrue($loader->supports('with_wrong_ext.yml', 'ini'), '->supports() returns true if the resource with forced type is loadable');
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Component\DependencyInjection\Tests\Loader;
|
||||
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\Config\Loader\LoaderResolver;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
|
||||
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
|
||||
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
|
||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
||||
|
||||
class LoaderResolverTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
private static $fixturesPath;
|
||||
|
||||
/** @var LoaderResolver */
|
||||
private $resolver;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
|
||||
|
||||
$container = new ContainerBuilder();
|
||||
$this->resolver = new LoaderResolver(array(
|
||||
new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
|
||||
new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
|
||||
new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/ini')),
|
||||
new PhpFileLoader($container, new FileLocator(self::$fixturesPath.'/php')),
|
||||
new ClosureLoader($container),
|
||||
));
|
||||
}
|
||||
|
||||
public function provideResourcesToLoad()
|
||||
{
|
||||
return array(
|
||||
array('ini_with_wrong_ext.xml', 'ini', IniFileLoader::class),
|
||||
array('xml_with_wrong_ext.php', 'xml', XmlFileLoader::class),
|
||||
array('php_with_wrong_ext.yml', 'php', PhpFileLoader::class),
|
||||
array('yaml_with_wrong_ext.ini', 'yaml', YamlFileLoader::class),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideResourcesToLoad
|
||||
*/
|
||||
public function testResolvesForcedType($resource, $type, $expectedClass)
|
||||
{
|
||||
$this->assertInstanceOf($expectedClass, $this->resolver->resolve($resource, $type));
|
||||
}
|
||||
}
|
@ -22,7 +22,8 @@ class PhpFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$loader = new PhpFileLoader(new ContainerBuilder(), new FileLocator());
|
||||
|
||||
$this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
|
||||
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
|
||||
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
|
||||
$this->assertTrue($loader->supports('with_wrong_ext.yml', 'php'), '->supports() returns true if the resource with forced type is loadable');
|
||||
}
|
||||
|
||||
public function testLoad()
|
||||
|
@ -163,6 +163,7 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
'bar' => '%foo%',
|
||||
'imported_from_ini' => true,
|
||||
'imported_from_yaml' => true,
|
||||
'with_wrong_ext' => 'from yaml',
|
||||
);
|
||||
|
||||
$this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
|
||||
@ -456,7 +457,8 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$loader = new XmlFileLoader(new ContainerBuilder(), new FileLocator());
|
||||
|
||||
$this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
|
||||
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
|
||||
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
|
||||
$this->assertTrue($loader->supports('with_wrong_ext.yml', 'xml'), '->supports() returns true if the resource with forced type is loadable');
|
||||
}
|
||||
|
||||
public function testNoNamingConflictsForAnonymousServices()
|
||||
|
@ -115,7 +115,17 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
$loader->load('services4.yml');
|
||||
|
||||
$actual = $container->getParameterBag()->all();
|
||||
$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);
|
||||
$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,
|
||||
'with_wrong_ext' => 'from yaml',
|
||||
);
|
||||
$this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
|
||||
$this->assertTrue($actual['imported_from_ini']);
|
||||
|
||||
@ -213,7 +223,9 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
|
||||
$this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');
|
||||
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
|
||||
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
|
||||
$this->assertTrue($loader->supports('with_wrong_ext.xml', 'yml'), '->supports() returns true if the resource with forced type is loadable');
|
||||
$this->assertTrue($loader->supports('with_wrong_ext.xml', 'yaml'), '->supports() returns true if the resource with forced type is loadable');
|
||||
}
|
||||
|
||||
public function testNonArrayTagsThrowsException()
|
||||
|
Reference in New Issue
Block a user