2010-02-17 13:55:05 +00:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\Finder;
|
2010-02-17 13:55:05 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-21 08:31:18 +01:00
|
|
|
* Glob matches globbing patterns against text.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
|
|
|
* if match_glob("foo.*", "foo.bar") echo "matched\n";
|
|
|
|
*
|
|
|
|
* // prints foo.bar and foo.baz
|
|
|
|
* $regex = glob_to_regex("foo.*");
|
|
|
|
* for (array('foo.bar', 'foo.baz', 'foo', 'bar') as $t)
|
|
|
|
* {
|
|
|
|
* if (/$regex/) echo "matched: $car\n";
|
|
|
|
* }
|
|
|
|
*
|
2010-02-19 06:59:36 +00:00
|
|
|
* Glob implements glob(3) style matching that can be used to match
|
2010-02-17 13:55:05 +00:00
|
|
|
* against text, rather than fetching names from a filesystem.
|
|
|
|
*
|
2010-04-21 08:31:18 +01:00
|
|
|
* Based on the Perl Text::Glob module.
|
2010-02-17 13:55:05 +00:00
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com> PHP port
|
2010-04-21 08:31:18 +01:00
|
|
|
* @author Richard Clamp <richardc@unixbeard.net> Perl version
|
2010-04-07 01:51:29 +01:00
|
|
|
* @copyright 2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-02-17 13:55:05 +00:00
|
|
|
* @copyright 2002 Richard Clamp <richardc@unixbeard.net>
|
|
|
|
*/
|
2010-02-19 06:59:36 +00:00
|
|
|
class Glob
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns a regexp which is the equivalent of the glob pattern.
|
|
|
|
*
|
|
|
|
* @param string $glob The glob pattern
|
|
|
|
*
|
|
|
|
* @return string regex The regexp
|
|
|
|
*/
|
|
|
|
static public function toRegex($glob, $strictLeadingDot = true, $strictWildcardSlash = true)
|
2010-02-17 13:55:05 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$firstByte = true;
|
|
|
|
$escaping = false;
|
|
|
|
$inCurlies = 0;
|
|
|
|
$regex = '';
|
|
|
|
$sizeGlob = strlen($glob);
|
2010-05-07 15:09:11 +01:00
|
|
|
for ($i = 0; $i < $sizeGlob; $i++) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$car = $glob[$i];
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($firstByte) {
|
2010-12-21 02:59:17 +00:00
|
|
|
if ($strictLeadingDot && '.' !== $car) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$regex .= '(?=[^\.])';
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$firstByte = false;
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-12-21 02:59:17 +00:00
|
|
|
if ('/' === $car) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$firstByte = true;
|
|
|
|
}
|
2010-02-17 13:55:05 +00:00
|
|
|
|
2010-12-21 02:59:17 +00:00
|
|
|
if ('.' === $car || '(' === $car || ')' === $car || '|' === $car || '+' === $car || '^' === $car || '$' === $car) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$regex .= "\\$car";
|
2010-12-21 02:59:17 +00:00
|
|
|
} elseif ('*' === $car) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$regex .= $escaping ? '\\*' : ($strictWildcardSlash ? '[^/]*' : '.*');
|
2010-12-21 02:59:17 +00:00
|
|
|
} elseif ('?' === $car) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$regex .= $escaping ? '\\?' : ($strictWildcardSlash ? '[^/]' : '.');
|
2010-12-21 02:59:17 +00:00
|
|
|
} elseif ('{' === $car) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$regex .= $escaping ? '\\{' : '(';
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$escaping) {
|
2010-05-06 12:25:53 +01:00
|
|
|
++$inCurlies;
|
|
|
|
}
|
2010-12-21 02:59:17 +00:00
|
|
|
} elseif ('}' === $car && $inCurlies) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$regex .= $escaping ? '}' : ')';
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$escaping) {
|
2010-05-06 12:25:53 +01:00
|
|
|
--$inCurlies;
|
|
|
|
}
|
2010-12-21 02:59:17 +00:00
|
|
|
} elseif (',' === $car && $inCurlies) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$regex .= $escaping ? ',' : '|';
|
2010-12-21 02:59:17 +00:00
|
|
|
} elseif ('\\' === $car) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if ($escaping) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$regex .= '\\\\';
|
|
|
|
$escaping = false;
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$escaping = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$regex .= $car;
|
|
|
|
}
|
|
|
|
$escaping = false;
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return '#^'.$regex.'$#';
|
2010-02-17 13:55:05 +00:00
|
|
|
}
|
|
|
|
}
|