Skip to content

Commit

Permalink
merged branch jakzal/finder-glob-support (PR #6531)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Commits
-------

29b9611 [Finder] Added support for GLOB patterns in the directories passed to the in() method.

Discussion
----------

[Finder] Added support for wildcard characters (GLOB patterns)

Added support for wildcard characters in the paths passed to the *in()* method. Each pattern has to resolve to at least one directory, otherwise exception is thrown (just like when path to an invalid directory is passed).

Example usage:

```php
$finder = new \Symfony\Component\Finder\Finder();
$files = $finder->files()
    ->name('validators.en.*')
    ->in(array(
        'src/Symfony/*/*/Resources/translations',
    ));

foreach ($files as $file) {
    var_dump($file->getRealPath());
}
```

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #5450
Todo: -
License of the code: MIT
Documentation PR: symfony/symfony-docs#2089
  • Loading branch information
fabpot committed Jan 3, 2013
2 parents 82ac2dd + 088ab86 commit 41ed834
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Symfony/Component/Finder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CHANGELOG

* added Finder::path() and Finder::notPath() methods
* added finder adapters to improve performance on specific platforms
* added support for wildcard characters (glob patterns) in the paths passed
to Finder::in()

2.1.0
-----
Expand Down
14 changes: 9 additions & 5 deletions src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,21 +598,25 @@ public function followLinks()
*
* @return Finder The current Finder instance
*
* @throws \InvalidArgumentException if one of the directory does not exist
* @throws \InvalidArgumentException if one of the directories does not exist
*
* @api
*/
public function in($dirs)
{
$dirs = (array) $dirs;
$resolvedDirs = array();

foreach ($dirs as $dir) {
if (!is_dir($dir)) {
foreach ((array) $dirs as $dir) {
if (is_dir($dir)) {
$resolvedDirs[] = $dir;
} elseif ($glob = glob($dir, GLOB_ONLYDIR)) {
$resolvedDirs = array_merge($resolvedDirs, $glob);
} else {
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
}
}

$this->dirs = array_merge($this->dirs, $dirs);
$this->dirs = array_merge($this->dirs, $resolvedDirs);

return $this;
}
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Finder/Tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,27 @@ public function testIn($adapter)
$this->assertIterator(array(self::$tmpDir.DIRECTORY_SEPARATOR.'test.php', __DIR__.DIRECTORY_SEPARATOR.'FinderTest.php'), $iterator);
}

/**
* @dataProvider getAdaptersTestData
*/
public function testInWithGlob($adapter)
{
$finder = $this->buildFinder($adapter);
$finder->in(array(__DIR__.'/Fixtures/*/B/C', __DIR__.'/Fixtures/*/*/B/C'))->getIterator();

$this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
}

/**
* @dataProvider getAdaptersTestData
* @expectedException \InvalidArgumentException
*/
public function testInWithNonDirectoryGlob($adapter)
{
$finder = $this->buildFinder($adapter);
$finder->in(__DIR__.'/Fixtures/A/a*');
}

/**
* @dataProvider getAdaptersTestData
*/
Expand Down

0 comments on commit 41ed834

Please sign in to comment.