Skip to content

[DI] FileLoaders: Allow to explicit type to load #20611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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 @@ -96,7 +104,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
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,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 @@ -127,7 +135,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
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[parameters]
with_wrong_ext = 'from ini'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$container->setParameter('with_wrong_ext', 'from php');
Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
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 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
with_wrong_ext: from yaml
Original file line number Diff line number Diff line change
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');
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,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 @@ -443,7 +444,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
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,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 @@ -208,7 +218,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