Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[Finder] Added support for GLOB patterns in the directories passed to…

… the in() method.

Pattern has to resolve to at least one directory, otherwise exception is thrown (just like when path to an invalid directory is passed).
  • Loading branch information...
commit b43123d66d1426b9699abf25a9d581e4c5f5f158 1 parent bd672f5
Jakub Zalas jakzal authored

Showing 3 changed files with 32 additions and 5 deletions. Show diff stats Hide diff stats

  1. +2 0  CHANGELOG.md
  2. +9 5 Finder.php
  3. +21 0 Tests/FinderTest.php
2  CHANGELOG.md
Source Rendered
@@ -6,6 +6,8 @@ CHANGELOG
6 6
7 7 * added Finder::path() and Finder::notPath() methods
8 8 * added finder adapters to improve performance on specific platforms
  9 + * added support for wildcard characters (glob patterns) in the paths passed
  10 + to Finder::in()
9 11
10 12 2.1.0
11 13 -----
14 Finder.php
@@ -598,21 +598,25 @@ public function followLinks()
598 598 *
599 599 * @return Finder The current Finder instance
600 600 *
601   - * @throws \InvalidArgumentException if one of the directory does not exist
  601 + * @throws \InvalidArgumentException if one of the directories does not exist
602 602 *
603 603 * @api
604 604 */
605 605 public function in($dirs)
606 606 {
607   - $dirs = (array) $dirs;
  607 + $resolvedDirs = array();
608 608
609   - foreach ($dirs as $dir) {
610   - if (!is_dir($dir)) {
  609 + foreach ((array) $dirs as $dir) {
  610 + if (is_dir($dir)) {
  611 + $resolvedDirs[] = $dir;
  612 + } elseif ($glob = glob($dir, GLOB_ONLYDIR)) {
  613 + $resolvedDirs = array_merge($resolvedDirs, $glob);
  614 + } else {
611 615 throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
612 616 }
613 617 }
614 618
615   - $this->dirs = array_merge($this->dirs, $dirs);
  619 + $this->dirs = array_merge($this->dirs, $resolvedDirs);
616 620
617 621 return $this;
618 622 }
21 Tests/FinderTest.php
@@ -314,6 +314,27 @@ public function testIn($adapter)
314 314 /**
315 315 * @dataProvider getAdaptersTestData
316 316 */
  317 + public function testInWithGlob($adapter)
  318 + {
  319 + $finder = $this->buildFinder($adapter);
  320 + $finder->in(array(__DIR__.'/Fixtures/*/B/C', __DIR__.'/Fixtures/*/*/B/C'))->getIterator();
  321 +
  322 + $this->assertIterator($this->toAbsoluteFixtures(array('A/B/C/abc.dat', 'copy/A/B/C/abc.dat.copy')), $finder);
  323 + }
  324 +
  325 + /**
  326 + * @dataProvider getAdaptersTestData
  327 + * @expectedException \InvalidArgumentException
  328 + */
  329 + public function testInWithNonDirectoryGlob($adapter)
  330 + {
  331 + $finder = $this->buildFinder($adapter);
  332 + $finder->in(__DIR__.'/Fixtures/A/a*');
  333 + }
  334 +
  335 + /**
  336 + * @dataProvider getAdaptersTestData
  337 + */
317 338 public function testGetIterator($adapter)
318 339 {
319 340 $finder = $this->buildFinder($adapter);

0 comments on commit b43123d

Please sign in to comment.
Something went wrong with that request. Please try again.