8000 Use glob pattern to load config file · symfony/symfony@0e5d09d · GitHub
[go: up one dir, main page]

Skip to content

Commit 0e5d09d

Browse files
committed
Use glob pattern to load config file
1 parent 136a5ff commit 0e5d09d

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

src/Symfony/Component/Config/FileLocator.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ public function locate($name, $currentPath = null, $first = true)
4242
}
4343

4444
if ($this->isAbsolutePath($name)) {
45-
if (!file_exists($name)) {
45+
if (array() === $files = glob($name, GLOB_BRACE)) {
4646
throw new FileLocatorFileNotFoundException(sprintf('The file "%s" does not exist.', $name));
4747
}
4848

49-
return $name;
49+
if ($first) {
50+
return array_shift($files);
51+
}
52+
53+
return $files;
5054
}
5155

5256
$paths = $this->paths;
@@ -58,19 +62,28 @@ public function locate($name, $currentPath = null, $first = true)
5862
$paths = array_unique($paths);
5963
$filepaths = array();
6064

61-
foreach ($paths as $path) {
62-
if (@file_exists($file = $path.DIRECTORY_SEPARATOR.$name)) {
63-
if (true === $first) {
64-
return $file;
65-
}
65+
$load = function ($file) use(&$filepaths) {
66+
if (@file_exists($file)) {
6667
$filepaths[] = $file;
6768
}
69+
};
70+
71+
foreach ($paths as $path) {
72+
if (array() !== $files = glob($path.DIRECTORY_SEPARATOR.$name)) {
73+
array_walk($files, $load);
74+
} else {
75+
$load($path.DIRECTORY_SEPARATOR.$name);
76+
}
6877
}
6978

7079
if (!$filepaths) {
7180
throw new FileLocatorFileNotFoundException(sprintf('The file "%s" does not exist (in: %s).', $name, implode(', ', $paths)));
7281
}
7382

83+
if (true === $first) {
84+
return array_shift($filepaths);
85+
}
86+
7487
return $filepaths;
7588
}
7689

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,15 @@ public function __construct(ContainerBuilder $container, FileLocatorInterface $l
3434

3535
parent::__construct($locator);
3636
}
37+
38+
protected function isAbsolutePath($file)
39+
{
40+
return strspn($file, '/\\', 0, 1)
41+
|| (strlen($file) > 3 && ctype_alpha($file[0])
42+
&& substr($file, 1, 1) === ':'
43+
&& strspn($file, '/\\', 2, 1)
44+
)
45+
|| null !== parse_url($file, PHP_URL_SCHEME)
46+
;
47+
}
3748
}

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,17 @@ private function parseImports(\DOMDocument $xml, $file)
106106
$defaultDirectory = dirname($file);
107107
foreach ($imports as $import) {
108108
$this->setCurrentDir($defaultDirectory);
109-
$this->import($import->getAttribute('resource'), XmlUtils::phpize($import->getAttribute('type')) ?: null, (bool) XmlUtils::phpize($import->getAttribute('ignore-errors')), $file);
109+
110+
$resourceFile = $import->getAttribute('resource');
111+
if (!$this->isAbsolutePath($resourceFile)) {
112+
$resourceFile = $defaultDirectory.DIRECTORY_SEPARATOR.$resourceFile;
113+
}
114+
115+
$files = glob($resourceFile, GLOB_BRACE);
116+
117+
foreach ($files as $resource) {
118+
$this->import($resource, XmlUtils::phpize($import->getAttribute('type')) ?: null, (bool) XmlUtils::phpize($import->getAttribute('ignore-errors')), $file);
119+
}
110120
}
111121
}
112122

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,16 @@ private function parseImports($content, $file)
138138
}
139139

140140
$this->setCurrentDir($defaultDirectory);
141-
$this->import($import['resource'], isset($import['type']) ? $import['type'] : null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
141+
142+
if (!$this->isAbsolutePath($import['resource'])) {
143+
$import['resource'] = $defaultDirectory.DIRECTORY_SEPARATOR.$import['resource'];
144+
}
145+
146+
$files = glob($import['resource'], GLOB_BRACE);
147+
148+
foreach ($files as $resource) {
149+
$this->import($resource, isset($import['type']) ? $import['type'] : null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
150+
}
142151
}
143152
}
144153

0 commit comments

Comments
 (0)
0