ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilPlugin.php
Go to the documentation of this file.
1<?php
2
25
31abstract class ilPlugin
32{
33 protected ilDBInterface $db;
35 protected string $id;
37 protected bool $lang_initialised = false;
39 protected string $message = '';
40
44 public function getMessage(): string
45 {
46 return $this->message;
47 }
48
49 // ------------------------------------------
50 // Initialisation
51 // ------------------------------------------
52
53 public function __construct(
56 string $id
57 ) {
59 throw new \LogicException(
60 "You tried to instantiate a plugin with an inexisting id '$id'." .
61 "This is odd... Please use ilComponentFactory to instantiate plugins."
62 );
63 }
64
65 $this->db = $db;
66 $this->component_repository = $component_repository;
67 $this->id = $id;
68
69 $this->provider_collection = new PluginProviderCollection();
70
71 // Fix for authentication plugins
72 $this->loadLanguageModule();
73
74 // Custom initialisation for plugin.
75 $this->init();
76 }
77
86 protected function init(): void
87 {
88 }
89
90
91 // ------------------------------------------
92 // General Information About Plugin
93 // ------------------------------------------
94
95 public function getPluginName(): string
96 {
97 return $this->getPluginInfo()->getName();
98 }
99
100 public function getId(): string
101 {
102 return $this->getPluginInfo()->getId();
103 }
104
111 public function getVersion(): string
112 {
113 return (string) $this->getPluginInfo()->getAvailableVersion();
114 }
115
121 public function getDirectory(): string
122 {
123 return $this->getPluginInfo()->getPath();
124 }
125
131 public function isActive(): bool
132 {
133 return $this->getPluginInfo()->isActive();
134 }
135
136 public function needsUpdate(): bool
137 {
138 return $this->getPluginInfo()->isUpdateRequired();
139 }
140
141 protected function getPluginInfo(): ilPluginInfo
142 {
143 return $this->component_repository
144 ->getPluginById(
145 $this->id
146 );
147 }
148
149 protected function getComponentInfo(): ilComponentInfo
150 {
151 return $this->getPluginInfo()->getComponent();
152 }
153
155 {
156 return $this->getPluginInfo()->getPluginSlot();
157 }
158
159
160 // ------------------------------------------
161 // (De-)Installation
162 // ------------------------------------------
163
164 public function install(): void
165 {
166 $this->afterInstall();
167 }
168
169 public function uninstall(): bool
170 {
171 if (!$this->beforeUninstall()) {
172 return false;
173 }
174
175 $this->getLanguageHandler()->uninstall();
176 $this->component_repository->removeStateInformationOf($this->getId());
177 $this->afterUninstall();
178 return true;
179 }
180
185 protected function afterInstall(): void
186 {
187 }
188
193 protected function beforeUninstall(): bool
194 {
195 return true;
196 }
197
202 protected function afterUninstall(): void
203 {
204 }
205
206
207 // ------------------------------------------
208 // (De-)Activation
209 // ------------------------------------------
210
214 public function activate(): bool
215 {
216 if ($this->needsUpdate() && !$this->update()) {
217 return false;
218 }
219
220 if (!$this->beforeActivation()) {
221 return false;
222 }
223
224 $this->component_repository->setActivation($this->getId(), true);
225 $this->afterActivation();
226
227 return true;
228 }
229
230 public function deactivate(): bool
231 {
232 $this->component_repository->setActivation($this->getId(), false);
233 $this->afterDeactivation();
234
235 return true;
236 }
237
242 protected function beforeActivation(): bool
243 {
244 return true;
245 }
246
251 protected function afterActivation(): void
252 {
253 }
254
259 protected function afterDeactivation(): void
260 {
261 }
262
263
264 // ------------------------------------------
265 // Update
266 // ------------------------------------------
267
268 public function update(): bool
269 {
270 global $DIC;
271 $ilDB = $DIC->database();
272
273 $result = $this->beforeUpdate();
274 if ($result === false) {
275 return false;
276 }
277
278 // Load language files
279 $this->getLanguageHandler()->updateLanguages();
280
281 // DB update
282 $db_version = $this->updateDatabase();
283
284 // set last update version to current version
285 $this->component_repository->setCurrentPluginVersion(
286 $this->getPluginInfo()->getId(),
287 $this->getPluginInfo()->getAvailableVersion(),
288 $db_version
289 );
290 $this->afterUpdate();
291
292 return true;
293 }
294
295 protected function updateDatabase(): int
296 {
297 global $DIC;
298 $ilDB = $DIC->database();
299 $lng = $DIC->language();
300
301 $dbupdate = new ilPluginDBUpdate(
302 $ilDB,
303 $this->getPluginInfo()
304 );
305
306 $dbupdate->applyUpdate();
307
308 return $dbupdate->getCurrentVersion();
309 }
310
315 protected function beforeUpdate(): bool
316 {
317 return true;
318 }
319
320
321 protected function afterUpdate(): void
322 {
323 }
324
325
326 // ------------------------------------------
327 // Language Handling
328 // ------------------------------------------
329
331 {
332 if ($this->language_handler === null) {
333 $this->language_handler = $this->buildLanguageHandler();
334 }
336 }
337
339 {
340 return new ilPluginLanguage($this->getPluginInfo());
341 }
342
348 public function loadLanguageModule(): void
349 {
350 $this->getLanguageHandler()->loadLanguageModule();
351 }
352
356 public function txt(string $a_var): string
357 {
358 return $this->getLanguageHandler()->txt($a_var);
359 }
360
361
362 // ------------------------------------------
363 // Rendering and Style
364 // ------------------------------------------
365
370 public function getTemplate(string $a_template, bool $a_par1 = true, bool $a_par2 = true): ilTemplate
371 {
372 return new ilTemplate($this->getDirectory() . "/templates/" . $a_template, $a_par1, $a_par2);
373 }
374
379 public function getStyleSheetLocation(string $a_css_file): string
380 {
381 $d2 = $this->getComponentInfo()->getId() . "_" . $this->getPluginSlotInfo()->getId() . "_" . $this->getPluginInfo()->getId();
382
383 $css = ilUtil::getStyleSheetLocation("output", $a_css_file, $d2);
384 if (is_int(strpos($css, "Customizing"))) {
385 return $css;
386 }
387
388 return $this->getDirectory() . "/templates/" . $a_css_file;
389 }
390
391
396 public function addBlockFile($a_tpl, $a_var, $a_block, $a_tplname): void
397 {
398 $a_tpl->addBlockFile(
399 $a_var,
400 $a_block,
401 $this->getDirectory() . "/templates/" . $a_tplname
402 );
403 }
404
405
406 // ------------------------------------------
407 // Global Screen
408 // ------------------------------------------
409
411 {
413 }
414
415
416 // ------------------------------------------
417 // Initialisation
418 // ------------------------------------------
419
431 public function exchangeUIRendererAfterInitialization(\ILIAS\DI\Container $dic): Closure
432 {
433 //This returns the callable of $c['ui.renderer'] without executing it.
434 return $dic->raw('ui.renderer');
435 }
436
437
451 public function exchangeUIFactoryAfterInitialization(string $dic_key, \ILIAS\DI\Container $dic): Closure
452 {
453 //This returns the callable of $c[$key] without executing it.
454 return $dic->raw($dic_key);
455 }
456}
Simple value class for basic information about a component.
Database Update class.
Simple value class for information about a plugin.
Simple value class for basic information about a pluginslot.
ilDBInterface $db
__construct(\ilDBInterface $db, \ilComponentRepositoryWrite $component_repository, string $id)
exchangeUIFactoryAfterInitialization(string $dic_key, \ILIAS\DI\Container $dic)
This methods allows to replace some factory for UI Components (see src/UI) of ILIAS after initializat...
ProviderCollection $provider_collection
afterActivation()
@deprecate If you cannot get rid of the requirement to use this, adjust the activate method in your s...
exchangeUIRendererAfterInitialization(\ILIAS\DI\Container $dic)
This methods allows to replace the UI Renderer (see src/UI) of ILIAS after initialization by returnin...
activate()
This will update (if required) and activate the plugin.
isActive()
Only very little classes seem to care about this:
ilPluginLanguage $language_handler
txt(string $a_var)
Get Language Variable (prefix will be prepended automatically)
addBlockFile($a_tpl, $a_var, $a_block, $a_tplname)
@deprecate ILIAS is moving towards UI components and plugins are expected to use these components.
getVersion()
Only very little classes seem to care about this:
afterInstall()
@deprecate If you cannot get rid of the requirement to use this, adjust the install method in your su...
string $message
afterUninstall()
@deprecate If you cannot get rid of the requirement to use this, adjust the uninstall method in your ...
init()
Object initialization.
string $id
getTemplate(string $a_template, bool $a_par1=true, bool $a_par2=true)
@deprecate ILIAS is moving towards UI components and plugins are expected to use these components.
beforeUpdate()
@deprecate If you cannot get rid of the requirement to use this, adjust the update method in your sub...
beforeUninstall()
@deprecate If you cannot get rid of the requirement to use this, adjust the uninstall method in your ...
getDirectory()
Only very little classes seem to care about this:
buildLanguageHandler()
beforeActivation()
@deprecate If you cannot get rid of the requirement to use this, adjust the activate method in your s...
loadLanguageModule()
Load language module for plugin.
bool $lang_initialised
ilComponentRepositoryWrite $component_repository
afterDeactivation()
@deprecate If you cannot get rid of the requirement to use this, adjust the activate method in your s...
getStyleSheetLocation(string $a_css_file)
@deprecate ILIAS is moving towards UI components and plugins are expected to use these components.
getGlobalScreenProviderCollection()
special template class to simplify handling of ITX/PEAR
static getStyleSheetLocation(string $mode="output", string $a_css_name="")
get full style sheet file name (path inclusive) of current user
An environment holds resources to be used in the setup process.
Definition: Environment.php:28
Writeable part of repository interface to ilComponentDataDB.
hasPluginId(string $id)
Check if a plugin exists.
Interface ilDBInterface.
$dic
Definition: ltiresult.php:33
Class HTTPServicesTest.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26