1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2014 Krzysztof Magosa |
4
|
|
|
* |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
* |
10
|
|
|
* Unless required by applicable law or agreed to in writing, software |
11
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
* See the License for the specific language governing permissions and |
14
|
|
|
* limitations under the License. |
15
|
|
|
*/ |
16
|
|
|
namespace KM\Saffron; |
17
|
|
|
|
18
|
|
|
use KM\Saffron\RoutingResult; |
19
|
|
|
use KM\Saffron\Exception\InvalidArgument; |
20
|
|
|
|
21
|
|
|
class Executor |
22
|
|
|
{ |
23
|
|
|
protected $controller; |
24
|
|
|
protected $method; |
25
|
|
|
protected $parameters = []; |
26
|
|
|
protected $preDispatch; |
27
|
|
|
protected $postDispatch; |
28
|
|
|
|
29
|
|
|
public function __construct(RoutingResult $result = null) |
30
|
|
|
{ |
31
|
|
|
if ($result) { |
32
|
|
|
if (!$result->isSuccessful()) { |
33
|
|
|
throw new InvalidArgument( |
34
|
|
|
'You cannot use unsuccessful RoutingResult to init Executor.' |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this |
39
|
|
|
->setController($result->getTarget()[0]) |
40
|
|
|
->setMethod($result->getTarget()[1]) |
41
|
|
|
->setParameters($result->getParameters()); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Sets controller to be fired. |
47
|
|
|
* It can be object or name of class. |
48
|
|
|
* |
49
|
|
|
* @param object|string $controller |
50
|
|
|
* @return Executor |
51
|
|
|
*/ |
52
|
|
|
public function setController($controller) |
53
|
|
|
{ |
54
|
|
|
if (is_string($controller)) { |
55
|
|
|
$this->controller = new $controller(); |
56
|
|
|
} else { |
57
|
|
|
$this->controller = $controller; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns controller object |
65
|
|
|
* |
66
|
|
|
* @return object |
67
|
|
|
*/ |
68
|
|
|
public function getController() |
69
|
|
|
{ |
70
|
|
|
return $this->controller; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Sets method to be fired |
75
|
|
|
* |
76
|
|
|
* @param string $method |
77
|
|
|
* @return Executor |
78
|
|
|
*/ |
79
|
|
|
public function setMethod($method) |
80
|
|
|
{ |
81
|
|
|
$this->method = $method; |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Sets parameters to be passed to controller |
87
|
|
|
* |
88
|
|
|
* @param array $parameters |
89
|
|
|
* @return Executor |
90
|
|
|
*/ |
91
|
|
|
public function setParameters(array $parameters) |
92
|
|
|
{ |
93
|
|
|
$this->parameters = $parameters; |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Sets callable to be called before firing controller |
99
|
|
|
* |
100
|
|
|
* @param callable $func |
101
|
|
|
* @return Executor |
102
|
|
|
*/ |
103
|
|
|
public function setPreDispatch(callable $func) |
104
|
|
|
{ |
105
|
|
|
$this->preDispatch = $func; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sets callable to be called after firing controller |
111
|
|
|
* |
112
|
|
|
* @param callable $func |
113
|
|
|
* @return Executor |
114
|
|
|
*/ |
115
|
|
|
public function setPostDispatch(callable $func) |
116
|
|
|
{ |
117
|
|
|
$this->postDispatch = $func; |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Calls hook if it's callable |
123
|
|
|
* |
124
|
|
|
* @param callable|null $hook Hook to be fired |
125
|
|
|
*/ |
126
|
|
|
protected function runHook($hook) |
127
|
|
|
{ |
128
|
|
|
if ($hook && is_callable($hook)) { |
129
|
|
|
call_user_func( |
130
|
|
|
$hook, |
131
|
|
|
$this->controller, |
132
|
|
|
$this->method, |
133
|
|
|
$this->parameters |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Runs: |
140
|
|
|
* - preDispatch (if set) |
141
|
|
|
* - method in controller |
142
|
|
|
* - postDispatch (if set) |
143
|
|
|
* |
144
|
|
|
* @return mixed Value returned by controller's method |
145
|
|
|
*/ |
146
|
|
|
public function fire() |
147
|
|
|
{ |
148
|
|
|
$this->runHook($this->preDispatch); |
149
|
|
|
|
150
|
|
|
$method = new \ReflectionMethod($this->controller, $this->method); |
151
|
|
|
$arguments = []; |
152
|
|
|
foreach ($method->getParameters() as $parameter) { |
153
|
|
|
// https://bugs.php.net/bug.php?id=61384 |
154
|
|
|
$name = $parameter->name; |
155
|
|
|
$arguments[] = isset($this->parameters[$name]) ? $this->parameters[$name] : null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$result = $method->invokeArgs($this->controller, $arguments); |
159
|
|
|
|
160
|
|
|
$this->runHook($this->postDispatch); |
161
|
|
|
|
162
|
|
|
return $result; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|