From 49be4416fefaa3712129bae62e14ce3fcb7b6300 Mon Sep 17 00:00:00 2001 From: Wilfried Date: Thu, 26 Sep 2019 14:50:12 +0200 Subject: [PATCH 1/9] Add .gitignore file --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..00380e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +!.gitignore +.idea From d86d53f91e9f9dd7ecee84bc6870f10b89302df1 Mon Sep 17 00:00:00 2001 From: Wilfried Jourdil Date: Thu, 26 Sep 2019 14:52:30 +0200 Subject: [PATCH 2/9] Allow to use wildcards as hook names --- src/Hook.php | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/Hook.php b/src/Hook.php index adc711e..f88caaa 100644 --- a/src/Hook.php +++ b/src/Hook.php @@ -177,15 +177,13 @@ protected function run($hook, $params, Callback $callback, $output = null) array_unshift($params, $callback); if (array_key_exists($hook, $this->watch)) { - if (is_array($this->watch[$hook])) { - foreach ($this->watch[$hook] as $function) { - if (!empty($this->stop[$hook])) { - unset($this->stop[$hook]); - break; - } - - $output = call_user_func_array($function['function'], $params); - $params[1] = $output; + $output = $this->getOutputForHook($hook, $params, $output); + } + else { + foreach(array_keys($this->watch) as $key) { + if(\Illuminate\Support\Str::is($key, $hook)) { + $output = $this->getOutputForHook($key, $params, $output); + break; } } } @@ -193,6 +191,30 @@ protected function run($hook, $params, Callback $callback, $output = null) return $output; } + /** + * Calculate and return the output for the given hook name + * + * @param string $hook Hook name + * @param array $params Parameters + * @param string|null $output html wrapped by hook + * @return mixed + */ + protected function getOutputForHook($hook, $params, $output) + { + if (is_array($this->watch[$hook])) { + foreach ($this->watch[$hook] as $function) { + if (!empty($this->stop[$hook])) { + unset($this->stop[$hook]); + break; + } + + $output = call_user_func_array($function['function'], $params); + $params[1] = $output; + } + } + return $output; + } + /** * Return the listeners. * From 455feec597c74a3a2b6cd7e69ae09e78e914243b Mon Sep 17 00:00:00 2001 From: Wilfried Jourdil Date: Thu, 26 Sep 2019 17:00:20 +0200 Subject: [PATCH 3/9] Add wildcards values to Hook::listen() params + update doc --- README.md | 24 ++++++++++++++++++++++++ src/Hook.php | 30 ++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 17aacd2..c53b433 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,30 @@ Hook::listen('template.hookName', function ($callback, $output, $variables) { }); ``` +# Wildcards + +You can use wildcards to attach the same listener to multiple hooks. +With wildcards, you can get another parameter `$wildcards` containing an array : +```php +Hook::listen('template.*', function($callback, $output, $variables, $wildcards = []) { + // For the hook "template.foobar", $wildcards will contain : ["foobar"] +}): +``` + +:exclamation: Warning : the wildcard hook will be executed **only if there is no exact match** for the given hook name. +
For example : +```php +Hook::listen('foo', function () { + return 'foo'; +}); +Hook::listen('*', function () { + return 'wildcard'; +}); + +Hook::get('foobar'); // => Will return 'wildcard' +Hook::get('bar'); // => Will return 'wildcard' +Hook::get('foo'); // => Will return 'foo' +``` # Stop ```php diff --git a/src/Hook.php b/src/Hook.php index f88caaa..9d01835 100644 --- a/src/Hook.php +++ b/src/Hook.php @@ -86,6 +86,31 @@ public function listen($hook, $function, $priority = null) ksort($this->watch[$hook]); } + /** + * Extract and return the words corresponding to wildcard(s) in the hook name pattern + * + * @param string $hook Hook name + * @param string $pattern Pattern to match against hook name + * @return array + */ + protected function getWildcards($hook, $pattern) + { + $matches = []; + + // Prepare $hook as a regex pattern + $hook = '/^' . $hook . '$/'; + $hook = str_replace('.', '\.', $hook); + $hook = str_replace('*', '(.*)', $hook); + + // Try to match wildcards + preg_match($hook, $pattern, $matches); + + // Remove first element, containing the full pattern match + array_shift($matches); + + return $matches; + } + /** * Return all registered hooks. * @@ -177,11 +202,12 @@ protected function run($hook, $params, Callback $callback, $output = null) array_unshift($params, $callback); if (array_key_exists($hook, $this->watch)) { + array_push($params, []); $output = $this->getOutputForHook($hook, $params, $output); - } - else { + } else { foreach(array_keys($this->watch) as $key) { if(\Illuminate\Support\Str::is($key, $hook)) { + array_push($params, $this->getWildcards($key, $hook)); $output = $this->getOutputForHook($key, $params, $output); break; } From 0fa7c830dc337583ddfbc60efd4c84d5474a4afd Mon Sep 17 00:00:00 2001 From: Wilfried Date: Thu, 26 Sep 2019 17:04:07 +0200 Subject: [PATCH 4/9] Update wildcards example --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c53b433..01843ec 100644 --- a/README.md +++ b/README.md @@ -145,12 +145,12 @@ Hook::listen('template.*', function($callback, $output, $variables, $wildcards = :exclamation: Warning : the wildcard hook will be executed **only if there is no exact match** for the given hook name.
For example : ```php -Hook::listen('foo', function () { - return 'foo'; -}); Hook::listen('*', function () { return 'wildcard'; }); +Hook::listen('foo', function () { + return 'foo'; +}); Hook::get('foobar'); // => Will return 'wildcard' Hook::get('bar'); // => Will return 'wildcard' From 29bc59be2b078fb11fd9cae3ae19ad959dbddf15 Mon Sep 17 00:00:00 2001 From: Wilfried Date: Thu, 26 Sep 2019 17:12:15 +0200 Subject: [PATCH 5/9] Import Str helper --- src/Hook.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Hook.php b/src/Hook.php index 9d01835..42feba2 100644 --- a/src/Hook.php +++ b/src/Hook.php @@ -3,6 +3,7 @@ namespace Esemve\Hook; use Illuminate\Support\Arr; +use Illuminate\Support\Str; class Hook { @@ -206,7 +207,7 @@ protected function run($hook, $params, Callback $callback, $output = null) $output = $this->getOutputForHook($hook, $params, $output); } else { foreach(array_keys($this->watch) as $key) { - if(\Illuminate\Support\Str::is($key, $hook)) { + if(Str::is($key, $hook)) { array_push($params, $this->getWildcards($key, $hook)); $output = $this->getOutputForHook($key, $params, $output); break; From 78faef304bd3130981a2030b9cba5d886ddabaf0 Mon Sep 17 00:00:00 2001 From: Wilfried Date: Thu, 26 Sep 2019 17:16:06 +0200 Subject: [PATCH 6/9] Fix CI issues --- src/Hook.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Hook.php b/src/Hook.php index 42feba2..0d25e67 100644 --- a/src/Hook.php +++ b/src/Hook.php @@ -88,10 +88,10 @@ public function listen($hook, $function, $priority = null) } /** - * Extract and return the words corresponding to wildcard(s) in the hook name pattern + * Extract and return the words corresponding to wildcard(s) in the hook name pattern. * - * @param string $hook Hook name - * @param string $pattern Pattern to match against hook name + * @param string $hook Hook name + * @param string $pattern Pattern to match against hook name * @return array */ protected function getWildcards($hook, $pattern) @@ -99,7 +99,7 @@ protected function getWildcards($hook, $pattern) $matches = []; // Prepare $hook as a regex pattern - $hook = '/^' . $hook . '$/'; + $hook = '/^'.$hook.'$/'; $hook = str_replace('.', '\.', $hook); $hook = str_replace('*', '(.*)', $hook); @@ -206,8 +206,8 @@ protected function run($hook, $params, Callback $callback, $output = null) array_push($params, []); $output = $this->getOutputForHook($hook, $params, $output); } else { - foreach(array_keys($this->watch) as $key) { - if(Str::is($key, $hook)) { + foreach (array_keys($this->watch) as $key) { + if (Str::is($key, $hook)) { array_push($params, $this->getWildcards($key, $hook)); $output = $this->getOutputForHook($key, $params, $output); break; @@ -219,11 +219,11 @@ protected function run($hook, $params, Callback $callback, $output = null) } /** - * Calculate and return the output for the given hook name + * Calculate and return the output for the given hook name. * - * @param string $hook Hook name - * @param array $params Parameters - * @param string|null $output html wrapped by hook + * @param string $hook Hook name + * @param array $params Parameters + * @param string|null $output html wrapped by hook * @return mixed */ protected function getOutputForHook($hook, $params, $output) From ab352be826d1b32c2e37fdd0003e05a0903b2882 Mon Sep 17 00:00:00 2001 From: Wilfried Date: Thu, 26 Sep 2019 17:17:00 +0200 Subject: [PATCH 7/9] Fix others CI issues --- src/Hook.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Hook.php b/src/Hook.php index 0d25e67..07b5e6b 100644 --- a/src/Hook.php +++ b/src/Hook.php @@ -92,6 +92,7 @@ public function listen($hook, $function, $priority = null) * * @param string $hook Hook name * @param string $pattern Pattern to match against hook name + * * @return array */ protected function getWildcards($hook, $pattern) @@ -224,6 +225,7 @@ protected function run($hook, $params, Callback $callback, $output = null) * @param string $hook Hook name * @param array $params Parameters * @param string|null $output html wrapped by hook + * * @return mixed */ protected function getOutputForHook($hook, $params, $output) From 48f5ee084d7f9213a79f271ae84bf30d84dcf3df Mon Sep 17 00:00:00 2001 From: Wilfried Date: Thu, 26 Sep 2019 17:17:42 +0200 Subject: [PATCH 8/9] Add empty line before return statement --- src/Hook.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Hook.php b/src/Hook.php index 07b5e6b..eaaa126 100644 --- a/src/Hook.php +++ b/src/Hook.php @@ -241,6 +241,7 @@ protected function getOutputForHook($hook, $params, $output) $params[1] = $output; } } + return $output; } From 3d6c9581597bde7bba7abbbc386825ef354c45c5 Mon Sep 17 00:00:00 2001 From: Wilfried Date: Fri, 27 Sep 2019 12:00:04 +0200 Subject: [PATCH 9/9] Add PHPDoc on Facade for autocomplete --- src/Facades/Hook.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Facades/Hook.php b/src/Facades/Hook.php index 710d300..5d4e3b8 100644 --- a/src/Facades/Hook.php +++ b/src/Facades/Hook.php @@ -4,6 +4,15 @@ use Illuminate\Support\Facades\Facade; +/** + * @method static void listen($hook, $function, $priority = null) + * @method static mixed get($hook, $params = [], callable $callback = null, $htmlContent = '') + * @method static void stop($hook) + * @method static void mock($name, $return) + * @method static array getListeners() + * @method static array getEvents($hook) + * @method static array getHooks() + */ class Hook extends Facade { protected static function getFacadeAccessor()