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
|
|
|
public function canForward(array $forward): bool |
73
|
|
|
{ |
74
|
|
|
$canForward = true; |
75
|
|
|
|
76
|
|
|
if ($this instanceof AbstractDispatcher || $this instanceof MvcDispatcher || $this instanceof CliDispatcher) { |
77
|
|
|
$canForward = false; |
78
|
|
|
|
79
|
|
|
if ((!isset($forward['namespace']) || $this->getNamespaceName() !== $forward['namespace']) && |
80
|
|
|
(!isset($forward['module']) || $this->getModuleName() !== $forward['module']) && |
81
|
|
|
(!isset($forward['action']) || $this->getActionName() !== $forward['action']) && |
82
|
|
|
(!isset($forward['params']) || $this->getParams() !== $forward['params']) |
83
|
|
|
) { |
84
|
|
|
if ($this instanceof MvcDispatcher) { |
85
|
|
|
if ((!isset($forward['controller']) || $this->getControllerName() !== $forward['controller'])) { |
86
|
|
|
$canForward = true; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($this instanceof CliDispatcher) { |
91
|
|
|
if ((!isset($forward['task']) || $this->getTaskName() !== $forward['task'])) { |
92
|
|
|
$canForward = true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $canForward; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function unsetForward(array $forward, ?array $parts = null): array |
102
|
|
|
{ |
103
|
|
|
$parts ??= [ |
104
|
|
|
'namespace', |
105
|
|
|
'module', |
106
|
|
|
'task', |
107
|
|
|
'controller', |
108
|
|
|
'action', |
109
|
|
|
'params', |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
foreach ($parts as $part) { |
113
|
|
|
if (is_null($forward[$part])) { |
114
|
|
|
unset($forward[$part]); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $forward; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function toArray(): array |
122
|
|
|
{ |
123
|
|
|
$ret = [ |
124
|
|
|
'namespace' => $this->getNamespaceName(), |
125
|
|
|
'module' => $this->getModuleName(), |
126
|
|
|
'action' => $this->getActionName(), |
127
|
|
|
'params' => $this->getParams(), |
128
|
|
|
'handlerClass' => $this->getHandlerClass(), |
129
|
|
|
'handlerSuffix' => $this->getHandlerSuffix(), |
130
|
|
|
'activeMethod' => $this->getActiveMethod(), |
131
|
|
|
'actionSuffix' => $this->getActionSuffix(), |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
if ($this instanceof MvcDispatcher) { |
135
|
|
|
$ret['controller'] = $this->getControllerName(); |
136
|
|
|
$ret['previousNamespace'] = $this->getPreviousNamespaceName(); |
137
|
|
|
$ret['previousController'] = $this->getPreviousControllerName(); |
138
|
|
|
$ret['previousAction'] = $this->getPreviousActionName(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($this instanceof CliDispatcher) { |
142
|
|
|
$ret['task'] = $this->getTaskName(); |
143
|
|
|
$ret['taskSuffix'] = $this->getTaskSuffix(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $ret; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|