Skip to content

Commit

Permalink
feature #20611 [DI] FileLoaders: Allow to explicit type to load (ogiz…
Browse files Browse the repository at this point in the history
…anagi)

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
  • Loading branch information
fabpot committed Jan 10, 2017
2 parents 629de96 + 6b660c2 commit 4d916c6
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 11 deletions.
Expand Up @@ -52,7 +52,15 @@ public function load($resource, $type = null)
*/
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;
}

/**
Expand Down
Expand Up @@ -44,6 +44,14 @@ public function load($resource, $type = null)
*/
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;
}
}
12 changes: 10 additions & 2 deletions src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
Expand Up @@ -65,7 +65,15 @@ public function load($resource, $type = null)
*/
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;
}

/**
Expand Down Expand Up @@ -98,7 +106,7 @@ private function parseImports(\DOMDocument $xml, $file)
$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);
}
}

Expand Down
Expand Up @@ -104,7 +104,15 @@ public function load($resource, $type = null)
*/
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);
}

/**
Expand All @@ -130,7 +138,7 @@ private function parseImports($content, $file)
}

$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);
}
}

Expand Down
Expand Up @@ -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">
Expand Down
@@ -0,0 +1,2 @@
[parameters]
with_wrong_ext = 'from ini'
@@ -0,0 +1,3 @@
<?php

$container->setParameter('with_wrong_ext', 'from php');
Expand Up @@ -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>
Expand Up @@ -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
Expand Up @@ -126,6 +126,7 @@ public function testSupports()
$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));
}
}
Expand Up @@ -22,7 +22,8 @@ public function testSupports()
$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()
Expand Down
Expand Up @@ -163,6 +163,7 @@ public function testLoadImports()
'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');
Expand Down Expand Up @@ -456,7 +457,8 @@ public function testSupports()
$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()
Expand Down
Expand Up @@ -115,7 +115,17 @@ public function testLoadImports()
$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']);

Expand Down Expand Up @@ -213,7 +223,9 @@ public function testSupports()

$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()
Expand Down

0 comments on commit 4d916c6

Please sign in to comment.