1 | <?php |
||
17 | class FileCollection implements \IteratorAggregate, \Countable, \ArrayAccess |
||
18 | { |
||
19 | protected $basepath; |
||
20 | protected $files = array(); |
||
21 | |||
22 | 24 | public function __construct($basepath = null) |
|
23 | { |
||
24 | 24 | if (null !== $basepath) { |
|
25 | 17 | $this->setBasepath($basepath); |
|
26 | 17 | } |
|
27 | 24 | } |
|
28 | |||
29 | 17 | public function setBasepath($basepath) |
|
35 | |||
36 | 1 | public function getBasepath() |
|
40 | |||
41 | /** |
||
42 | * @param $pattern |
||
43 | * @param bool $force assumes the $pattern is a absolute file and adds it, even if it doesn't exist |
||
44 | * |
||
45 | * @throws \LogicException |
||
46 | * |
||
47 | * @return $this |
||
48 | */ |
||
49 | 16 | public function add($pattern, $force = false) |
|
50 | { |
||
51 | 16 | if (null === $this->basepath) { |
|
52 | throw new \LogicException('You need to call "setBasepath()" first before adding files'); |
||
53 | } |
||
54 | |||
55 | 16 | $filepath = $this->basepath . '/' . $pattern; |
|
56 | 16 | if ($force || is_file($filepath)) { |
|
57 | 11 | $this->files[] = $pattern; |
|
58 | |||
59 | 11 | return $this; |
|
60 | } |
||
61 | |||
62 | 16 | $regex = Glob::toRegex($pattern, false); |
|
63 | 16 | if (is_dir($filepath) || '*' === $pattern) { |
|
64 | 16 | $regex = str_replace('$', '', $regex); |
|
65 | 16 | } |
|
66 | |||
67 | 16 | $finder = new Finder(); |
|
68 | $finder |
||
69 | 16 | ->files() |
|
70 | 16 | ->ignoreDotFiles(false) |
|
71 | 16 | ->in($this->basepath) |
|
72 | 16 | ->path($regex) |
|
73 | ; |
||
74 | |||
75 | 16 | $this->files = array_merge($this->files, $this->mapFinder($finder)); |
|
76 | 16 | $this->files = array_unique($this->files); |
|
77 | |||
78 | 16 | return $this; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param Finder $finder |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | 16 | protected function mapFinder($finder) |
|
92 | |||
93 | 3 | public function remove($pattern) |
|
94 | { |
||
95 | 3 | if ('*' === $pattern) { |
|
96 | 2 | $this->files = array(); |
|
97 | 2 | } else { |
|
98 | 1 | $matching = $this->match($pattern); |
|
99 | |||
100 | $this->files = array_filter($this->files, function ($file) use ($pattern, $matching) { |
||
101 | 1 | return false === in_array($file, $matching); |
|
102 | 1 | }); |
|
103 | |||
104 | 1 | $this->files = array_values($this->files); |
|
105 | } |
||
106 | |||
107 | 3 | return $this; |
|
108 | } |
||
109 | |||
110 | public function merge(FileCollection $collection) |
||
114 | |||
115 | public function toArray() |
||
119 | |||
120 | /** |
||
121 | * @param FileCollection $array |
||
122 | */ |
||
123 | public function intersect($array) |
||
124 | { |
||
125 | if ($array instanceof self) { |
||
126 | $array = $array->toArray(); |
||
|
|||
127 | } |
||
128 | |||
129 | $this->files = array_intersect($array, $this->files); |
||
130 | } |
||
131 | |||
132 | 2 | public function match($pattern) |
|
147 | |||
148 | 10 | public function has($pattern) |
|
149 | { |
||
150 | 10 | if ('*' !== substr($pattern, -1)) { |
|
151 | 9 | if ('/' === substr($pattern, -1, 1)) { |
|
152 | 2 | $pattern .= '*'; |
|
153 | 2 | } |
|
154 | 9 | } |
|
155 | |||
156 | 10 | $regex = Glob::toRegex($pattern, false, false); |
|
157 | |||
158 | 10 | foreach ($this->files as $file) { |
|
159 | 10 | if (1 === preg_match($regex, $file)) { |
|
160 | 7 | return true; |
|
161 | } |
||
162 | 9 | } |
|
163 | |||
164 | 4 | return false; |
|
165 | } |
||
166 | |||
167 | 4 | public function getIterator() |
|
171 | |||
172 | 1 | public function count() |
|
176 | |||
177 | public function offsetExists($offset) |
||
181 | |||
182 | public function offsetGet($offset) |
||
186 | |||
187 | 1 | public function offsetSet($offset, $value) |
|
188 | 1 | { |
|
189 | 1 | $this->add($value); |
|
190 | 1 | } |
|
191 | |||
192 | public function offsetUnset($offset) |
||
196 | } |
||
197 |