1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Dispatcher; |
12
|
|
|
|
13
|
|
|
use Phalcon\Mvc\Dispatcher as MvcDispatcher; |
14
|
|
|
use Phalcon\Cli\Dispatcher as CliDispatcher; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class DispatcherTrait |
18
|
|
|
* |
19
|
|
|
* @author Julien Turbide <[email protected]> |
20
|
|
|
* @copyright Zemit Team <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
* @version 1.0 |
24
|
|
|
* |
25
|
|
|
* @package Zemit\Dispatcher |
26
|
|
|
*/ |
27
|
|
|
trait DispatcherTrait |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @param $handler |
31
|
|
|
* @param string $actionMethod |
32
|
|
|
* @param array $params |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
|
|
public function callActionMethod($handler, string $actionMethod, array $params = []) |
36
|
|
|
{ |
37
|
|
|
return call_user_func_array( |
38
|
|
|
[$handler, $actionMethod], |
39
|
|
|
array_filter($params, 'is_int', ARRAY_FILTER_USE_KEY) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Extending forwarding event to prevent cyclic routing when forwarding under dispatcher events |
45
|
|
|
* - @TODO handle params and other possible route parameters too |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
* |
48
|
|
|
* @param array $route |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function forward(array $forward, $preventCycle = false): void |
53
|
|
|
{ |
54
|
|
|
if (!$preventCycle) { |
55
|
|
|
parent::forward($forward); |
56
|
|
|
} |
57
|
|
|
else { |
58
|
|
|
if ((!isset($forward['namespace']) || $this->getNamespaceName() !== $forward['namespace']) && |
|
|
|
|
59
|
|
|
(!isset($forward['module']) || $this->getModuleName() !== $forward['module']) && |
|
|
|
|
60
|
|
|
(!isset($forward['task']) || $this->getControllerName() !== $forward['task']) && |
|
|
|
|
61
|
|
|
(!isset($forward['controller']) || $this->getControllerName() !== $forward['controller']) && |
62
|
|
|
(!isset($forward['action']) || $this->getActionName() !== $forward['action']) && |
|
|
|
|
63
|
|
|
(!isset($forward['params']) || $this->getParams() !== $forward['params']) |
|
|
|
|
64
|
|
|
) { |
65
|
|
|
if (!isset($forward['namespace'])) { |
66
|
|
|
unset($forward['namespace']); |
67
|
|
|
} |
68
|
|
|
if (!isset($forward['module'])) { |
69
|
|
|
unset($forward['module']); |
70
|
|
|
} |
71
|
|
|
if (!isset($forward['task'])) { |
72
|
|
|
unset($forward['task']); |
73
|
|
|
} |
74
|
|
|
if (!isset($forward['controller'])) { |
75
|
|
|
unset($forward['controller']); |
76
|
|
|
} |
77
|
|
|
if (!isset($forward['action'])) { |
78
|
|
|
unset($forward['action']); |
79
|
|
|
} |
80
|
|
|
if (!isset($forward['params'])) { |
81
|
|
|
unset($forward['params']); |
82
|
|
|
} |
83
|
|
|
$this->forward($forward); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function toArray() |
92
|
|
|
{ |
93
|
|
|
$ret = [ |
94
|
|
|
'namespace' => $this->getNamespaceName(), |
95
|
|
|
'module' => $this->getModuleName(), |
96
|
|
|
'action' => $this->getActionName(), |
97
|
|
|
'params' => $this->getParams(), |
98
|
|
|
'handlerClass' => $this->getHandlerClass(), |
|
|
|
|
99
|
|
|
'handlerSuffix' => $this->getHandlerSuffix(), |
|
|
|
|
100
|
|
|
'activeMethod' => $this->getActiveMethod(), |
|
|
|
|
101
|
|
|
'actionSuffix' => $this->getActionSuffix(), |
|
|
|
|
102
|
|
|
]; |
103
|
|
|
if ($this instanceof MvcDispatcher) { |
104
|
|
|
$ret['controller'] = $this->getControllerName(); |
105
|
|
|
$ret['previousNamespace'] = $this->getPreviousNamespaceName(); |
106
|
|
|
$ret['previousController'] = $this->getPreviousControllerName(); |
107
|
|
|
$ret['previousAction'] = $this->getPreviousActionName(); |
108
|
|
|
} |
109
|
|
|
if ($this instanceof CliDispatcher) { |
110
|
|
|
$ret['task'] = $this->getTaskName(); |
111
|
|
|
$ret['taskSuffix'] = $this->getTaskSuffix(); |
112
|
|
|
} |
113
|
|
|
return $ret; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|