Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commited9f714

Browse files
committed
Use glob pattern to load config file
1 parent136a5ff commited9f714

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

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

Lines 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‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
useSymfony\Component\DependencyInjection\ContainerBuilder;
1515
useSymfony\Component\Config\Loader\FileLoaderasBaseFileLoader;
1616
useSymfony\Component\Config\FileLocatorInterface;
17+
useSymfony\Component\Config\Exception\FileLoaderLoadException;
18+
useSymfony\Component\Config\Exception\FileLocatorFileNotFoundException;
19+
useSymfony\Component\Config\Resource\DirectoryResource;
20+
useSymfony\Component\Config\Resource\FileResource;
1721

1822
/**
1923
* FileLoader is the abstract class used by all built-in loaders that are file based.
@@ -24,6 +28,17 @@ abstract class FileLoader extends BaseFileLoader
2428
{
2529
protected$container;
2630

31+
private$currentDir;
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
publicfunctionsetCurrentDir($dir)
37+
{
38+
$this->currentDir =$dir;
39+
parent::setCurrentDir($dir);
40+
}
41+
2742
/**
2843
* @param ContainerBuilder $container A ContainerBuilder instance
2944
* @param FileLocatorInterface $locator A FileLocator instance
@@ -34,4 +49,48 @@ public function __construct(ContainerBuilder $container, FileLocatorInterface $l
3449

3550
parent::__construct($locator);
3651
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
publicfunctionimport($resource,$type =null,$ignoreErrors =false,$sourceResource =null)
57+
{
58+
if (strlen($resource) ===$i =strcspn($resource,'*?{[')) {
59+
$directoryPrefix =$resource;
60+
$directoryGlob ='';
61+
}else {
62+
$directoryPrefix =dirname(substr($resource,0,1 +$i));
63+
$directoryGlob =substr($resource,strlen($directoryPrefix));
64+
}
65+
66+
try {
67+
$directoryPrefix =$this->locator->locate($directoryPrefix,$this->currentDir,true);
68+
69+
$directoryPrefix =realpath($directoryPrefix) ?:$directoryPrefix;
70+
$directoryGlob =$directoryPrefix.$directoryGlob;
71+
}catch (FileLocatorFileNotFoundException$e) {
72+
if (!$ignoreErrors) {
73+
// prevent embedded imports from nesting multiple exceptions
74+
if ($einstanceof FileLoaderLoadException) {
75+
throw$e;
76+
}
77+
78+
thrownewFileLoaderLoadException($resource,$sourceResource);
79+
}
80+
}
81+
82+
if (!$files =glob($directoryGlob,defined('GLOB_BRACE') ?GLOB_BRACE :0)) {
83+
thrownewFileLoaderLoadException($directoryGlob,$sourceResource);
84+
}
85+
86+
foreach ($filesas$file) {
87+
if (is_dir($file)) {
88+
$this->container->addResource(newDirectoryResource($file,'/^$/'));
89+
parent::import($file,'directory',$ignoreErrors,$sourceResource);
90+
}else {
91+
$this->container->addResource(newFileResource($file));
92+
parent::import($file,$type,$ignoreErrors,$sourceResource);
93+
}
94+
}
95+
}
3796
}
Lines 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+
namespaceSymfony\Component\DependencyInjection\Tests\Loader;
13+
14+
useSymfony\Component\Config\FileLocator;
15+
useSymfony\Component\Config\Loader\LoaderResolver;
16+
useSymfony\Component\DependencyInjection\ContainerBuilder;
17+
useSymfony\Component\DependencyInjection\Loader\FileLoader;
18+
useSymfony\Component\DependencyInjection\Loader\IniFileLoader;
19+
useSymfony\Component\DependencyInjection\Loader\PhpFileLoader;
20+
useSymfony\Component\DependencyInjection\Loader\XmlFileLoader;
21+
useSymfony\Component\DependencyInjection\Loader\YamlFileLoader;
22+
useSymfony\Component\DependencyInjection\Reference;
23+
24+
class FileLoaderTestextends \PHPUnit_Framework_TestCase
25+
{
26+
protectedstatic$fixturesPath;
27+
28+
publicstaticfunctionsetUpBeforeClass()
29+
{
30+
self::$fixturesPath =realpath(__DIR__.'/../');
31+
}
32+
33+
publicfunctiontestImportWithGlobPattern()
34+
{
35+
$container =newContainerBuilder();
36+
$loader =newTestFileLoader($container,newFileLocator(self::$fixturesPath));
37+
38+
$resolver =newLoaderResolver(array(
39+
newIniFileLoader($container,newFileLocator(self::$fixturesPath.'/ini')),
40+
newXmlFileLoader($container,newFileLocator(self::$fixturesPath.'/xml')),
41+
newPhpFileLoader($container,newFileLocator(self::$fixturesPath.'/php')),
42+
newYamlFileLoader($container,newFileLocator(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' =>newReference('foo_bar'),
71+
);
72+
73+
$this->assertEquals(array_keys($expected),array_keys($actual),'->load() imports and merges imported files');
74+
}
75+
}
76+
77+
class TestFileLoaderextends FileLoader
78+
{
79+
publicfunctionload($resource,$type =null)
80+
{
81+
return$resource;
82+
}
83+
84+
publicfunctionsupports($resource,$type =null)
85+
{
86+
returnfalse;
87+
}
88+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp