4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Iterator
;
15 * ExcludeDirectoryFilterIterator filters out directories.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class ExcludeDirectoryFilterIterator
extends FilterIterator
implements \RecursiveIterator
23 private $excludedDirs = array();
24 private $excludedPattern;
29 * @param \Iterator $iterator The Iterator to filter
30 * @param array $directories An array of directories to exclude
32 public function __construct(\Iterator
$iterator, array $directories)
34 $this->iterator
= $iterator;
35 $this->isRecursive
= $iterator instanceof \RecursiveIterator
;
37 foreach ($directories as $directory) {
38 if (!$this->isRecursive ||
false !== strpos($directory, '/')) {
39 $patterns[] = preg_quote($directory, '#');
41 $this->excludedDirs
[$directory] = true;
45 $this->excludedPattern
= '#(?:^|/)(?:'.implode('|', $patterns).')(?:/|$)#';
48 parent
::__construct($iterator);
52 * Filters the iterator values.
54 * @return bool true if the value should be kept, false otherwise
56 public function accept()
58 if ($this->isRecursive
&& isset($this->excludedDirs
[$this->getFilename()]) && $this->isDir()) {
62 if ($this->excludedPattern
) {
63 $path = $this->isDir() ?
$this->current()->getRelativePathname() : $this->current()->getRelativePath();
64 $path = str_replace('\\', '/', $path);
66 return !preg_match($this->excludedPattern
, $path);
72 public function hasChildren()
74 return $this->isRecursive
&& $this->iterator
->hasChildren();
77 public function getChildren()
79 $children = new self($this->iterator
->getChildren(), array());
80 $children->excludedDirs
= $this->excludedDirs
;
81 $children->excludedPattern
= $this->excludedPattern
;