DeepGettable.php
3 weeks ago
DependencyResolvable.php
3 weeks ago
Dispatchable.php
3 weeks ago
HasConstants.php
3 weeks ago
Dispatchable.php
66 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Trait giving event classes static dispatch, dispatch_if, and dispatch_unless helpers. |
| 5 | * Instantiates the event and forwards it to the Event facade in one call. |
| 6 | * Keeps domain events decoupled from the EventManager while preserving a fluent dispatch API. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Concerns |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Concerns; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use Closure; |
| 16 | use Kirki\Framework\Supports\Facades\Event; |
| 17 | \defined('ABSPATH') || exit; |
| 18 | trait Dispatchable |
| 19 | { |
| 20 | /** |
| 21 | * Dispatch the event. |
| 22 | * |
| 23 | * @return void |
| 24 | * |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | public static function dispatch() |
| 28 | { |
| 29 | Event::dispatch(new static(...\func_get_args())); |
| 30 | } |
| 31 | /** |
| 32 | * Dispatch the event if the boolean is true. |
| 33 | * |
| 34 | * @param mixed $boolean The boolean. |
| 35 | * @param mixed $arguments The method arguments. |
| 36 | * |
| 37 | * @return void |
| 38 | * |
| 39 | * @since 1.0.0 |
| 40 | */ |
| 41 | public static function dispatch_if($boolean, ...$arguments) |
| 42 | { |
| 43 | $boolean = $boolean instanceof Closure ? $boolean() : $boolean; |
| 44 | if ($boolean) { |
| 45 | Event::dispatch(new static(...$arguments)); |
| 46 | } |
| 47 | } |
| 48 | /** |
| 49 | * Dispatch the event unless the boolean is true. |
| 50 | * |
| 51 | * @param mixed $boolean The boolean. |
| 52 | * @param mixed $arguments The method arguments. |
| 53 | * |
| 54 | * @return void |
| 55 | * |
| 56 | * @since 1.0.0 |
| 57 | */ |
| 58 | public static function dispatch_unless($boolean, ...$arguments) |
| 59 | { |
| 60 | $boolean = $boolean instanceof Closure ? $boolean() : $boolean; |
| 61 | if (!$boolean) { |
| 62 | Event::dispatch(new static(...$arguments)); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 |