From cc3257181cfc25a025b049348c5f9053e83ec627 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Tue, 12 Feb 2019 11:00:06 -0500 Subject: [PATCH] Handle preg_quote changes for PHP 7.3 (#14) * Add PHP 7.3 to Travis * Prepare preg_replace() replacements by PHP version PHP 7.3+ requires the pattern to expect preg_quote() to escape `#` characters in the event pattern. --- .travis.yml | 1 + .../ListenerPattern.php | 59 ++++++++++++++----- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index fabfff5..64b381d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ php: - 7.0 - 7.1 - 7.2 + - 7.3 cache: directories: diff --git a/src/Jmikola/WildcardEventDispatcher/ListenerPattern.php b/src/Jmikola/WildcardEventDispatcher/ListenerPattern.php index abd6017..5619cd2 100644 --- a/src/Jmikola/WildcardEventDispatcher/ListenerPattern.php +++ b/src/Jmikola/WildcardEventDispatcher/ListenerPattern.php @@ -12,18 +12,7 @@ class ListenerPattern protected $priority; protected $regex; - private static $replacements = array( - // Trailing single-wildcard with separator prefix - '/\\\\\.\\\\\*$/' => '(?:\.\w+)?', - // Single-wildcard with separator prefix - '/\\\\\.\\\\\*/' => '(?:\.\w+)', - // Single-wildcard without separator prefix - '/(? '(?:\w+)', - // Multi-wildcard with separator prefix - '/\\\\\.#/' => '(?:\.\w+)*', - // Multi-wildcard without separator prefix - '/(? '(?:|\w+(?:\.\w+)*)', - ); + private static $replacements; /** * Constructor. @@ -110,10 +99,52 @@ class ListenerPattern */ private function createRegex($eventPattern) { + $replacements = self::getReplacements(); + return sprintf('/^%s$/', preg_replace( - array_keys(self::$replacements), - array_values(self::$replacements), + array_keys($replacements), + array_values($replacements), preg_quote($eventPattern, '/') )); } + + /** + * Returns preg_replace() replacements for preparing the event pattern. + * + * @return array + */ + private static function getReplacements() + { + if (null !== self::$replacements) { + return self::$replacements; + } + + self::$replacements = array( + // Trailing single-wildcard with separator prefix + '/\\\\\.\\\\\*$/' => '(?:\.\w+)?', + // Single-wildcard with separator prefix + '/\\\\\.\\\\\*/' => '(?:\.\w+)', + // Single-wildcard without separator prefix + '/(? '(?:\w+)', + ); + + // preg_quote() escapes `#` in PHP 7.3+ + if (PHP_VERSION_ID >= 70300) { + self::$replacements += array( + // Multi-wildcard with separator prefix + '/\\\\\.\\\\\#/' => '(?:\.\w+)*', + // Multi-wildcard without separator prefix + '/(? '(?:|\w+(?:\.\w+)*)', + ); + } else { + self::$replacements += array( + // Multi-wildcard with separator prefix + '/\\\\\.#/' => '(?:\.\w+)*', + // Multi-wildcard without separator prefix + '/(? '(?:|\w+(?:\.\w+)*)', + ); + } + + return self::$replacements; + } } \ No newline at end of file