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: * PathFilterIterator filters files by path patterns (e.g. some/special/dir).
16: *
17: * @author Fabien Potencier <fabien@symfony.com>
18: * @author Włodzimierz Gajda <gajdaw@gajdaw.pl>
19: */
20: class PathFilterIterator extends MultiplePcreFilterIterator
21: {
22:
23: /**
24: * Filters the iterator values.
25: *
26: * @return Boolean true if the value should be kept, false otherwise
27: */
28: public function accept()
29: {
30: $filename = $this->current()->getRelativePathname();
31:
32: if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
33: $filename = strtr($filename, '\\', '/');
34: }
35:
36: // should at least not match one rule to exclude
37: foreach ($this->noMatchRegexps as $regex) {
38: if (preg_match($regex, $filename)) {
39: return false;
40: }
41: }
42:
43: // should at least match one rule
44: $match = true;
45: if ($this->matchRegexps) {
46: $match = false;
47: foreach ($this->matchRegexps as $regex) {
48: if (preg_match($regex, $filename)) {
49: return true;
50: }
51: }
52: }
53:
54: return $match;
55: }
56:
57: /**
58: * Converts strings to regexp.
59: *
60: * PCRE patterns are left unchanged.
61: *
62: * Default conversion:
63: * 'lorem/ipsum/dolor' ==> 'lorem\/ipsum\/dolor/'
64: *
65: * Use only / as directory separator (on Windows also).
66: *
67: * @param string $str Pattern: regexp or dirname.
68: *
69: * @return string regexp corresponding to a given string or regexp
70: */
71: protected function toRegex($str)
72: {
73: return $this->isRegex($str) ? $str : '/'.preg_quote($str, '/').'/';
74: }
75: }
76: