From 306585e4cd6b8b7b852b2f3894c55c6502938edc Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Sun, 21 Jan 2024 16:05:29 -0500 Subject: [PATCH] Fluent hook calls --- CHANGELOG.md | 4 ++++ src/Hooks.php | 12 +++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c1784d..5ccabd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ format. This project adheres to [Semantic Versioning](https://semver.org/spec/v2 ## [Unreleased] +### Added + +- Made hook calls fluent + ## [0.0.2] - 2024-01-21 ### Changed diff --git a/src/Hooks.php b/src/Hooks.php index 76e82e3..d1ac4ef 100644 --- a/src/Hooks.php +++ b/src/Hooks.php @@ -15,25 +15,31 @@ public function __construct( ) { } - public function __call(string $name, array $arguments) + public function __call(string $name, array $arguments): static { $hook = $arguments[0]; $priority = $arguments[1] ?? Hook::DEFAULT_PRIORITY; $this->on($name, $hook, $priority); + + return $this; } - public function default(Closure|Hook $hook, int $priority = Hook::DEFAULT_PRIORITY) + public function default(Closure|Hook $hook, int $priority = Hook::DEFAULT_PRIORITY): static { $this->on(static::DEFAULT, $hook, $priority); + + return $this; } - public function on(string $name, Closure|Hook $hook, int $priority = Hook::DEFAULT_PRIORITY) + public function on(string $name, Closure|Hook $hook, int $priority = Hook::DEFAULT_PRIORITY): static { if ($hook instanceof Closure) { $hook = new Hook($hook, $priority); } $this->registry->register($hook, $this->target, $name); + + return $this; } }