1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Controllers\Traits; |
4
|
|
|
|
5
|
|
|
use Nip\Container\Container; |
6
|
|
|
use Nip\Controllers\Controller; |
7
|
|
|
use Nip\Dispatcher\Dispatcher; |
8
|
|
|
use Nip\Request; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait DispatcherAwareTrait |
12
|
|
|
* @package Nip\Controllers\Traits |
13
|
|
|
*/ |
14
|
|
|
trait DispatcherAwareTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @return mixed |
18
|
|
|
* @throws \Exception |
19
|
|
|
*/ |
20
|
2 |
|
public function call() |
21
|
|
|
{ |
22
|
2 |
|
$arguments = func_get_args(); |
23
|
2 |
|
if (count($arguments) >= 3) { |
24
|
|
|
return $this->callMCA(...$arguments); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
2 |
|
if (count($arguments) == 2 && is_array($arguments[1])) { |
28
|
1 |
|
if (is_string($arguments[0])) { |
29
|
1 |
|
return $this->{$arguments[0]}(...$arguments[1]); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
if (count($arguments) == 1) { |
34
|
1 |
|
return $this->{$arguments[0]}(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
throw new \Exception("Controller call method invoked with invalid parameters"); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param bool $action |
42
|
|
|
* @param bool $controller |
43
|
|
|
* @param bool $module |
44
|
|
|
* @param array $params |
45
|
|
|
* @return mixed |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
|
|
protected function callMCA($action = false, $controller = false, $module = false, $params = []) |
49
|
|
|
{ |
50
|
|
|
/** @var Request $newRequest */ |
51
|
|
|
$newRequest = $this->getRequest()->duplicateWithParams($action, $controller, $module, $params); |
|
|
|
|
52
|
|
|
|
53
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
54
|
|
|
return $this->getDispatcher()->callFromRequest($newRequest, $params); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param bool $action |
59
|
|
|
* @param bool $controller |
60
|
|
|
* @param bool $module |
61
|
|
|
* @param array $params |
62
|
|
|
* @throws \Nip\Dispatcher\Exceptions\ForwardException |
63
|
|
|
*/ |
64
|
|
|
protected function forward($action = false, $controller = false, $module = false, $params = []) |
65
|
|
|
{ |
66
|
|
|
$this->getDispatcher()->forward($action, $controller, $module, $params); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return Dispatcher |
71
|
|
|
*/ |
72
|
|
|
protected function getDispatcher() |
73
|
|
|
{ |
74
|
|
|
if (function_exists('app')) { |
75
|
|
|
return app('dispatcher'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return Container::getInstance()->get('dispatcher'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|