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\Dispatcher\AbstractDispatcher; |
|
|
|
|
15
|
|
|
use Phalcon\Mvc\Dispatcher as MvcDispatcher; |
16
|
|
|
use Phalcon\Cli\Dispatcher as CliDispatcher; |
17
|
|
|
|
18
|
|
|
trait DispatcherTrait |
19
|
|
|
{ |
20
|
|
|
abstract public function getNamespaceName(): string; |
21
|
|
|
|
22
|
|
|
abstract public function getModuleName(): string; |
23
|
|
|
|
24
|
|
|
abstract public function getActionName(): string; |
25
|
|
|
|
26
|
|
|
abstract public function getParams(): array; |
27
|
|
|
|
28
|
|
|
abstract public function getHandlerClass(): string; |
29
|
|
|
|
30
|
|
|
abstract public function getHandlerSuffix(): string; |
31
|
|
|
|
32
|
|
|
// abstract public function getTaskName(): string; |
33
|
|
|
|
34
|
|
|
// abstract public function getControllerName(): string; |
35
|
|
|
|
36
|
|
|
abstract public function getActionSuffix(): string; |
37
|
|
|
|
38
|
|
|
abstract public function getActiveMethod(): string; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
* The string typed keys are not passed to the action method arguments |
43
|
|
|
* Only the int keys will be passed |
44
|
|
|
* |
45
|
|
|
* @param $handler |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
public function callActionMethod($handler, string $actionMethod, array $params = []) |
49
|
|
|
{ |
50
|
|
|
return call_user_func_array( |
51
|
|
|
[$handler, $actionMethod], |
52
|
|
|
array_filter($params, 'is_int', ARRAY_FILTER_USE_KEY) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Extending forwarding event to prevent cyclic routing when forwarding under dispatcher events |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function forward(array $forward, bool $preventCycle = false): void |
61
|
|
|
{ |
62
|
|
|
$forward = $this->unsetForward($forward); |
63
|
|
|
|
64
|
|
|
if (!$preventCycle) { |
65
|
|
|
parent::forward($forward); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
elseif ($this->canForward($forward)) { |
69
|
|
|
parent::forward($forward); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function canForward(array $forward): bool |
74
|
|
|
{ |
75
|
|
|
$parts = [ |
76
|
|
|
'namespace' => $this->getNamespaceName(), |
77
|
|
|
'module' => $this->getModuleName(), |
78
|
|
|
'action' => $this->getActionName(), |
79
|
|
|
'params' => $this->getParams(), |
80
|
|
|
]; |
81
|
|
|
foreach ($parts as $part => $current) { |
82
|
|
|
if (isset($forward[$part]) && $current !== $forward[$part]) { |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($this instanceof MvcDispatcher && |
88
|
|
|
isset($forward['controller']) && |
89
|
|
|
$this->getControllerName() !== $forward['controller'] |
90
|
|
|
) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
elseif ($this instanceof CliDispatcher && |
95
|
|
|
isset($forward['task']) && |
96
|
|
|
$this->getTaskName() !== $forward['task'] |
97
|
|
|
) { |
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function unsetForward(array $forward, ?array $parts = null): array |
105
|
|
|
{ |
106
|
|
|
$parts ??= [ |
107
|
|
|
'namespace', |
108
|
|
|
'module', |
109
|
|
|
'task', |
110
|
|
|
'controller', |
111
|
|
|
'action', |
112
|
|
|
'params', |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
foreach ($parts as $part) { |
116
|
|
|
if (is_null($forward[$part])) { |
117
|
|
|
unset($forward[$part]); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $forward; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function toArray(): array |
125
|
|
|
{ |
126
|
|
|
$ret = [ |
127
|
|
|
'namespace' => $this->getNamespaceName(), |
128
|
|
|
'module' => $this->getModuleName(), |
129
|
|
|
'action' => $this->getActionName(), |
130
|
|
|
'params' => $this->getParams(), |
131
|
|
|
'handlerClass' => $this->getHandlerClass(), |
132
|
|
|
'handlerSuffix' => $this->getHandlerSuffix(), |
133
|
|
|
'activeMethod' => $this->getActiveMethod(), |
134
|
|
|
'actionSuffix' => $this->getActionSuffix(), |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
if ($this instanceof MvcDispatcher) { |
138
|
|
|
$ret['controller'] = $this->getControllerName(); |
139
|
|
|
$ret['previousNamespace'] = $this->getPreviousNamespaceName(); |
140
|
|
|
$ret['previousController'] = $this->getPreviousControllerName(); |
141
|
|
|
$ret['previousAction'] = $this->getPreviousActionName(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ($this instanceof CliDispatcher) { |
145
|
|
|
$ret['task'] = $this->getTaskName(); |
146
|
|
|
$ret['taskSuffix'] = $this->getTaskSuffix(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $ret; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: