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;
27 * @param \Iterator $iterator The Iterator to filter
28 * @param array $directories An array of directories to exclude
30 public function __construct(\Iterator $iterator, array $directories)
32 $this->iterator = $iterator;
33 $this->isRecursive = $iterator instanceof \RecursiveIterator;
35 foreach ($directories as $directory) {
36 $directory = rtrim($directory, '/');
37 if (!$this->isRecursive || false !== strpos($directory, '/')) {
38 $patterns[] = preg_quote($directory, '#');
40 $this->excludedDirs[$directory] = true;
44 $this->excludedPattern = '#(?:^|/)(?:'.implode('|', $patterns).')(?:/|$)#';
47 parent::__construct($iterator);
51 * Filters the iterator values.
53 * @return bool True if the value should be kept, false otherwise
55 public function accept()
57 if ($this->isRecursive && isset($this->excludedDirs[$this->getFilename()]) && $this->isDir()) {
61 if ($this->excludedPattern) {
62 $path = $this->isDir() ? $this->current()->getRelativePathname() : $this->current()->getRelativePath();
63 $path = str_replace('\\', '/', $path);
65 return !preg_match($this->excludedPattern, $path);
71 public function hasChildren()
73 return $this->isRecursive && $this->iterator->hasChildren();
76 public function getChildren()
78 $children = new self($this->iterator->getChildren(), array());
79 $children->excludedDirs = $this->excludedDirs;
80 $children->excludedPattern = $this->excludedPattern;