Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Finder;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Glob matches globbing patterns against text.
|
Chris@0
|
16 *
|
Chris@17
|
17 * if match_glob("foo.*", "foo.bar") echo "matched\n";
|
Chris@0
|
18 *
|
Chris@17
|
19 * // prints foo.bar and foo.baz
|
Chris@17
|
20 * $regex = glob_to_regex("foo.*");
|
Chris@17
|
21 * for (['foo.bar', 'foo.baz', 'foo', 'bar'] as $t)
|
Chris@17
|
22 * {
|
Chris@17
|
23 * if (/$regex/) echo "matched: $car\n";
|
Chris@17
|
24 * }
|
Chris@0
|
25 *
|
Chris@0
|
26 * Glob implements glob(3) style matching that can be used to match
|
Chris@0
|
27 * against text, rather than fetching names from a filesystem.
|
Chris@0
|
28 *
|
Chris@0
|
29 * Based on the Perl Text::Glob module.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @author Fabien Potencier <fabien@symfony.com> PHP port
|
Chris@0
|
32 * @author Richard Clamp <richardc@unixbeard.net> Perl version
|
Chris@0
|
33 * @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
34 * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
|
Chris@0
|
35 */
|
Chris@0
|
36 class Glob
|
Chris@0
|
37 {
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Returns a regexp which is the equivalent of the glob pattern.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param string $glob The glob pattern
|
Chris@0
|
42 * @param bool $strictLeadingDot
|
Chris@0
|
43 * @param bool $strictWildcardSlash
|
Chris@0
|
44 * @param string $delimiter Optional delimiter
|
Chris@0
|
45 *
|
Chris@0
|
46 * @return string regex The regexp
|
Chris@0
|
47 */
|
Chris@0
|
48 public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true, $delimiter = '#')
|
Chris@0
|
49 {
|
Chris@0
|
50 $firstByte = true;
|
Chris@0
|
51 $escaping = false;
|
Chris@0
|
52 $inCurlies = 0;
|
Chris@0
|
53 $regex = '';
|
Chris@17
|
54 $sizeGlob = \strlen($glob);
|
Chris@0
|
55 for ($i = 0; $i < $sizeGlob; ++$i) {
|
Chris@0
|
56 $car = $glob[$i];
|
Chris@0
|
57 if ($firstByte && $strictLeadingDot && '.' !== $car) {
|
Chris@0
|
58 $regex .= '(?=[^\.])';
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 $firstByte = '/' === $car;
|
Chris@0
|
62
|
Chris@0
|
63 if ($firstByte && $strictWildcardSlash && isset($glob[$i + 2]) && '**' === $glob[$i + 1].$glob[$i + 2] && (!isset($glob[$i + 3]) || '/' === $glob[$i + 3])) {
|
Chris@0
|
64 $car = '[^/]++/';
|
Chris@0
|
65 if (!isset($glob[$i + 3])) {
|
Chris@0
|
66 $car .= '?';
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 if ($strictLeadingDot) {
|
Chris@0
|
70 $car = '(?=[^\.])'.$car;
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 $car = '/(?:'.$car.')*';
|
Chris@0
|
74 $i += 2 + isset($glob[$i + 3]);
|
Chris@0
|
75
|
Chris@0
|
76 if ('/' === $delimiter) {
|
Chris@0
|
77 $car = str_replace('/', '\\/', $car);
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
|
Chris@0
|
82 $regex .= "\\$car";
|
Chris@0
|
83 } elseif ('*' === $car) {
|
Chris@0
|
84 $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
|
Chris@0
|
85 } elseif ('?' === $car) {
|
Chris@0
|
86 $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
|
Chris@0
|
87 } elseif ('{' === $car) {
|
Chris@0
|
88 $regex .= $escaping ? '\\{' : '(';
|
Chris@0
|
89 if (!$escaping) {
|
Chris@0
|
90 ++$inCurlies;
|
Chris@0
|
91 }
|
Chris@0
|
92 } elseif ('}' === $car && $inCurlies) {
|
Chris@0
|
93 $regex .= $escaping ? '}' : ')';
|
Chris@0
|
94 if (!$escaping) {
|
Chris@0
|
95 --$inCurlies;
|
Chris@0
|
96 }
|
Chris@0
|
97 } elseif (',' === $car && $inCurlies) {
|
Chris@0
|
98 $regex .= $escaping ? ',' : '|';
|
Chris@0
|
99 } elseif ('\\' === $car) {
|
Chris@0
|
100 if ($escaping) {
|
Chris@0
|
101 $regex .= '\\\\';
|
Chris@0
|
102 $escaping = false;
|
Chris@0
|
103 } else {
|
Chris@0
|
104 $escaping = true;
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 continue;
|
Chris@0
|
108 } else {
|
Chris@0
|
109 $regex .= $car;
|
Chris@0
|
110 }
|
Chris@0
|
111 $escaping = false;
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 return $delimiter.'^'.$regex.'$'.$delimiter;
|
Chris@0
|
115 }
|
Chris@0
|
116 }
|