3f5a5dfeb133cde33d2aa3fe9bc7c9083ae4eb82
[yaffs-website] / vendor / symfony / finder / Iterator / ExcludeDirectoryFilterIterator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Finder\Iterator;
13
14 /**
15  * ExcludeDirectoryFilterIterator filters out directories.
16  *
17  * @author Fabien Potencier <fabien@symfony.com>
18  */
19 class ExcludeDirectoryFilterIterator extends FilterIterator implements \RecursiveIterator
20 {
21     private $iterator;
22     private $isRecursive;
23     private $excludedDirs = array();
24     private $excludedPattern;
25
26     /**
27      * Constructor.
28      *
29      * @param \Iterator $iterator    The Iterator to filter
30      * @param array     $directories An array of directories to exclude
31      */
32     public function __construct(\Iterator $iterator, array $directories)
33     {
34         $this->iterator = $iterator;
35         $this->isRecursive = $iterator instanceof \RecursiveIterator;
36         $patterns = array();
37         foreach ($directories as $directory) {
38             $directory = rtrim($directory, '/');
39             if (!$this->isRecursive || false !== strpos($directory, '/')) {
40                 $patterns[] = preg_quote($directory, '#');
41             } else {
42                 $this->excludedDirs[$directory] = true;
43             }
44         }
45         if ($patterns) {
46             $this->excludedPattern = '#(?:^|/)(?:'.implode('|', $patterns).')(?:/|$)#';
47         }
48
49         parent::__construct($iterator);
50     }
51
52     /**
53      * Filters the iterator values.
54      *
55      * @return bool True if the value should be kept, false otherwise
56      */
57     public function accept()
58     {
59         if ($this->isRecursive && isset($this->excludedDirs[$this->getFilename()]) && $this->isDir()) {
60             return false;
61         }
62
63         if ($this->excludedPattern) {
64             $path = $this->isDir() ? $this->current()->getRelativePathname() : $this->current()->getRelativePath();
65             $path = str_replace('\\', '/', $path);
66
67             return !preg_match($this->excludedPattern, $path);
68         }
69
70         return true;
71     }
72
73     public function hasChildren()
74     {
75         return $this->isRecursive && $this->iterator->hasChildren();
76     }
77
78     public function getChildren()
79     {
80         $children = new self($this->iterator->getChildren(), array());
81         $children->excludedDirs = $this->excludedDirs;
82         $children->excludedPattern = $this->excludedPattern;
83
84         return $children;
85     }
86 }