1
|
|
|
<?php |
2
|
|
|
namespace agoalofalife\bpm; |
3
|
|
|
|
4
|
|
|
use agoalofalife\bpm\Actions\Create; |
5
|
|
|
use agoalofalife\bpm\Actions\Delete; |
6
|
|
|
use agoalofalife\bpm\Actions\Read; |
7
|
|
|
use agoalofalife\bpm\Actions\Update; |
8
|
|
|
use agoalofalife\bpm\Contracts\Action; |
9
|
|
|
use agoalofalife\bpm\Contracts\Authentication; |
10
|
|
|
use agoalofalife\bpm\Contracts\Handler; |
11
|
|
|
use agoalofalife\bpm\Contracts\SourceConfiguration; |
12
|
|
|
use agoalofalife\bpm\Handlers\JsonHandler; |
13
|
|
|
use agoalofalife\bpm\Handlers\XmlHandler; |
14
|
|
|
use agoalofalife\bpm\ServiceProviders\ActionsServiceProviders; |
15
|
|
|
use agoalofalife\bpm\ServiceProviders\AuthenticationServiceProvider; |
16
|
|
|
use agoalofalife\bpm\ServiceProviders\ClientServiceProvider; |
17
|
|
|
use agoalofalife\bpm\ServiceProviders\ConfigurationServiceProvider; |
18
|
|
|
use agoalofalife\bpm\ServiceProviders\LoggerServiceProvider; |
19
|
|
|
use Assert\Assertion; |
20
|
|
|
use Assert\AssertionFailedException; |
21
|
|
|
use GuzzleHttp\ClientInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class KernelBpm |
25
|
|
|
* @const string PATH_LOG |
26
|
|
|
* @property array $action list action |
27
|
|
|
* @property array $handlers list handler xml | json |
28
|
|
|
* @property string $collection Name collection in Bpm |
29
|
|
|
* @property Action $currentAction current action which use in client code |
30
|
|
|
* @property Handler $currentHandler current handler which use in client code |
31
|
|
|
* @property string $url |
32
|
|
|
* @property string $prefixConfiguration prefix for set and search in Repository class |
33
|
|
|
* @property array $serviceProviders list ServiceProvider for bootstrap |
34
|
|
|
* |
35
|
|
|
* @package agoalofalife\bpm |
36
|
|
|
*/ |
37
|
|
|
class KernelBpm |
38
|
|
|
{ |
39
|
|
|
const PATH_LOG = __DIR__ . '/resource/logs/'; |
40
|
|
|
|
41
|
|
|
protected $action = [ |
42
|
|
|
'create' => Create::class, |
43
|
|
|
'read' => Read::class, |
44
|
|
|
'update' => Update::class, |
45
|
|
|
'delete' => Delete::class, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
protected $handlers = [ |
49
|
|
|
'xml' => XmlHandler::class, |
50
|
|
|
'json' => JsonHandler::class, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
protected $collection; |
54
|
|
|
protected $currentAction; |
55
|
|
|
protected $currentHandler; |
56
|
|
|
protected $url; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* prefix name configuration |
60
|
|
|
*/ |
61
|
|
|
protected $prefixConfiguration; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* list providers for pre bootstrapping packages |
65
|
|
|
*/ |
66
|
|
|
protected $serviceProviders = [ |
67
|
|
|
ConfigurationServiceProvider::class, |
68
|
|
|
ActionsServiceProviders::class, |
69
|
|
|
AuthenticationServiceProvider::class, |
70
|
|
|
ClientServiceProvider::class, |
71
|
|
|
LoggerServiceProvider::class |
72
|
|
|
]; |
73
|
|
|
|
74
|
22 |
|
public function __construct() |
75
|
|
|
{ |
76
|
22 |
|
$this->bootstrapping(); |
77
|
22 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Auth in Bpm online |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
1 |
|
public function authentication() |
84
|
|
|
{ |
85
|
1 |
|
$auth = app()->make(Authentication::class); |
86
|
1 |
|
$auth->setConfig(config($this->prefixConfiguration)); |
87
|
1 |
|
$auth->auth(); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get list actions |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
2 |
|
public function getListActions() |
95
|
|
|
{ |
96
|
2 |
|
return $this->action; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
1 |
|
public function getPrefixConfig() |
103
|
|
|
{ |
104
|
1 |
|
return $this->prefixConfiguration; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param SourceConfiguration $configuration |
109
|
|
|
*/ |
110
|
1 |
|
public function loadConfiguration(SourceConfiguration $configuration) |
111
|
|
|
{ |
112
|
1 |
|
config()->set( $this->prefixConfiguration = $configuration->getName(), $configuration->get()); |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param $key |
117
|
|
|
* @param array $array |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
3 |
|
public function setConfigManually($key, array $array) |
121
|
|
|
{ |
122
|
3 |
|
$this->prefixConfiguration = $key; |
123
|
3 |
|
config()->set($key, $array); |
124
|
3 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return Handler |
128
|
|
|
*/ |
129
|
2 |
|
public function getHandler() |
130
|
|
|
{ |
131
|
2 |
|
return $this->currentHandler; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set the response handler |
136
|
|
|
* @param string $typeHandler default xml |
137
|
|
|
* @return Action |
138
|
|
|
*/ |
139
|
5 |
|
public function setHandler($typeHandler = 'xml') |
140
|
|
|
{ |
141
|
5 |
|
Assertion::keyIsset($this->handlers, $typeHandler); |
142
|
4 |
|
$this->currentHandler = app($this->handlers[$typeHandler]); |
143
|
4 |
|
return $this->currentHandler; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return Action |
148
|
|
|
*/ |
149
|
1 |
|
public function getAction() |
150
|
|
|
{ |
151
|
1 |
|
return $this->currentAction; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param $typeAction string |
156
|
|
|
* @return Action |
157
|
|
|
*/ |
158
|
5 |
|
public function setAction($typeAction) |
159
|
|
|
{ |
160
|
5 |
|
Assertion::keyIsset($this->action, $typeAction); |
161
|
|
|
|
162
|
4 |
|
$this->currentAction = app()->make( $this->action[$typeAction] ); |
163
|
|
|
|
164
|
4 |
|
return $this->currentAction; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Example action parameter 'read:json' |
169
|
|
|
* @param string $action |
170
|
|
|
* @param callable $callback |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
1 |
|
public function action($action, callable $callback) |
174
|
|
|
{ |
175
|
|
|
|
176
|
1 |
|
extract($this->splitAction($action)); |
|
|
|
|
177
|
|
|
|
178
|
1 |
|
$action = $this->setAction($action); |
179
|
1 |
|
$this->setHandler($handler); |
180
|
|
|
|
181
|
1 |
|
$action->injectionKernel($this); |
182
|
1 |
|
call_user_func($callback, $action); |
183
|
1 |
|
$this->currentAction = $action; |
184
|
|
|
|
185
|
1 |
|
return $this; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return array url -> string , http_type -> string |
190
|
|
|
* |
191
|
|
|
*/ |
192
|
1 |
|
public function get() |
193
|
|
|
{ |
194
|
|
|
// here query in BPM |
195
|
1 |
|
return $this->currentAction->processData(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Set collection for correct query |
200
|
|
|
* @param string $collection |
201
|
|
|
* @return mixed |
202
|
|
|
* @throws \Exception |
203
|
|
|
*/ |
204
|
4 |
|
public function setCollection($collection) |
205
|
|
|
{ |
206
|
|
|
try { |
207
|
4 |
|
Assertion::regex($collection, '/[A-z]+Collection$/'); |
208
|
4 |
|
} catch(AssertionFailedException $e) { |
209
|
1 |
|
throw new \Exception("Expected word 'Collection' in parameter method setCollection received : " . $e->getValue()); |
210
|
|
|
} |
211
|
|
|
|
212
|
3 |
|
return $this->collection = $collection; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
1 |
|
public function getCollection() |
219
|
|
|
{ |
220
|
1 |
|
return $this->collection; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return ClientInterface |
225
|
|
|
*/ |
226
|
1 |
|
public function getCurl() |
227
|
|
|
{ |
228
|
1 |
|
return app()->make(ClientInterface::class); |
229
|
|
|
} |
230
|
|
|
/** |
231
|
|
|
* @param $action |
232
|
|
|
* @return array |
233
|
|
|
*/ |
234
|
1 |
|
private function splitAction($action) |
235
|
|
|
{ |
236
|
1 |
|
$split = explode(':', $action); |
237
|
|
|
|
238
|
|
|
// verification values |
239
|
1 |
|
Assertion::between(count($split), 2, 2); |
240
|
1 |
|
Assertion::keyExists( $this->action, $split[0]); |
241
|
1 |
|
Assertion::keyExists( $this->handlers, $split[1]); |
242
|
|
|
|
243
|
1 |
|
return ['action' => $split[0], 'handler' => $split[1]]; |
244
|
|
|
} |
245
|
|
|
|
246
|
22 |
|
private function bootstrapping() |
247
|
|
|
{ |
248
|
22 |
|
foreach ($this->serviceProviders as $provider) |
249
|
|
|
{ |
250
|
22 |
|
(new $provider)->register(); |
251
|
22 |
|
} |
252
|
|
|
} |
253
|
|
|
} |