1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* |
5
|
|
|
* @author Donii Sergii <[email protected]> |
6
|
|
|
* Date: 10/25/17 |
7
|
|
|
* Time: 11:57 AM |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace sonrac\WAMP\Routers; |
11
|
|
|
|
12
|
|
|
use Thruway\Peer\RouterInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait RouterTrait. |
16
|
|
|
* Base router trait. |
17
|
|
|
*/ |
18
|
|
|
trait RouterTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Controllers list |
22
|
|
|
* |
23
|
|
|
* @var \sonrac\WAMP\Abstracts\WAMPControllerInterface[] |
24
|
|
|
* |
25
|
|
|
* @author Donii Sergii <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
protected $controllers; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Main router |
31
|
|
|
* |
32
|
|
|
* @var null|\Thruway\Peer\RouterInterface|\sonrac\WAMP\Routers\Router |
33
|
|
|
* |
34
|
|
|
* @author Donii Sergii <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
protected $router = null; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Route path prefix. |
40
|
|
|
* |
41
|
|
|
* @var string|null |
42
|
|
|
* |
43
|
|
|
* @author Donii Sergii <[email protected]> |
44
|
|
|
*/ |
45
|
|
|
private $prefix = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Controller namespace. |
49
|
|
|
* |
50
|
|
|
* @var string|null |
51
|
|
|
* |
52
|
|
|
* @author Donii Sergii <[email protected]> |
53
|
|
|
*/ |
54
|
|
|
private $groupControllerNamespace = null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Middleware list. |
58
|
|
|
* |
59
|
|
|
* @var null|array |
60
|
|
|
* |
61
|
|
|
* @author Donii Sergii <[email protected]> |
62
|
|
|
*/ |
63
|
|
|
private $middleware = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Group routes. |
67
|
|
|
* |
68
|
|
|
* @param array $config Group config |
69
|
|
|
* @param \Closure $runner Closure runner group |
70
|
|
|
* |
71
|
|
|
* @author Donii Sergii <[email protected]> |
72
|
|
|
*/ |
73
|
2 |
|
public function group(array $config, \Closure $runner) |
74
|
|
|
{ |
75
|
2 |
|
$middleware = isset($config['middleware']) ? explode('|', $config['middleware']) : []; |
76
|
2 |
|
$namespace = isset($config['namespace']) ? $config['namespace'] : 'App\Controllers\WAMP'; |
77
|
|
|
|
78
|
2 |
|
$this->groups[] = [ |
|
|
|
|
79
|
2 |
|
'middleware' => $middleware, |
80
|
2 |
|
'namespace' => $namespace, |
81
|
2 |
|
'prefix' => isset($config['prefix']) ? $config['prefix'] : '', |
82
|
2 |
|
'callback' => $runner, |
83
|
|
|
]; |
84
|
2 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get route groups |
88
|
|
|
* |
89
|
|
|
* @return null|\sonrac\WAMP\GroupsConfigInterface[] |
90
|
|
|
* |
91
|
|
|
* @author Donii Sergii <[email protected]> |
92
|
|
|
*/ |
93
|
1 |
|
public function getGroups() |
94
|
|
|
{ |
95
|
1 |
|
return $this->groups; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get router |
100
|
|
|
* |
101
|
|
|
* @return mixed|null|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router|\Thruway\Peer\RouterInterface |
102
|
|
|
* |
103
|
|
|
* @author Donii Sergii <[email protected]> |
104
|
|
|
*/ |
105
|
3 |
|
public function getRouter() |
106
|
|
|
{ |
107
|
3 |
|
return $this->router ?? $this->router = app()->wampRouter; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set router. |
112
|
|
|
* |
113
|
|
|
* @author Donii Sergii <[email protected]> |
114
|
|
|
*/ |
115
|
2 |
|
public function setRouter(RouterInterface $router) |
116
|
|
|
{ |
117
|
2 |
|
$this->router = $router; |
118
|
2 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get client session. |
122
|
|
|
* |
123
|
|
|
* @return \Thruway\ClientSession |
124
|
|
|
* |
125
|
|
|
* @author Donii Sergii <[email protected]> |
126
|
|
|
*/ |
127
|
2 |
|
public function getClientSession() |
128
|
|
|
{ |
129
|
2 |
|
return $this->getRouter()->getClient()->getSession(); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get client. |
134
|
|
|
* |
135
|
|
|
* @return \sonrac\WAMP\Client|\Thruway\Peer\ClientInterface |
136
|
|
|
* |
137
|
|
|
* @author Donii Sergii <[email protected]> |
138
|
|
|
*/ |
139
|
1 |
|
public function getClient() |
140
|
|
|
{ |
141
|
1 |
|
return $this->getRouter()->getClient(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Parse callback function |
146
|
|
|
* |
147
|
|
|
* @param string|\Closure $callback Callback |
148
|
|
|
* @param null $namespace Controller namespace |
|
|
|
|
149
|
|
|
* |
150
|
|
|
* @return \Closure |
151
|
|
|
* |
152
|
|
|
* @author Donii Sergii <[email protected]> |
153
|
|
|
*/ |
154
|
4 |
|
public function parseCallback($callback, $namespace = null) |
155
|
|
|
{ |
156
|
4 |
|
if ($callback instanceof \Closure) { |
157
|
2 |
|
return $callback; |
158
|
|
|
} |
159
|
|
|
|
160
|
3 |
|
$namespace = $namespace ? $namespace.'\\' : ''; |
|
|
|
|
161
|
|
|
|
162
|
3 |
|
$callback = explode('@', $callback); |
163
|
|
|
|
164
|
3 |
|
return function () use ($callback, $namespace) { |
165
|
1 |
|
if (count($callback) === 1) { |
166
|
|
|
return app()->call([$this, $callback[0]], func_get_args()); |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
if (isset($this->controllers[$callback[0]])) { |
170
|
|
|
return call_user_func_array([$this->controllers[$callback[0]], $callback[1]], func_get_args()); |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
$namespace = rtrim($namespace, '\\'); |
174
|
|
|
|
175
|
1 |
|
$className = class_exists($callback[0]) ? $callback[0] : $namespace.'\\'.$callback[0]; |
176
|
|
|
|
177
|
1 |
|
$this->controllers[$callback[0]] = app()->make($className); |
178
|
|
|
|
179
|
1 |
|
return app()->call([$this->controllers[$callback[0]], $callback[1]], func_get_args()); |
180
|
3 |
|
}; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* get controller namespace for group |
185
|
|
|
* |
186
|
|
|
* @return null|string |
187
|
|
|
* |
188
|
|
|
* @author Donii Sergii <[email protected]> |
189
|
|
|
*/ |
190
|
1 |
|
public function getGroupControllerNamespace() |
191
|
|
|
{ |
192
|
1 |
|
return $this->groupControllerNamespace; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
3 |
|
public function parseGroups() |
199
|
|
|
{ |
200
|
3 |
|
if (!is_array($this->groups) || !count($this->groups)) { |
201
|
2 |
|
return; |
202
|
|
|
} |
203
|
1 |
|
gc_enable(); |
204
|
1 |
|
$callbacks = []; |
205
|
1 |
|
foreach ($this->groups as $group) { |
206
|
1 |
|
$this->prefix = $group['prefix']; |
207
|
1 |
|
$this->groupControllerNamespace = $group['namespace']; |
208
|
1 |
|
$this->middleware = $group['middleware']; |
209
|
1 |
|
$callbacks[] = $group['callback']($this->getClientSession(), $this->getClient()); |
210
|
|
|
} |
211
|
|
|
|
212
|
1 |
|
$this->groups = null; |
|
|
|
|
213
|
1 |
|
unset($this->groups); |
214
|
1 |
|
$this->groups = []; |
215
|
|
|
|
216
|
1 |
|
$this->prefix = null; |
217
|
1 |
|
$this->groupControllerNamespace = null; |
218
|
|
|
|
219
|
1 |
|
gc_collect_cycles(); |
220
|
1 |
|
gc_disable(); |
221
|
|
|
|
222
|
1 |
|
return $callbacks; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Prepare path. |
227
|
|
|
* |
228
|
|
|
* @param \Closure|string $callback Callback |
229
|
|
|
* |
230
|
|
|
* @return array |
231
|
|
|
* |
232
|
|
|
* @author Donii Sergii <[email protected]> |
233
|
|
|
*/ |
234
|
1 |
|
protected function prepareCallback($callback) |
235
|
|
|
{ |
236
|
1 |
|
$namespace = $this->groupControllerNamespace ?? $this->getRouter()->getGroupControllerNamespace(); |
|
|
|
|
237
|
1 |
|
$namespace = $namespace ?? $this->getRouter()->getControllerNamespace(); |
|
|
|
|
238
|
|
|
|
239
|
|
|
return [ |
240
|
1 |
|
'prefix' => $this->prefix, |
241
|
1 |
|
'namespace' => $namespace, |
242
|
1 |
|
'callback' => $this->parseCallback($callback, $namespace), |
|
|
|
|
243
|
1 |
|
'middleware' => $this->middleware, |
244
|
|
|
]; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|