|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\core\installer; |
|
3
|
|
|
|
|
4
|
|
|
use Composer\IO\IOInterface; |
|
5
|
|
|
use gossi\swagger\Swagger; |
|
6
|
|
|
use keeko\core\events\ModuleEvent; |
|
7
|
|
|
use keeko\core\model\Action; |
|
8
|
|
|
use keeko\core\model\ActionQuery; |
|
9
|
|
|
use keeko\core\model\Api; |
|
10
|
|
|
use keeko\core\model\ApiQuery; |
|
11
|
|
|
use keeko\core\model\Group; |
|
12
|
|
|
use keeko\core\model\GroupQuery; |
|
13
|
|
|
use keeko\core\model\Module; |
|
14
|
|
|
use keeko\core\model\ModuleQuery; |
|
15
|
|
|
use keeko\core\package\ModuleManager; |
|
16
|
|
|
use keeko\core\schema\ModuleSchema; |
|
17
|
|
|
|
|
18
|
|
|
class ModuleInstaller extends AbstractPackageInstaller { |
|
19
|
|
|
|
|
20
|
|
|
/** @var ModuleManager */ |
|
21
|
|
|
private $manager; |
|
22
|
|
|
|
|
23
|
|
|
/** @var Group */ |
|
24
|
|
|
private $guestGroup; |
|
25
|
|
|
|
|
26
|
|
|
/** @var Group */ |
|
27
|
|
|
private $userGroup; |
|
28
|
|
|
|
|
29
|
|
|
/** @var Group */ |
|
30
|
|
|
private $adminGroup; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct() { |
|
33
|
|
|
parent::__construct(); |
|
34
|
|
|
$this->manager = $this->service->getModuleManager(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function install(IOInterface $io, $packageName) { |
|
38
|
|
|
$io->write('[Keeko] Install Module: ' . $packageName); |
|
39
|
|
|
|
|
40
|
|
|
$package = $this->getPackageSchema($packageName); |
|
41
|
|
|
$keeko = $package->getKeeko(); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
if ($keeko->isModule()) { |
|
44
|
|
|
$pkg = $keeko->getModule(); |
|
45
|
|
|
|
|
46
|
|
|
// create module |
|
47
|
|
|
$model = new Module(); |
|
48
|
|
|
$model->setClassName($pkg->getClass()); |
|
49
|
|
|
$model->setSlug($pkg->getSlug()); |
|
50
|
|
|
$this->updatePackage($model, $pkg); |
|
51
|
|
|
|
|
52
|
|
|
// run module -> install |
|
53
|
|
|
$className = $pkg->getClass(); |
|
54
|
|
|
$class = new $className($model, $this->service); |
|
|
|
|
|
|
55
|
|
|
$class->install(); |
|
56
|
|
|
|
|
57
|
|
|
$this->dispatcher->dispatch(ModuleEvent::INSTALLED, new ModuleEvent($model)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function update(IOInterface $io, $packageName, $from, $to) { |
|
62
|
|
|
$io->write(sprintf('[Keeko] Update Module: %s from %s to %s', $packageName, $from, $to)); |
|
63
|
|
|
|
|
64
|
|
|
// retrieve module |
|
65
|
|
|
$model = ModuleQuery::create()->findOneByName($packageName); |
|
66
|
|
|
$this->updatePackage($model, $packageName); |
|
67
|
|
|
|
|
68
|
|
|
// run module -> update |
|
69
|
|
|
$className = $model->getClass(); |
|
70
|
|
|
$class = new $className($model, $this->service); |
|
|
|
|
|
|
71
|
|
|
$class->update($from, $to); |
|
72
|
|
|
|
|
73
|
|
|
// update api and actions |
|
74
|
|
|
if ($this->manager->isActivated($packageName)) { |
|
75
|
|
|
$this->updateModule($model); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->dispatcher->dispatch(ModuleEvent::UPDATED, new ModuleEvent($model)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function uninstall(IOInterface $io, $packageName) { |
|
82
|
|
|
$io->write('[Keeko] Uninstall Module: ' . $packageName); |
|
83
|
|
|
|
|
84
|
|
|
// retrieve module |
|
85
|
|
|
$model = ModuleQuery::create()->findOneByName($packageName); |
|
86
|
|
|
|
|
87
|
|
|
// delete if found |
|
88
|
|
|
if ($model !== null) { |
|
89
|
|
|
$model->delete(); |
|
90
|
|
|
|
|
91
|
|
|
// TODO: Check if api and actions are also deleted (by the call above) |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->dispatcher->dispatch(ModuleEvent::UNINSTALLED, new ModuleEvent($model)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function activate(IOInterface $io, $packageName) { |
|
98
|
|
|
$io->write('[Keeko] Activate Module: ' . $packageName); |
|
99
|
|
|
|
|
100
|
|
|
$package = $this->service->getPackageManager()->getComposerPackage($packageName); |
|
101
|
|
|
|
|
102
|
|
|
$model = ModuleQuery::create()->findOneByName($packageName); |
|
103
|
|
|
$model->setActivatedVersion($package->getPrettyVersion()); |
|
104
|
|
|
$model->save(); |
|
105
|
|
|
|
|
106
|
|
|
$this->updateModule($model); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function updateModule(Module $model) { |
|
110
|
|
|
$package = $this->service->getPackageManager()->getPackage($model->getName()); |
|
111
|
|
|
$keeko = $package->getKeeko(); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
if ($keeko->isModule()) { |
|
114
|
|
|
$module = $keeko->getModule(); |
|
|
|
|
|
|
115
|
|
|
$actions = $this->updateActions($model, $module); |
|
116
|
|
|
$this->updateApi($model, $module, $actions); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
private function updateActions(Module $model, ModuleSchema $module) { |
|
121
|
|
|
$actions = []; |
|
122
|
|
|
|
|
123
|
|
|
foreach ($module->getActionNames() as $name) { |
|
124
|
|
|
$action = $module->getAction($name); |
|
125
|
|
|
$a = new Action(); |
|
|
|
|
|
|
126
|
|
|
$a->setName($name); |
|
127
|
|
|
$a->setModule($model); |
|
128
|
|
|
$a->setTitle($action->getTitle()); |
|
129
|
|
|
$a->setDescription($action->getDescription()); |
|
130
|
|
|
$a->setClassName($action->getClass()); |
|
131
|
|
|
|
|
132
|
|
|
// add acl |
|
133
|
|
|
foreach ($action->getAcl() as $group) { |
|
134
|
|
|
$a->addGroup($this->getGroup($group)); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$a->save(); |
|
138
|
|
|
$actions[$name] = $a->getId(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
// remove obsolete actions |
|
142
|
|
|
ActionQuery::create() |
|
143
|
|
|
->filterByModule($model) |
|
144
|
|
|
->where('Action.Name NOT IN ?', $module->getActionNames()->toArray()) |
|
145
|
|
|
->delete(); |
|
146
|
|
|
|
|
147
|
|
|
return $actions; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param string $name |
|
152
|
|
|
* @return Group |
|
153
|
|
|
*/ |
|
154
|
|
View Code Duplication |
private function getGroup($name) { |
|
|
|
|
|
|
155
|
|
|
switch ($name) { |
|
156
|
|
|
case 'guest': |
|
157
|
|
|
if ($this->guestGroup === null) { |
|
158
|
|
|
$this->guestGroup = GroupQuery::create()->filterByIsGuest(true)->findOne(); |
|
159
|
|
|
} |
|
160
|
|
|
return $this->guestGroup; |
|
161
|
|
|
|
|
162
|
|
|
case 'user': |
|
163
|
|
|
if ($this->userGroup === null) { |
|
164
|
|
|
$this->userGroup = GroupQuery::create()->filterByIsDefault(true)->findOne(); |
|
165
|
|
|
} |
|
166
|
|
|
return $this->userGroup; |
|
167
|
|
|
|
|
168
|
|
|
case 'admin': |
|
169
|
|
|
if ($this->adminGroup === null) { |
|
170
|
|
|
$this->adminGroup = GroupQuery::create()->findOneById(3); |
|
171
|
|
|
} |
|
172
|
|
|
return $this->adminGroup; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
private function updateApi(Module $model, $actions) { |
|
|
|
|
|
|
177
|
|
|
$repo = $this->service->getResourceRepository(); |
|
|
|
|
|
|
178
|
|
|
$filename = sprintf('/packages/%s/api.json', $model->getName()); |
|
179
|
|
|
if (!$repo->contains($filename)) { |
|
180
|
|
|
return; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
// delete every api existent for the given module prior to create the new ones |
|
184
|
|
|
// $q = ApiQuery::create() |
|
185
|
|
|
// ->useActionQuery() |
|
186
|
|
|
// ->filterByModule($model) |
|
187
|
|
|
// ->endUse(); |
|
188
|
|
|
|
|
189
|
|
|
// echo $q->toString(); |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
// $q->delete(); |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
// TODO: Work out extension system before defining parent endpoints |
|
194
|
|
|
|
|
195
|
|
|
// $swagger = new Swagger($repo->get($fileName)->getBody()); |
|
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
// foreach ($data['api']['apis'] as $apis) { |
|
|
|
|
|
|
198
|
|
|
// $path = $apis['path']; |
|
199
|
|
|
// foreach ($apis['operations'] as $op) { |
|
200
|
|
|
// // fetch required params |
|
201
|
|
|
// $required = []; |
|
202
|
|
|
// if (isset($op['parameters'])) { |
|
203
|
|
|
// foreach ($op['parameters'] as $param) { |
|
204
|
|
|
// if (isset($param['paramType']) && $param['paramType'] == 'path') { |
|
205
|
|
|
// $required[] = $param['name']; |
|
206
|
|
|
// } |
|
207
|
|
|
// } |
|
208
|
|
|
// } |
|
209
|
|
|
|
|
210
|
|
|
// // create record |
|
|
|
|
|
|
211
|
|
|
// $fullPath = str_replace('//', '/', $base . '/' . $path); |
|
212
|
|
|
// $api = new Api(); |
|
213
|
|
|
// $api->setMethod($op['method']); |
|
214
|
|
|
// $api->setRoute($fullPath); |
|
215
|
|
|
// $api->setActionId($actionMap[$op['nickname']]); |
|
216
|
|
|
// $api->setRequiredParams(implode(',', $required)); |
|
217
|
|
|
// $api->save(); |
|
218
|
|
|
// } |
|
219
|
|
|
// } |
|
220
|
|
|
|
|
221
|
|
|
$model->setApi(true); |
|
222
|
|
|
$model->save(); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function deactivate(IOInterface $io, $packageName) { |
|
226
|
|
|
$io->write('[Keeko] Deactivate Module: ' . $packageName); |
|
227
|
|
|
|
|
228
|
|
|
$mod = ModuleQuery::create()->filterByName($packageName)->findOne(); |
|
229
|
|
|
$mod->setActivatedVersion(null); |
|
230
|
|
|
$mod->save(); |
|
231
|
|
|
} |
|
232
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.