Skip to content

Commit 9d0d084

Browse files
committed
Internally cache Glob::toRegex() method
1 parent 7b75c27 commit 9d0d084

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/Glob.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Glob
2222
/** @var string The glob pattern */
2323
protected $pattern;
2424

25+
/** @var array Memoization cache */
26+
private $cache = [];
27+
2528
/** @var string The directory separator */
2629
protected static $directorySeparator = DIRECTORY_SEPARATOR;
2730

@@ -104,6 +107,10 @@ public function reject(array $array): array
104107
/** Convert the glob a regular expression pattern. */
105108
public function toRegex(int $options = self::BOTH_ANCHORS): string
106109
{
110+
if (isset($this->cache[$options])) {
111+
return $this->cache[$options];
112+
}
113+
107114
$pattern = '';
108115
$characterGroup = 0;
109116
$patternGroup = 0;
@@ -194,7 +201,7 @@ public function toRegex(int $options = self::BOTH_ANCHORS): string
194201
$pattern = $pattern . '$';
195202
}
196203

197-
return sprintf('#%s#', $pattern);
204+
return $this->cache[$options] = sprintf('#%s#', $pattern);
198205
}
199206

200207
/** Return the glob pattern as a string. */

0 commit comments

Comments
 (0)