|
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\ClientSession; |
|
13
|
|
|
use Thruway\Peer\RouterInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Trait RouterTrait. |
|
17
|
|
|
* Base router trait. |
|
18
|
|
|
*/ |
|
19
|
|
|
trait RouterTrait |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Router groups. |
|
23
|
|
|
* |
|
24
|
|
|
* @var null|\sonrac\WAMP\GroupsConfigInterface[] |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $groups = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Main router |
|
30
|
|
|
* |
|
31
|
|
|
* @var null|\Thruway\Peer\RouterInterface|\sonrac\WAMP\Routers\Router |
|
32
|
|
|
* |
|
33
|
|
|
* @author Donii Sergii <[email protected]> |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $router = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Group routes |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $config Group config |
|
41
|
|
|
* @param \Closure $runner Closure runner group |
|
42
|
|
|
* |
|
43
|
|
|
* @author Donii Sergii <[email protected]> |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function group(array $config, \Closure $runner) |
|
46
|
|
|
{ |
|
47
|
1 |
|
$middleware = isset($config['middleware']) ? explode('|', $config['middleware']) : []; |
|
48
|
1 |
|
$namespace = isset($config['namespace']) ? $config['namespace'] : 'App\Controllers\WAMP'; |
|
49
|
|
|
|
|
50
|
1 |
|
$this->groups[] = (object)[ |
|
51
|
1 |
|
'middleware' => $middleware, |
|
52
|
1 |
|
'namespace' => $namespace, |
|
53
|
1 |
|
'prefix' => isset($config['prefix']) ? $config['prefix'] : '', |
|
54
|
1 |
|
'callback' => $runner, |
|
55
|
|
|
]; |
|
56
|
1 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get route groups |
|
60
|
|
|
* |
|
61
|
|
|
* @return null|\sonrac\WAMP\GroupsConfigInterface[] |
|
62
|
|
|
* |
|
63
|
|
|
* @author Donii Sergii <[email protected]> |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function getGroups() { |
|
66
|
1 |
|
return $this->groups; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get router |
|
71
|
|
|
* |
|
72
|
|
|
* @return mixed|null|\sonrac\WAMP\Contracts\WAMPRouterInterface|\sonrac\WAMP\Routers\Router|\Thruway\Peer\RouterInterface |
|
73
|
|
|
* |
|
74
|
|
|
* @author Donii Sergii <[email protected]> |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function getRouter() |
|
77
|
|
|
{ |
|
78
|
2 |
|
return $this->router ?? $this->router = app()->wampRouter; |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Set router. |
|
83
|
|
|
* |
|
84
|
|
|
* @author Donii Sergii <[email protected]> |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function setRouter(RouterInterface $router) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->router = $router; |
|
89
|
1 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get client session. |
|
93
|
|
|
* |
|
94
|
|
|
* @return \Thruway\ClientSession |
|
95
|
|
|
* |
|
96
|
|
|
* @author Donii Sergii <[email protected]> |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function getClientSession() |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->getRouter()->getClient()->getSession(); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get client. |
|
105
|
|
|
* |
|
106
|
|
|
* @return \sonrac\WAMP\Client|\Thruway\Peer\ClientInterface |
|
107
|
|
|
* |
|
108
|
|
|
* @author Donii Sergii <[email protected]> |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getClient() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->getRouter()->getClient(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Parse callback function |
|
117
|
|
|
* |
|
118
|
|
|
* @param string|\Closure $callback Callback |
|
119
|
|
|
* @param null $namespace Controller namespace |
|
|
|
|
|
|
120
|
|
|
* |
|
121
|
|
|
* @return \Closure |
|
122
|
|
|
* |
|
123
|
|
|
* @author Donii Sergii <[email protected]> |
|
124
|
|
|
*/ |
|
125
|
1 |
|
protected function parseCallback($callback, $namespace = null) |
|
126
|
|
|
{ |
|
127
|
1 |
|
if ($callback instanceof \Closure) { |
|
128
|
1 |
|
return $callback; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$namespace = $namespace ? $namespace . '\\' : ''; |
|
132
|
|
|
|
|
133
|
|
|
$callback = explode('&', $callback); |
|
134
|
|
|
$self = $this; |
|
135
|
|
|
return function (ClientSession $clientSession) use ($callback, $namespace, $self) { |
|
136
|
|
|
if (count($callback) === 1) { |
|
137
|
|
|
return $this->{$callback[0]}($clientSession, $self->getClient()); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$class = app()->make($namespace . $callback[0]); |
|
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
return $class->{$callback[1]}($clientSession, $self->getClient()); |
|
143
|
|
|
}; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|