Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f5cd0de

Browse filesBrowse files
committed
Use glob pattern to load config file
1 parent 136a5ff commit f5cd0de
Copy full SHA for f5cd0de

File tree

Expand file treeCollapse file tree

3 files changed

+146
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+146
-0
lines changed

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
* using the `PhpDumper` with an uncompiled `ContainerBuilder` is deprecated and
1515
will not be supported anymore in 4.0
1616
* deprecated the `DefinitionDecorator` class in favor of `ChildDefinition`
17+
* allow config files to be loaded using a glob pattern
1718

1819
3.2.0
1920
-----

‎src/Symfony/Component/DependencyInjection/Loader/FileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;
1616
use Symfony\Component\Config\FileLocatorInterface;
17+
use Symfony\Component\Config\Exception\FileLoaderLoadException;
18+
use Symfony\Component\Config\Resource\DirectoryResource;
19+
use Symfony\Component\Config\Resource\FileResource;
1720

1821
/**
1922
* FileLoader is the abstract class used by all built-in loaders that are file based.
@@ -24,6 +27,17 @@ abstract class FileLoader extends BaseFileLoader
2427
{
2528
protected $container;
2629

30+
private $currentDir;
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function setCurrentDir($dir)
36+
{
37+
$this->currentDir = $dir;
38+
parent::setCurrentDir($dir);
39+
}
40+
2741
/**
2842
* @param ContainerBuilder $container A ContainerBuilder instance
2943
* @param FileLocatorInterface $locator A FileLocator instance
@@ -34,4 +48,47 @@ public function __construct(ContainerBuilder $container, FileLocatorInterface $l
3448

3549
parent::__construct($locator);
3650
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
56+
{
57+
if (strlen($resource) === $i = strcspn($resource, '*?{[')) {
58+
$directoryPrefix = $resource;
59+
$directoryGlob = '';
60+
} else {
61+
$directoryPrefix = dirname(substr($resource, 0, 1 + $i));
62+
$directoryGlob = substr($resource, strlen($directoryPrefix));
63+
}
64+
65+
try {
66+
$directoryPrefix = $this->locator->locate($directoryPrefix, $this->currentDir, true);
67+
$directoryPrefix = realpath($directoryPrefix) ?: $directoryPrefix;
68+
$directoryGlob = $directoryPrefix.$directoryGlob;
69+
70+
if (!$files = glob($directoryGlob, defined('GLOB_BRACE') ? GLOB_BRACE : 0)) {
71+
throw new FileLoaderLoadException($directoryGlob, $sourceResource);
72+
}
73+
74+
foreach ($files as $file) {
75+
if (is_dir($file)) {
76+
$this->container->addResource(new DirectoryResource($file, '/^$/'));
77+
parent::import($file, 'directory', $ignoreErrors, $sourceResource);
78+
} else {
79+
$this->container->addResource(new FileResource($file));
80+
parent::import($file, $type, $ignoreErrors, $sourceResource);
81+
}
82+
}
83+
} catch (\Exception $e) {
84+
if (!$ignoreErrors) {
85+
// prevent embedded imports from nesting multiple exceptions
86+
if ($e instanceof FileLoaderLoadException) {
87+
throw $e;
88+
}
89+
90+
throw new FileLoaderLoadException($resource, $sourceResource);
91+
}
92+
}
93+
}
3794
}
+88Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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\FileLoader;
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+
use Symfony\Component\DependencyInjection\Reference;
23+
24+
class FileLoaderTest extends \PHPUnit_Framework_TestCase
25+
{
26+
protected static $fixturesPath;
27+
28+
public static function setUpBeforeClass()
29+
{
30+
self::$fixturesPath = realpath(__DIR__.'/../');
31+
}
32+
33+
public function testImportWithGlobPattern()
34+
{
35+
$container = new ContainerBuilder();
36+
$loader = new TestFileLoader($container, new FileLocator(self::$fixturesPath));
37+
38+
$resolver = new LoaderResolver(array(
39+
new IniFileLoader($container, new FileLocator(self::$fixturesPath.'/ini')),
40+
new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')),
41+
new PhpFileLoader($container, new FileLocator(self::$fixturesPath.'/php')),
42+
new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')),
43+
));
44+
45+
$loader->setResolver($resolver);
46+
$loader->import('Fixtures/{xml,yaml}/services2.{yml,xml}');
47+
48+
$actual = $container->getParameterBag()->all();
49+
$expected = array(
50+
'a string',
51+
'foo' => 'bar',
52+
'values' => array(
53+
0,
54+
'integer' => 4,
55+
100 => null,
56+
'true',
57+
true,
58+
false,
59+
'on',
60+
'off',
61+
'float' => 1.3,
62+
1000.3,
63+
'a string',
64+
array('foo', 'bar'),
65+
),
66+
'mixedcase' => array('MixedCaseKey' => 'value'),
67+
'constant' => PHP_EOL,
68+
'bar' => '%foo%',
69+
'escape' => '@escapeme',
70+
'foo_bar' => new Reference('foo_bar'),
71+
);
72+
73+
$this->assertEquals(array_keys($expected), array_keys($actual), '->load() imports and merges imported files');
74+
}
75+
}
76+
77+
class TestFileLoader extends FileLoader
78+
{
79+
public function load($resource, $type = null)
80+
{
81+
return $resource;
82+
}
83+
84+
public function supports($resource, $type = null)
85+
{
86+
return false;
87+
}
88+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.