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