1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Service; |
3
|
|
|
|
4
|
|
|
use Mezon\Security\ProviderInterface; |
5
|
|
|
use Mezon\Service\ServiceRestTransport\ServiceRestTransport; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Service |
9
|
|
|
* |
10
|
|
|
* @package Mezon |
11
|
|
|
* @subpackage ServiceBase |
12
|
|
|
* @author Dodonov A.A. |
13
|
|
|
* @version v.1.0 (2019/12/09) |
14
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base service class |
19
|
|
|
* |
20
|
|
|
* It bounds together transport, request parameters fetcher, logic, authorization and model |
21
|
|
|
* |
22
|
|
|
* @author Dodonov A.A. |
23
|
|
|
*/ |
24
|
|
|
class ServiceBase |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Service's ransport |
29
|
|
|
* |
30
|
|
|
* @var object Service transport object |
31
|
|
|
*/ |
32
|
|
|
private $serviceTransport = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Service's logic |
36
|
|
|
* |
37
|
|
|
* @var array Login object or list of logic objects |
38
|
|
|
*/ |
39
|
|
|
private $serviceLogic = false; |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param ServiceBaseLogic $serviceLogic |
45
|
|
|
* service's logic |
46
|
|
|
* @param ServiceModel $serviceModel |
47
|
|
|
* service's model |
48
|
|
|
* @param ProviderInterface $securityProvider |
49
|
|
|
* service's security provider |
50
|
|
|
* @param TransportInterface $serviceTransport |
51
|
|
|
* service's transport |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
ServiceBaseLogic $serviceLogic, |
55
|
|
|
ServiceModel $serviceModel, |
56
|
|
|
ProviderInterface $securityProvider, |
57
|
|
|
TransportInterface $serviceTransport) |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
$this->initTransport($serviceTransport, $securityProvider); |
61
|
|
|
|
62
|
|
|
$this->initServiceLogic($serviceLogic, $serviceModel); |
63
|
|
|
|
64
|
|
|
$this->initCustomRoutes(); |
65
|
|
|
|
66
|
|
|
$this->fetchActions(); |
67
|
|
|
} catch (\Exception $e) { |
68
|
|
|
$this->getTransport()->handleException($e); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Method fetches actions if they are existing |
74
|
|
|
*/ |
75
|
|
|
protected function fetchActions(): void |
76
|
|
|
{ |
77
|
|
|
if ($this instanceof ServiceBaseLogicInterface) { |
78
|
|
|
$this->serviceTransport->fetchActions($this); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
foreach ($this->serviceLogics as $actionsSet) { |
82
|
|
|
if ($actionsSet instanceof ServiceBaseLogicInterface) { |
83
|
|
|
$this->serviceTransport->fetchActions($actionsSet); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Method inits service's transport |
90
|
|
|
* |
91
|
|
|
* @param mixed $serviceTransport |
92
|
|
|
* Service's transport |
93
|
|
|
* @param mixed $securityProvider |
94
|
|
|
* Service's security provider |
95
|
|
|
*/ |
96
|
|
|
protected function initTransport($serviceTransport, $securityProvider): void |
97
|
|
|
{ |
98
|
|
|
if (is_string($serviceTransport)) { |
99
|
|
|
$this->serviceTransport = new $serviceTransport($securityProvider); |
100
|
|
|
} else { |
101
|
|
|
$this->serviceTransport = $serviceTransport; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Method constructs service logic if necessary |
107
|
|
|
* |
108
|
|
|
* @param mixed $serviceLogic |
109
|
|
|
* Service logic class name of object itself |
110
|
|
|
* @param mixed $serviceModel |
111
|
|
|
* Service model class name of object itself |
112
|
|
|
* @return ServiceLogic logic object |
113
|
|
|
*/ |
114
|
|
|
protected function constructServiceLogic($serviceLogic, $serviceModel) |
115
|
|
|
{ |
116
|
|
|
if (is_string($serviceLogic)) { |
117
|
|
|
$result = new $serviceLogic( |
118
|
|
|
$this->serviceTransport->getParamsFetcher(), |
119
|
|
|
$this->serviceTransport->getSecurityProvider(), |
120
|
|
|
$serviceModel); |
121
|
|
|
} else { |
122
|
|
|
$result = $serviceLogic; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Method inits service's logic |
130
|
|
|
* |
131
|
|
|
* @param mixed $serviceLogic |
132
|
|
|
* Service's logic |
133
|
|
|
* @param mixed $serviceModel |
134
|
|
|
* Service's Model |
135
|
|
|
*/ |
136
|
|
|
protected function initServiceLogic($serviceLogic, $serviceModel): void |
137
|
|
|
{ |
138
|
|
|
if (is_array($serviceLogic)) { |
139
|
|
|
$this->serviceLogics = []; |
|
|
|
|
140
|
|
|
|
141
|
|
|
foreach ($serviceLogic as $logic) { |
142
|
|
|
$this->serviceLogics[] = $this->constructServiceLogic($logic, $serviceModel); |
143
|
|
|
} |
144
|
|
|
} else { |
145
|
|
|
$this->serviceLogics = [ |
146
|
|
|
$this->constructServiceLogic($serviceLogic, $serviceModel) |
147
|
|
|
]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->serviceTransport->setServiceLogics($this->serviceLogics); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Method inits custom routes if necessary |
155
|
|
|
*/ |
156
|
|
|
protected function initCustomRoutes(): void |
157
|
|
|
{ |
158
|
|
|
$reflector = new \ReflectionClass(get_class($this)); |
159
|
|
|
$classPath = dirname($reflector->getFileName()); |
160
|
|
|
|
161
|
|
|
// TODO make /Conf/... |
162
|
|
|
if (file_exists($classPath . '/conf/routes.php')) { |
163
|
|
|
$this->serviceTransport->loadRoutesFromConfig($classPath . '/conf/routes.php'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (file_exists($classPath . '/conf/routes.json')) { |
167
|
|
|
$this->serviceTransport->loadRoutes(json_decode(file_get_contents($classPath . '/conf/routes.json'), true)); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Running $this->serviceTransport run loop |
173
|
|
|
*/ |
174
|
|
|
public function run(): void |
175
|
|
|
{ |
176
|
|
|
$this->serviceTransport->run(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Method sets transport |
181
|
|
|
* |
182
|
|
|
* @param Transport $transport |
183
|
|
|
*/ |
184
|
|
|
public function setTransport(Transport $transport): void |
185
|
|
|
{ |
186
|
|
|
$this->serviceTransport = $transport; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Method returns transport |
191
|
|
|
* |
192
|
|
|
* @return Transport |
193
|
|
|
*/ |
194
|
|
|
public function getTransport(): Transport |
195
|
|
|
{ |
196
|
|
|
return $this->serviceTransport; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Method returns logic objects |
201
|
|
|
* |
202
|
|
|
* @return ServiceLogic[] |
203
|
|
|
*/ |
204
|
|
|
public function getLogics() |
205
|
|
|
{ |
206
|
|
|
return $this->serviceLogics; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|