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