Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b9e51c

Browse files
[Config] Allow empty globs
1 parent adc39a2 commit 6b9e51c

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,18 @@ public function getLocator()
8383
*/
8484
public function import($resource, $type = null, $ignoreErrors = false, $sourceResource = null)
8585
{
86-
if (is_string($resource) && false !== strpbrk($resource, '*?{[')) {
86+
if (is_string($resource) && strlen($resource) !== $i = strcspn($resource, '*?{[')) {
8787
$ret = array();
88-
foreach ($this->glob($resource, false, $_, true) as $path => $info) {
89-
$ret[] = $this->doImport($path, $type, $ignoreErrors, $sourceResource);
88+
$isSubpath = 0 !== $i && false !== strpos(substr($resource, 0, $i), '/');
89+
foreach ($this->glob($resource, false, $tracker, $ignoreErrors || !$isSubpath) as $path => $info) {
90+
if (null !== $res = $this->doImport($path, $type, $ignoreErrors, $sourceResource)) {
91+
$ret[] = $res;
92+
}
93+
$isSubpath = true;
9094
}
91-
if ($ret) {
92-
return count($ret) > 1 ? $ret : $ret[0];
95+
96+
if ($isSubpath) {
97+
return isset($ret[1]) ? $ret : (isset($ret[0]) ? $ret[0] : null);
9398
}
9499
}
95100

@@ -104,7 +109,7 @@ protected function glob($pattern, $recursive, &$resource = null, $ignoreErrors =
104109
if (strlen($pattern) === $i = strcspn($pattern, '*?{[')) {
105110
$prefix = $pattern;
106111
$pattern = '';
107-
} elseif (0 === $i) {
112+
} elseif (0 === $i || false === strpos(substr($pattern, 0, $i), '/')) {
108113
$prefix = '.';
109114
$pattern = '/'.$pattern;
110115
} else {

src/Symfony/Component/Config/Tests/Loader/FileLoaderTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Config\Tests\Loader;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Config\FileLocator;
1516
use Symfony\Component\Config\Loader\FileLoader;
1617
use Symfony\Component\Config\Loader\LoaderResolver;
1718

@@ -74,6 +75,21 @@ public function testImportWithGlobLikeResource()
7475

7576
$this->assertSame('[foo]', $loader->import('[foo]'));
7677
}
78+
79+
public function testImportWithNoGlobMatch()
80+
{
81+
$locatorMock = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();
82+
$loader = new TestFileLoader($locatorMock);
83+
84+
$this->assertNull($loader->import('./*.abc'));
85+
}
86+
87+
public function testImportWithSimpleGlob()
88+
{
89+
$loader = new TestFileLoader(new FileLocator(__DIR__));
90+
91+
$this->assertSame(__FILE__, $loader->import('FileLoaderTest.*'));
92+
}
7793
}
7894

7995
class TestFileLoader extends FileLoader

0 commit comments

Comments
 (0)