Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.35% |
14 / 17 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| AbstractDispatcher | |
82.35% |
14 / 17 |
|
75.00% |
6 / 8 |
12.79 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| application | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getApplication | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setApplication | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| hasApplication | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setDefaultAction | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getDefaultAction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| dispatch | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Dispatch; |
| 16 | |
| 17 | use Pop\Application; |
| 18 | |
| 19 | /** |
| 20 | * Abstract dispatcher class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop |
| 24 | * @author Nick Sagona, III <nick@popphp.org> |
| 25 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 5.0.0 |
| 28 | */ |
| 29 | abstract class AbstractDispatcher implements DispatchableInterface |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Application object |
| 34 | * @var ?Application |
| 35 | */ |
| 36 | protected ?Application $application = null; |
| 37 | |
| 38 | /** |
| 39 | * Default action |
| 40 | * @var string |
| 41 | */ |
| 42 | protected string $defaultAction = 'error'; |
| 43 | |
| 44 | /** |
| 45 | * Dispatchable constructor |
| 46 | * |
| 47 | * @param ?Application $application |
| 48 | */ |
| 49 | public function __construct(?Application $application = null) |
| 50 | { |
| 51 | $this->application = $application; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get application object (alias) |
| 56 | * |
| 57 | * @return ?Application |
| 58 | */ |
| 59 | public function application(): ?Application |
| 60 | { |
| 61 | return $this->application; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get application object |
| 66 | * |
| 67 | * @return ?Application |
| 68 | */ |
| 69 | public function getApplication(): ?Application |
| 70 | { |
| 71 | return $this->application; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Set application object |
| 76 | * |
| 77 | * @param Application $application |
| 78 | * @return static |
| 79 | */ |
| 80 | public function setApplication(Application $application): static |
| 81 | { |
| 82 | $this->application = $application; |
| 83 | return $this; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Has application object |
| 88 | * |
| 89 | * @return bool |
| 90 | */ |
| 91 | public function hasApplication(): bool |
| 92 | { |
| 93 | return !empty($this->application); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Set the default action |
| 98 | * |
| 99 | * @param string $default |
| 100 | * @return static |
| 101 | */ |
| 102 | public function setDefaultAction(string $default): static |
| 103 | { |
| 104 | $this->defaultAction = $default; |
| 105 | return $this; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the default action |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | public function getDefaultAction(): string |
| 114 | { |
| 115 | return $this->defaultAction; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Dispatch the controller based on the action |
| 120 | * |
| 121 | * @param ?string $action |
| 122 | * @param ?array $params |
| 123 | * @throws Exception |
| 124 | * @return void |
| 125 | */ |
| 126 | public function dispatch(?string $action = null, ?array $params = null): void |
| 127 | { |
| 128 | // Dispatch route action |
| 129 | if (($action !== null) && method_exists($this, $action)) { |
| 130 | if ($params !== null) { |
| 131 | call_user_func_array([$this, $action], array_values($params)); |
| 132 | } else { |
| 133 | $this->$action(); |
| 134 | } |
| 135 | // Else, fallback to default route action |
| 136 | } else if (method_exists($this, $this->defaultAction)) { |
| 137 | $action = $this->defaultAction; |
| 138 | $this->$action(); |
| 139 | } else { |
| 140 | throw new Exception("The action to handle the route is not defined."); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | } |