Skip to content
Newer
Older
100644 104 lines (95 sloc) 3.1 KB
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
1 <?php
2
3 /*
782e089 @blue-eyes normalized license messages in PHP files
blue-eyes authored
4 * This file is part of the Symfony package.
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
5 *
db96c6a @fabpot replaced symfony-project.org by symfony.com
fabpot authored
6 * (c) Fabien Potencier <fabien@symfony.com>
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
7 *
782e089 @blue-eyes normalized license messages in PHP files
blue-eyes authored
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
10 */
11
782e089 @blue-eyes normalized license messages in PHP files
blue-eyes authored
12 namespace Symfony\Component\Finder;
13
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
14 /**
15 * Glob matches globbing patterns against text.
16 *
17 * if match_glob("foo.*", "foo.bar") echo "matched\n";
18 *
19 * // prints foo.bar and foo.baz
20 * $regex = glob_to_regex("foo.*");
21 * for (array('foo.bar', 'foo.baz', 'foo', 'bar') as $t)
22 * {
23 * if (/$regex/) echo "matched: $car\n";
24 * }
25 *
26 * Glob implements glob(3) style matching that can be used to match
27 * against text, rather than fetching names from a filesystem.
28 *
29 * Based on the Perl Text::Glob module.
30 *
db96c6a @fabpot replaced symfony-project.org by symfony.com
fabpot authored
31 * @author Fabien Potencier <fabien@symfony.com> PHP port
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
32 * @author Richard Clamp <richardc@unixbeard.net> Perl version
db96c6a @fabpot replaced symfony-project.org by symfony.com
fabpot authored
33 * @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
34 * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
35 */
36 class Glob
37 {
38 /**
39 * Returns a regexp which is the equivalent of the glob pattern.
40 *
099248d @fabpot fixed CS
fabpot authored
41 * @param string $glob The glob pattern
215b265 @pborreli [Phpdoc] Cleaning/fixing
pborreli authored
42 * @param Boolean $strictLeadingDot
43 * @param Boolean $strictWildcardSlash
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
44 *
45 * @return string regex The regexp
46 */
fccf904 @fabpot fixed CS
fabpot authored
47 public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true)
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
48 {
49 $firstByte = true;
50 $escaping = false;
51 $inCurlies = 0;
52 $regex = '';
53 $sizeGlob = strlen($glob);
54 for ($i = 0; $i < $sizeGlob; $i++) {
55 $car = $glob[$i];
56 if ($firstByte) {
7d3565e @Seldaek CS: Unified strict equality comparisons, put var on the right side
Seldaek authored
57 if ($strictLeadingDot && '.' !== $car) {
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
58 $regex .= '(?=[^\.])';
59 }
60
61 $firstByte = false;
62 }
63
7d3565e @Seldaek CS: Unified strict equality comparisons, put var on the right side
Seldaek authored
64 if ('/' === $car) {
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
65 $firstByte = true;
66 }
67
7d3565e @Seldaek CS: Unified strict equality comparisons, put var on the right side
Seldaek authored
68 if ('.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
69 $regex .= "\\$car";
7d3565e @Seldaek CS: Unified strict equality comparisons, put var on the right side
Seldaek authored
70 } elseif ('*' === $car) {
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
71 $regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
7d3565e @Seldaek CS: Unified strict equality comparisons, put var on the right side
Seldaek authored
72 } elseif ('?' === $car) {
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
73 $regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
7d3565e @Seldaek CS: Unified strict equality comparisons, put var on the right side
Seldaek authored
74 } elseif ('{' === $car) {
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
75 $regex .= $escaping ? '\\{' : '(';
76 if (!$escaping) {
77 ++$inCurlies;
78 }
7d3565e @Seldaek CS: Unified strict equality comparisons, put var on the right side
Seldaek authored
79 } elseif ('}' === $car && $inCurlies) {
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
80 $regex .= $escaping ? '}' : ')';
81 if (!$escaping) {
82 --$inCurlies;
83 }
7d3565e @Seldaek CS: Unified strict equality comparisons, put var on the right side
Seldaek authored
84 } elseif (',' === $car && $inCurlies) {
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
85 $regex .= $escaping ? ',' : '|';
7d3565e @Seldaek CS: Unified strict equality comparisons, put var on the right side
Seldaek authored
86 } elseif ('\\' === $car) {
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored
87 if ($escaping) {
88 $regex .= '\\\\';
89 $escaping = false;
90 } else {
91 $escaping = true;
92 }
93
94 continue;
95 } else {
96 $regex .= $car;
97 }
98 $escaping = false;
99 }
100
101 return '#^'.$regex.'$#';
102 }
103 }
Something went wrong with that request. Please try again.