Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/CrawlerDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ class CrawlerDetect
*/
protected $compiledExclusions;

/**
* Cache of compiled regex strings keyed by pattern-list hash, shared
* across instances so per-request `new CrawlerDetect` calls don't
* re-implode the (~1500-entry) pattern list each time.
*
* @var array<string, string>
*/
protected static $compileCache = [];

/**
* Class constructor.
*/
Expand All @@ -92,12 +101,21 @@ public function __construct(?array $headers = null, $userAgent = null)
/**
* Compile the regex patterns into one regex string.
*
* A non-capturing group is used because callers only need the full
* match (preg_match's $matches[0]), not a back-reference.
*
* @param array
* @return string
*/
public function compileRegex($patterns)
{
return '('.implode('|', $patterns).')';
$key = md5(serialize($patterns));

if (! isset(self::$compileCache[$key])) {
self::$compileCache[$key] = '(?:'.implode('|', $patterns).')';
}

return self::$compileCache[$key];
}

/**
Expand Down
Loading