TYPO3  7.6
Glob.php
Go to the documentation of this file.
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;
13 
36 class Glob
37 {
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 ('.' === $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 }