1 |
cb15593b
|
Cajova-Houba
|
<?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;
|
13 |
|
|
|
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 |
|
|
*
|
31 |
|
|
* @author Fabien Potencier <fabien@symfony.com> PHP port
|
32 |
|
|
* @author Richard Clamp <richardc@unixbeard.net> Perl version
|
33 |
|
|
* @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
|
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 |
|
|
*
|
41 |
|
|
* @param string $glob The glob pattern
|
42 |
|
|
* @param bool $strictLeadingDot
|
43 |
|
|
* @param bool $strictWildcardSlash
|
44 |
|
|
* @param string $delimiter Optional delimiter
|
45 |
|
|
*
|
46 |
|
|
* @return string regex The regexp
|
47 |
|
|
*/
|
48 |
|
|
public static function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true, $delimiter = '#')
|
49 |
|
|
{
|
50 |
|
|
$firstByte = true;
|
51 |
|
|
$escaping = false;
|
52 |
|
|
$inCurlies = 0;
|
53 |
|
|
$regex = '';
|
54 |
|
|
$sizeGlob = strlen($glob);
|
55 |
|
|
for ($i = 0; $i < $sizeGlob; ++$i) {
|
56 |
|
|
$car = $glob[$i];
|
57 |
|
|
if ($firstByte) {
|
58 |
|
|
if ($strictLeadingDot && '.' !== $car) {
|
59 |
|
|
$regex .= '(?=[^\.])';
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
$firstByte = false;
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
if ('/' === $car) {
|
66 |
|
|
$firstByte = true;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
if ($delimiter === $car || '.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
|
70 |
|
|
$regex .= "\\$car";
|
71 |
|
|
} elseif ('*' === $car) {
|
72 |
|
|
$regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
|
73 |
|
|
} elseif ('?' === $car) {
|
74 |
|
|
$regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
|
75 |
|
|
} elseif ('{' === $car) {
|
76 |
|
|
$regex .= $escaping ? '\\{' : '(';
|
77 |
|
|
if (!$escaping) {
|
78 |
|
|
++$inCurlies;
|
79 |
|
|
}
|
80 |
|
|
} elseif ('}' === $car && $inCurlies) {
|
81 |
|
|
$regex .= $escaping ? '}' : ')';
|
82 |
|
|
if (!$escaping) {
|
83 |
|
|
--$inCurlies;
|
84 |
|
|
}
|
85 |
|
|
} elseif (',' === $car && $inCurlies) {
|
86 |
|
|
$regex .= $escaping ? ',' : '|';
|
87 |
|
|
} elseif ('\\' === $car) {
|
88 |
|
|
if ($escaping) {
|
89 |
|
|
$regex .= '\\\\';
|
90 |
|
|
$escaping = false;
|
91 |
|
|
} else {
|
92 |
|
|
$escaping = true;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
continue;
|
96 |
|
|
} else {
|
97 |
|
|
$regex .= $car;
|
98 |
|
|
}
|
99 |
|
|
$escaping = false;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
return $delimiter.'^'.$regex.'$'.$delimiter;
|
103 |
|
|
}
|
104 |
|
|
}
|