1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Utils;
9:
10: use Nette;
11: use RecursiveDirectoryIterator;
12: use RecursiveIteratorIterator;
13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24:
25: class Finder extends Nette\Object implements \IteratorAggregate, \Countable
26: {
27:
28: private $paths = array();
29:
30:
31: private $groups;
32:
33:
34: private $exclude = array();
35:
36:
37: private $order = RecursiveIteratorIterator::SELF_FIRST;
38:
39:
40: private $maxDepth = -1;
41:
42:
43: private $cursor;
44:
45:
46: 47: 48: 49: 50:
51: public static function find($mask)
52: {
53: if (!is_array($mask)) {
54: $mask = func_get_args();
55: }
56: $finder = new static;
57: return $finder->select($mask, 'isDir')->select($mask, 'isFile');
58: }
59:
60:
61: 62: 63: 64: 65:
66: public static function findFiles($mask)
67: {
68: if (!is_array($mask)) {
69: $mask = func_get_args();
70: }
71: $finder = new static;
72: return $finder->select($mask, 'isFile');
73: }
74:
75:
76: 77: 78: 79: 80:
81: public static function findDirectories($mask)
82: {
83: if (!is_array($mask)) {
84: $mask = func_get_args();
85: }
86: $finder = new static;
87: return $finder->select($mask, 'isDir');
88: }
89:
90:
91: 92: 93: 94: 95: 96:
97: private function select($masks, $type)
98: {
99: $this->cursor = & $this->groups[];
100: $pattern = self::buildPattern($masks);
101: if ($type || $pattern) {
102: $this->filter(function (RecursiveDirectoryIterator $file) use ($type, $pattern) {
103: return !$file->isDot()
104: && (!$type || $file->$type())
105: && (!$pattern || preg_match($pattern, '/' . strtr($file->getSubPathName(), '\\', '/')));
106: });
107: }
108: return $this;
109: }
110:
111:
112: 113: 114: 115: 116:
117: public function in($path)
118: {
119: if (!is_array($path)) {
120: $path = func_get_args();
121: }
122: $this->maxDepth = 0;
123: return $this->from($path);
124: }
125:
126:
127: 128: 129: 130: 131:
132: public function from($path)
133: {
134: if ($this->paths) {
135: throw new Nette\InvalidStateException('Directory to search has already been specified.');
136: }
137: if (!is_array($path)) {
138: $path = func_get_args();
139: }
140: $this->paths = $path;
141: $this->cursor = & $this->exclude;
142: return $this;
143: }
144:
145:
146: 147: 148: 149:
150: public function childFirst()
151: {
152: $this->order = RecursiveIteratorIterator::CHILD_FIRST;
153: return $this;
154: }
155:
156:
157: 158: 159: 160: 161:
162: private static function buildPattern($masks)
163: {
164: $pattern = array();
165: foreach ($masks as $mask) {
166: $mask = rtrim(strtr($mask, '\\', '/'), '/');
167: $prefix = '';
168: if ($mask === '') {
169: continue;
170:
171: } elseif ($mask === '*') {
172: return NULL;
173:
174: } elseif ($mask[0] === '/') {
175: $mask = ltrim($mask, '/');
176: $prefix = '(?<=^/)';
177: }
178: $pattern[] = $prefix . strtr(preg_quote($mask, '#'),
179: array('\*\*' => '.*', '\*' => '[^/]*', '\?' => '[^/]', '\[\!' => '[^', '\[' => '[', '\]' => ']', '\-' => '-'));
180: }
181: return $pattern ? '#/(' . implode('|', $pattern) . ')\z#i' : NULL;
182: }
183:
184:
185:
186:
187:
188: 189: 190: 191:
192: public function count()
193: {
194: return iterator_count($this->getIterator());
195: }
196:
197:
198: 199: 200: 201:
202: public function getIterator()
203: {
204: if (!$this->paths) {
205: throw new Nette\InvalidStateException('Call in() or from() to specify directory to search.');
206:
207: } elseif (count($this->paths) === 1) {
208: return $this->buildIterator($this->paths[0]);
209:
210: } else {
211: $iterator = new \AppendIterator();
212: $iterator->append($workaround = new \ArrayIterator(array('workaround PHP bugs #49104, #63077')));
213: foreach ($this->paths as $path) {
214: $iterator->append($this->buildIterator($path));
215: }
216: unset($workaround[0]);
217: return $iterator;
218: }
219: }
220:
221:
222: 223: 224: 225: 226:
227: private function buildIterator($path)
228: {
229: $iterator = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
230:
231: if ($this->exclude) {
232: $filters = $this->exclude;
233: $iterator = new RecursiveCallbackFilterIterator($iterator, function ($foo, $bar, RecursiveCallbackFilterIterator $iterator) use ($filters) {
234: $file = $iterator->getInnerIterator();
235: if (!$file->isDot() && !$file->isFile()) {
236: foreach ($filters as $filter) {
237: if (!call_user_func($filter, $file)) {
238: return FALSE;
239: }
240: }
241: }
242: return TRUE;
243: });
244: }
245:
246: if ($this->maxDepth !== 0) {
247: $iterator = new RecursiveIteratorIterator($iterator, $this->order);
248: $iterator->setMaxDepth($this->maxDepth);
249: }
250:
251: $groups = $this->groups;
252: $iterator = new CallbackFilterIterator($iterator, function ($foo, $bar, CallbackFilterIterator $file) use ($groups) {
253: do {
254: $file = $file->getInnerIterator();
255: } while (!$file instanceof RecursiveDirectoryIterator);
256:
257: foreach ($groups as $filters) {
258: foreach ($filters as $filter) {
259: if (!call_user_func($filter, $file)) {
260: continue 2;
261: }
262: }
263: return TRUE;
264: }
265: return FALSE;
266: });
267:
268: return $iterator;
269: }
270:
271:
272:
273:
274:
275: 276: 277: 278: 279: 280:
281: public function exclude($masks)
282: {
283: if (!is_array($masks)) {
284: $masks = func_get_args();
285: }
286: $pattern = self::buildPattern($masks);
287: if ($pattern) {
288: $this->filter(function (RecursiveDirectoryIterator $file) use ($pattern) {
289: return !preg_match($pattern, '/' . strtr($file->getSubPathName(), '\\', '/'));
290: });
291: }
292: return $this;
293: }
294:
295:
296: 297: 298: 299: 300:
301: public function filter($callback)
302: {
303: $this->cursor[] = $callback;
304: return $this;
305: }
306:
307:
308: 309: 310: 311: 312:
313: public function limitDepth($depth)
314: {
315: $this->maxDepth = $depth;
316: return $this;
317: }
318:
319:
320: 321: 322: 323: 324: 325:
326: public function size($operator, $size = NULL)
327: {
328: if (func_num_args() === 1) {
329: if (!preg_match('#^(?:([=<>!]=?|<>)\s*)?((?:\d*\.)?\d+)\s*(K|M|G|)B?\z#i', $operator, $matches)) {
330: throw new Nette\InvalidArgumentException('Invalid size predicate format.');
331: }
332: list(, $operator, $size, $unit) = $matches;
333: static $units = array('' => 1, 'k' => 1e3, 'm' => 1e6, 'g' => 1e9);
334: $size *= $units[strtolower($unit)];
335: $operator = $operator ? $operator : '=';
336: }
337: return $this->filter(function (RecursiveDirectoryIterator $file) use ($operator, $size) {
338: return Finder::compare($file->getSize(), $operator, $size);
339: });
340: }
341:
342:
343: 344: 345: 346: 347: 348:
349: public function date($operator, $date = NULL)
350: {
351: if (func_num_args() === 1) {
352: if (!preg_match('#^(?:([=<>!]=?|<>)\s*)?(.+)\z#i', $operator, $matches)) {
353: throw new Nette\InvalidArgumentException('Invalid date predicate format.');
354: }
355: list(, $operator, $date) = $matches;
356: $operator = $operator ? $operator : '=';
357: }
358: $date = DateTime::from($date)->format('U');
359: return $this->filter(function (RecursiveDirectoryIterator $file) use ($operator, $date) {
360: return Finder::compare($file->getMTime(), $operator, $date);
361: });
362: }
363:
364:
365: 366: 367: 368: 369: 370:
371: public static function compare($l, $operator, $r)
372: {
373: switch ($operator) {
374: case '>':
375: return $l > $r;
376: case '>=':
377: return $l >= $r;
378: case '<':
379: return $l < $r;
380: case '<=':
381: return $l <= $r;
382: case '=':
383: case '==':
384: return $l == $r;
385: case '!':
386: case '!=':
387: case '<>':
388: return $l != $r;
389: default:
390: throw new Nette\InvalidArgumentException("Unknown operator $operator.");
391: }
392: }
393:
394: }
395: