1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\package; |
3
|
|
|
|
4
|
|
|
use keeko\core\exceptions\ModuleException; |
5
|
|
|
use keeko\core\exceptions\PermissionDeniedException; |
6
|
|
|
use keeko\core\model\Action; |
7
|
|
|
use keeko\core\model\ActionQuery; |
8
|
|
|
use keeko\core\model\Module; |
9
|
|
|
use keeko\core\model\User; |
10
|
|
|
use keeko\core\preferences\Preferences; |
11
|
|
|
use keeko\core\service\ServiceContainer; |
12
|
|
|
use keeko\core\schema\PackageSchema; |
13
|
|
|
use keeko\core\schema\ActionSchema; |
14
|
|
|
|
15
|
|
|
abstract class AbstractModule { |
16
|
|
|
|
17
|
|
|
/** @var Module */ |
18
|
|
|
protected $model; |
19
|
|
|
|
20
|
|
|
protected $actions; |
21
|
|
|
|
22
|
|
|
/** @var User */ |
23
|
|
|
protected $user; |
24
|
|
|
|
25
|
|
|
/** @var ServiceContainer */ |
26
|
|
|
protected $service; |
27
|
|
|
|
28
|
|
|
/** @var Preferences */ |
29
|
|
|
private $preferences; |
30
|
|
|
|
31
|
|
|
/** @var PackageSchema */ |
32
|
|
|
protected $package; |
33
|
|
|
|
34
|
|
|
public function __construct(Module $module, ServiceContainer $service) { |
35
|
|
|
$this->model = $module; |
|
|
|
|
36
|
|
|
$this->service = $service; |
37
|
|
|
|
38
|
|
|
$packageManager = $service->getPackageManager(); |
39
|
|
|
$this->package = $packageManager->getPackage($module->getName()); |
|
|
|
|
40
|
|
|
|
41
|
|
|
$this->loadActions(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns the modules name |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getName() { |
50
|
|
|
return $this->model->getName(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the modules canonical name |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getCanonicalName() { |
59
|
|
|
return str_replace('/', '.', $this->getName()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the modules title |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getTitle() { |
68
|
|
|
return $this->model->getTitle(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the service container |
73
|
|
|
* |
74
|
|
|
* @return ServiceContainer |
75
|
|
|
*/ |
76
|
|
|
public function getServiceContainer() { |
77
|
|
|
return $this->service; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the associated module model |
82
|
|
|
* |
83
|
|
|
* @return Module |
84
|
|
|
*/ |
85
|
|
|
public function getModel() { |
86
|
|
|
return $this->model; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
public function getPath() { |
92
|
|
|
return sprintf('%s/%s/', KEEKO_PATH_MODULES, $this->model->getName()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getManagedFilesPath() { |
96
|
|
|
return sprintf('%s/managed/%s', KEEKO_PATH_FILES, $this->model->getName()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getManagedFilesUrl() { |
100
|
|
|
return sprintf('%s/files/managed/%s', $this->getServiceContainer()->getApplication()->getRootUrl(), $this->model->getName()); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the module's preferences |
105
|
|
|
* |
106
|
|
|
* @return Preferences |
107
|
|
|
*/ |
108
|
|
|
public function getPreferences() { |
109
|
|
|
if ($this->preferences === null) { |
110
|
|
|
$this->preferences = $this->service->getPreferenceLoader()->getModulePreferences($this->model->getId()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->preferences; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function loadActions() { |
117
|
|
|
$models = []; |
|
|
|
|
118
|
|
|
$actions = ActionQuery::create()->filterByModule($this->model)->find(); |
119
|
|
|
foreach ($actions as $action) { |
120
|
|
|
$models[$action->getName()] = $action; |
121
|
|
|
} |
122
|
|
|
$keeko = $this->package->getKeeko(); |
|
|
|
|
123
|
|
|
$module = $keeko->getModule(); |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
$this->actions = []; |
127
|
|
|
foreach ($module->getActionNames() as $actionName) { |
128
|
|
|
if (isset($models[$actionName])) { |
129
|
|
|
$this->actions[$actionName] = [ |
130
|
|
|
'model' => $models[$actionName], |
131
|
|
|
'action' => $module->getAction($actionName) |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Loads the given action |
139
|
|
|
* |
140
|
|
|
* @param Action|string $actionName |
|
|
|
|
141
|
|
|
* @param string $response the response type (e.g. html, json, ...) |
142
|
|
|
* @return AbstractAction |
143
|
|
|
*/ |
144
|
|
|
public function loadAction($nameOrAction, $response) { |
145
|
|
|
$model = null; |
146
|
|
|
if ($nameOrAction instanceof Action) { |
147
|
|
|
$model = $nameOrAction; |
|
|
|
|
148
|
|
|
$actionName = $nameOrAction->getName(); |
149
|
|
|
} else { |
150
|
|
|
$actionName = $nameOrAction; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (!isset($this->actions[$actionName])) { |
154
|
|
|
throw new ModuleException(sprintf('Action (%s) not found in Module (%s)', $actionName, $this->model->getName())); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if ($model === null) { |
158
|
|
|
$model = $this->actions[$actionName]['model']; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/* @var $action ActionSchema */ |
162
|
|
|
$action = $this->actions[$actionName]['action']; |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
// check permission |
166
|
|
|
if (!$this->service->getFirewall()->hasActionPermission($model)) { |
167
|
|
|
throw new PermissionDeniedException(sprintf('Can\'t access Action (%s) in Module (%s)', $actionName, $this->model->getName())); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// check if a response is given |
171
|
|
|
if (!$action->hasResponse($response)) { |
172
|
|
|
throw new ModuleException(sprintf('No Response (%s) given for Action (%s) in Module (%s)', $response, $actionName, $this->model->getName())); |
173
|
|
|
} |
174
|
|
|
$responseClass = $action->getResponse($response); |
175
|
|
|
|
176
|
|
|
if (!class_exists($responseClass)) { |
177
|
|
|
throw new ModuleException(sprintf('Response (%s) not found in Module (%s)', $responseClass, $this->model->getName())); |
178
|
|
|
} |
179
|
|
|
$response = new $responseClass($this, $response); |
180
|
|
|
|
181
|
|
|
// gets the action class |
182
|
|
|
$className = $model->getClassName(); |
183
|
|
|
|
184
|
|
|
if (!class_exists($className)) { |
185
|
|
|
throw new ModuleException(sprintf('Action (%s) not found in Module (%s)', $className, $this->model->getName())); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$class = new $className($model, $this, $response); |
189
|
|
|
|
190
|
|
|
// // l10n |
|
|
|
|
191
|
|
|
// $app = $this->getServiceContainer()->getApplication(); |
192
|
|
|
// $lang = $app->getLocalization()->getLanguage()->getAlpha2(); |
193
|
|
|
// $country = $app->getLocalization()->getCountry()->getAlpha2(); |
194
|
|
|
// $l10n = $this->getPath() . 'l10n/'; |
195
|
|
|
// $locale = $lang . '_' . $country; |
196
|
|
|
|
197
|
|
|
// // load module l10n |
|
|
|
|
198
|
|
|
// $this->addL10nFile('module', $l10n, $lang, $locale, $class); |
199
|
|
|
|
200
|
|
|
// // load additional l10n files |
|
|
|
|
201
|
|
|
// if (isset($this->actions[$actionName]['l10n'])) { |
202
|
|
|
// foreach ($this->actions[$actionName]['l10n'] as $file) { |
203
|
|
|
// $this->addL10nFile($file, $l10n, $lang, $locale, $class); |
204
|
|
|
// } |
205
|
|
|
// } |
206
|
|
|
|
207
|
|
|
// // load action l10n |
|
|
|
|
208
|
|
|
// $this->addL10nFile(sprintf('actions/%s', $actionName), $l10n, $lang, $locale, $class); |
209
|
|
|
|
210
|
|
|
// // assets |
|
|
|
|
211
|
|
|
// $page = $app->getPage(); |
212
|
|
|
// $moduleUrl = sprintf('%s/_keeko/modules/%s/', $app->getRootUrl(), $this->getName()); |
213
|
|
|
// if (isset($this->actions[$actionName]['assets']['styles'])) { |
214
|
|
|
// foreach ($this->actions[$actionName]['assets']['styles'] as $style) { |
215
|
|
|
// $page->addStyle($moduleUrl . $style); |
216
|
|
|
// } |
217
|
|
|
// } |
218
|
|
|
|
219
|
|
|
// if (isset($this->actions[$actionName]['assets']['scripts'])) { |
|
|
|
|
220
|
|
|
// foreach ($this->actions[$actionName]['assets']['scripts'] as $script) { |
221
|
|
|
// $page->addScript($moduleUrl . $script); |
222
|
|
|
// } |
223
|
|
|
// } |
224
|
|
|
|
225
|
|
|
return $class; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
private function addL10nFile($file, $dir, $lang, $locale, $class) { |
|
|
|
|
229
|
|
|
$translator = $this->getServiceContainer()->getTranslator(); |
230
|
|
|
$langPath = sprintf('%s%s/%s.json', $dir, $lang, $file); |
|
|
|
|
231
|
|
|
$localePath = sprintf('%s%s/%s.json', $dir, $locale, $file); |
232
|
|
|
|
233
|
|
|
if (file_exists($langPath)) { |
234
|
|
|
$translator->addResource('json', $langPath, $lang, $class->getCanonicalName()); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (file_exists($localePath)) { |
238
|
|
|
$translator->addResource('json', $langPath, $locale, $class->getCanonicalName()); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Shortcut for getting permission on the given action in this module |
244
|
|
|
* |
245
|
|
|
* @param string $action |
246
|
|
|
* @param User $user |
247
|
|
|
*/ |
248
|
|
|
public function hasPermission($action, User $user = null) { |
249
|
|
|
return $this->getServiceContainer()->getFirewall()->hasPermission($this->getName(), $action, $user); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
abstract public function install(); |
|
|
|
|
253
|
|
|
|
254
|
|
|
abstract public function uninstall(); |
|
|
|
|
255
|
|
|
|
256
|
|
|
abstract public function update($from, $to); |
|
|
|
|
257
|
|
|
} |
258
|
|
|
|
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.