1 | <?php |
||
9 | class CDispatcherBasic implements \Anax\DI\IInjectionAware |
||
10 | { |
||
11 | use \Anax\DI\TInjectionAware; |
||
12 | |||
13 | |||
14 | |||
15 | /** |
||
16 | * Properties |
||
17 | * |
||
18 | */ |
||
19 | private $controllerName; // Name of controller |
||
20 | private $controller; // Actual controller |
||
21 | private $action; // Name of action |
||
22 | private $params; // Params |
||
23 | |||
24 | |||
25 | |||
26 | /** |
||
27 | * Prepare the name. |
||
28 | * |
||
29 | * @param string $name to prepare. |
||
30 | * |
||
31 | * @return string as the prepared name. |
||
32 | */ |
||
33 | 1 | public function prepareName($name) |
|
34 | { |
||
35 | 1 | $name = empty($name) ? 'index' : $name; |
|
36 | 1 | $name = strtolower($name); |
|
37 | 1 | $name = str_replace(['-', '_'], ' ', $name); |
|
38 | 1 | $name = ucwords($name); |
|
39 | 1 | $name = str_replace(' ', '', $name); |
|
40 | |||
41 | 1 | return $name; |
|
42 | } |
||
43 | |||
44 | |||
45 | |||
46 | /** |
||
47 | * Set the name of the controller. |
||
48 | * |
||
49 | * @param string $name of the controller, defaults to 'index'. |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | 1 | public function setControllerName($name = 'index') |
|
54 | { |
||
55 | 1 | $name = $this->prepareName($name) . 'Controller'; |
|
56 | |||
57 | 1 | $this->controllerName = $name; |
|
58 | |||
59 | 1 | $this->controller = $this->di->has($name) |
|
60 | 1 | ? $this->di->get($name) |
|
61 | 1 | : null; |
|
62 | 1 | } |
|
63 | |||
64 | |||
65 | |||
66 | /** |
||
67 | * Check if a controller exists with this name. |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | 2 | public function isValidController() |
|
75 | |||
76 | |||
77 | |||
78 | /** |
||
79 | * Set the name of the action. |
||
80 | * |
||
81 | * @param string $name of the action, defaults to 'index'. |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | 1 | public function setActionName($name = 'index') |
|
89 | |||
90 | |||
91 | |||
92 | /** |
||
93 | * Set the params. |
||
94 | * |
||
95 | * @param array $params all parameters, defaults to empty. |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | public function setParams($params = []) |
||
103 | |||
104 | |||
105 | |||
106 | /** |
||
107 | * Dispatch to a controller, action with parameters. |
||
108 | * |
||
109 | * @return bool |
||
110 | */ |
||
111 | public function isCallable() |
||
124 | |||
125 | |||
126 | /** |
||
127 | * Inspect if callable and throw exception if parts is not callable. |
||
128 | * |
||
129 | * @return void |
||
130 | * @throws \Exception |
||
131 | */ |
||
132 | 2 | public function isCallableOrException() |
|
177 | |||
178 | |||
179 | |||
180 | /** |
||
181 | * Dispatch to a controller, action with parameters. |
||
182 | * |
||
183 | * @return mixed result from dispatched controller action. |
||
184 | */ |
||
185 | 2 | public function dispatch() |
|
196 | |||
197 | |||
198 | /** |
||
199 | * Forward to a controller, action with parameters. |
||
200 | * |
||
201 | * @param array $forward with details for controller, action, parameters. |
||
202 | * |
||
203 | * @return mixed result from dispatched controller action. |
||
204 | */ |
||
205 | public function forward($forward = []) |
||
226 | } |
||
227 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.