|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Zemit Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Zemit Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zemit\Dispatcher; |
|
13
|
|
|
|
|
14
|
|
|
use Phalcon\Mvc\Dispatcher as MvcDispatcher; |
|
15
|
|
|
use Phalcon\Cli\Dispatcher as CliDispatcher; |
|
16
|
|
|
|
|
17
|
|
|
trait DispatcherTrait |
|
18
|
|
|
{ |
|
19
|
|
|
abstract public function getNamespaceName(): string; |
|
20
|
|
|
|
|
21
|
|
|
abstract public function getModuleName(): string; |
|
22
|
|
|
|
|
23
|
|
|
abstract public function getActionName(): string; |
|
24
|
|
|
|
|
25
|
|
|
abstract public function getParams(): array; |
|
26
|
|
|
|
|
27
|
|
|
abstract public function getHandlerClass(): string; |
|
28
|
|
|
|
|
29
|
|
|
abstract public function getHandlerSuffix(): string; |
|
30
|
|
|
|
|
31
|
|
|
// abstract public function getTaskName(): string; |
|
32
|
|
|
|
|
33
|
|
|
// abstract public function getControllerName(): string; |
|
34
|
|
|
|
|
35
|
|
|
abstract public function getActionSuffix(): string; |
|
36
|
|
|
|
|
37
|
|
|
abstract public function getActiveMethod(): string; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritDoc} |
|
41
|
|
|
* The string typed keys are not passed to the action method arguments |
|
42
|
|
|
* Only the int keys will be passed |
|
43
|
|
|
* |
|
44
|
|
|
* @param $handler |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
public function callActionMethod($handler, string $actionMethod, array $params = []) |
|
48
|
|
|
{ |
|
49
|
|
|
return call_user_func_array( |
|
50
|
|
|
[$handler, $actionMethod], |
|
51
|
|
|
array_filter($params, 'is_int', ARRAY_FILTER_USE_KEY) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Extending forwarding event to prevent cyclic routing when forwarding under dispatcher events |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function forward(array $forward, bool $preventCycle = false): void |
|
60
|
|
|
{ |
|
61
|
|
|
$forward = $this->unsetForward($forward); |
|
62
|
|
|
|
|
63
|
|
|
if (!$preventCycle) { |
|
64
|
|
|
parent::forward($forward); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
elseif ($this->canForward($forward)) { |
|
68
|
|
|
parent::forward($forward); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Check whether the forward attribute can be forwarded |
|
74
|
|
|
* we do additional checks to prevent dispatcher cycling |
|
75
|
|
|
*/ |
|
76
|
|
|
public function canForward(array $forward): bool |
|
77
|
|
|
{ |
|
78
|
|
|
$parts = [ |
|
79
|
|
|
'namespace' => $this->getNamespaceName(), |
|
80
|
|
|
'module' => $this->getModuleName(), |
|
81
|
|
|
'action' => $this->getActionName(), |
|
82
|
|
|
'params' => $this->getParams(), |
|
83
|
|
|
]; |
|
84
|
|
|
foreach ($parts as $part => $current) { |
|
85
|
|
|
if (isset($forward[$part]) && $current !== $forward[$part]) { |
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->canForwardHandler($forward); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Check whether the handler is changed or not |
|
95
|
|
|
* depending on the dispatcher |
|
96
|
|
|
* MVC: controller |
|
97
|
|
|
* CLI: task |
|
98
|
|
|
*/ |
|
99
|
|
|
public function canForwardHandler(array $forward): bool |
|
100
|
|
|
{ |
|
101
|
|
|
if ($this instanceof MvcDispatcher && |
|
102
|
|
|
isset($forward['controller']) && |
|
103
|
|
|
$this->getControllerName() !== $forward['controller'] |
|
104
|
|
|
) { |
|
105
|
|
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if ($this instanceof CliDispatcher && |
|
109
|
|
|
isset($forward['task']) && |
|
110
|
|
|
$this->getTaskName() !== $forward['task'] |
|
111
|
|
|
) { |
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public function unsetForward(array $forward, ?array $parts = null): array |
|
119
|
|
|
{ |
|
120
|
|
|
$parts ??= [ |
|
121
|
|
|
'namespace', |
|
122
|
|
|
'module', |
|
123
|
|
|
'task', |
|
124
|
|
|
'controller', |
|
125
|
|
|
'action', |
|
126
|
|
|
'params', |
|
127
|
|
|
]; |
|
128
|
|
|
|
|
129
|
|
|
foreach ($parts as $part) { |
|
130
|
|
|
if (is_null($forward[$part])) { |
|
131
|
|
|
unset($forward[$part]); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $forward; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function toArray(): array |
|
139
|
|
|
{ |
|
140
|
|
|
$ret = [ |
|
141
|
|
|
'namespace' => $this->getNamespaceName(), |
|
142
|
|
|
'module' => $this->getModuleName(), |
|
143
|
|
|
'action' => $this->getActionName(), |
|
144
|
|
|
'params' => $this->getParams(), |
|
145
|
|
|
'handlerClass' => $this->getHandlerClass(), |
|
146
|
|
|
'handlerSuffix' => $this->getHandlerSuffix(), |
|
147
|
|
|
'activeMethod' => $this->getActiveMethod(), |
|
148
|
|
|
'actionSuffix' => $this->getActionSuffix(), |
|
149
|
|
|
]; |
|
150
|
|
|
|
|
151
|
|
|
if ($this instanceof MvcDispatcher) { |
|
152
|
|
|
$ret['controller'] = $this->getControllerName(); |
|
153
|
|
|
$ret['previousNamespace'] = $this->getPreviousNamespaceName(); |
|
154
|
|
|
$ret['previousController'] = $this->getPreviousControllerName(); |
|
155
|
|
|
$ret['previousAction'] = $this->getPreviousActionName(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
if ($this instanceof CliDispatcher) { |
|
159
|
|
|
$ret['task'] = $this->getTaskName(); |
|
160
|
|
|
$ret['taskSuffix'] = $this->getTaskSuffix(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $ret; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|