ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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
119 public function getDirectory(): string
120 {
121 return $this->getPluginInfo()->getPath();
122 }
123
124 public function getRelativeDirectory(): string
125 {
126 return str_replace(
127 ILIAS_ABSOLUTE_PATH . "/public/",
128 "",
129 realpath($this->getPluginInfo()->getPath())
130 );
131 }
132
138 public function isActive(): bool
139 {
140 return $this->getPluginInfo()->isActive();
141 }
142
143 public function needsUpdate(): bool
144 {
145 return $this->getPluginInfo()->isUpdateRequired();
146 }
147
148 protected function getPluginInfo(): ilPluginInfo
149 {
150 return $this->component_repository
151 ->getPluginById(
152 $this->id
153 );
154 }
155
156 protected function getComponentInfo(): ilComponentInfo
157 {
158 return $this->getPluginInfo()->getComponent();
159 }
160
162 {
163 return $this->getPluginInfo()->getPluginSlot();
164 }
165
166
167 // ------------------------------------------
168 // (De-)Installation
169 // ------------------------------------------
170
171 public function install(): void
172 {
173 $this->afterInstall();
174 }
175
176 public function uninstall(): bool
177 {
178 if (!$this->beforeUninstall()) {
179 return false;
180 }
181
182 $this->getLanguageHandler()->uninstall();
183 $this->component_repository->removeStateInformationOf($this->getId());
184 $this->afterUninstall();
185 return true;
186 }
187
192 protected function afterInstall(): void
193 {
194 }
195
200 protected function beforeUninstall(): bool
201 {
202 return true;
203 }
204
209 protected function afterUninstall(): void
210 {
211 }
212
213
214 // ------------------------------------------
215 // (De-)Activation
216 // ------------------------------------------
217
221 public function activate(): bool
222 {
223 if ($this->needsUpdate() && !$this->update()) {
224 return false;
225 }
226
227 if (!$this->beforeActivation()) {
228 return false;
229 }
230
231 $this->component_repository->setActivation($this->getId(), true);
232 $this->afterActivation();
233
234 return true;
235 }
236
237 public function deactivate(): bool
238 {
239 $this->component_repository->setActivation($this->getId(), false);
240 $this->afterDeactivation();
241
242 return true;
243 }
244
249 protected function beforeActivation(): bool
250 {
251 return true;
252 }
253
258 protected function afterActivation(): void
259 {
260 }
261
266 protected function afterDeactivation(): void
267 {
268 }
269
270
271 // ------------------------------------------
272 // Update
273 // ------------------------------------------
274
275 public function update(): bool
276 {
277 global $DIC;
278 $ilDB = $DIC->database();
279
280 $result = $this->beforeUpdate();
281 if ($result === false) {
282 return false;
283 }
284
285 // Load language files
286 $this->getLanguageHandler()->updateLanguages();
287
288 // DB update
289 $db_version = $this->updateDatabase();
290
291 // set last update version to current version
292 $this->component_repository->setCurrentPluginVersion(
293 $this->getPluginInfo()->getId(),
294 $this->getPluginInfo()->getAvailableVersion(),
295 $db_version
296 );
297 $this->afterUpdate();
298
299 return true;
300 }
301
302 protected function updateDatabase(): int
303 {
304 global $DIC;
305 $ilDB = $DIC->database();
306 $lng = $DIC->language();
307
308 $dbupdate = new ilPluginDBUpdate(
309 $ilDB,
310 $this->getPluginInfo()
311 );
312
313 $dbupdate->applyUpdate();
314
315 return $dbupdate->getCurrentVersion();
316 }
317
322 protected function beforeUpdate(): bool
323 {
324 return true;
325 }
326
327
328 protected function afterUpdate(): void
329 {
330 }
331
332
333 // ------------------------------------------
334 // Language Handling
335 // ------------------------------------------
336
338 {
339 if ($this->language_handler === null) {
340 $this->language_handler = $this->buildLanguageHandler();
341 }
343 }
344
346 {
347 return new ilPluginLanguage($this->getPluginInfo());
348 }
349
355 public function loadLanguageModule(): void
356 {
357 $this->getLanguageHandler()->loadLanguageModule();
358 }
359
363 public function txt(string $a_var): string
364 {
365 return $this->getLanguageHandler()->txt($a_var);
366 }
367
368
369 // ------------------------------------------
370 // Rendering and Style
371 // ------------------------------------------
372
377 public function getTemplate(string $a_template, bool $a_par1 = true, bool $a_par2 = true): ilTemplate
378 {
379 return new ilTemplate($this->getDirectory() . "/templates/" . $a_template, $a_par1, $a_par2);
380 }
381
386 public function getStyleSheetLocation(string $a_css_file): string
387 {
388 $d2 = $this->getComponentInfo()->getId() . "_" . $this->getPluginSlotInfo()->getId() . "_" . $this->getPluginInfo()->getId();
389
390 $css = ilUtil::getStyleSheetLocation("output", $a_css_file, $d2);
391 if (is_int(strpos($css, "Customizing"))) {
392 return $css;
393 }
394
395 return $this->getDirectory() . "/templates/" . $a_css_file;
396 }
397
398
403 public function addBlockFile($a_tpl, $a_var, $a_block, $a_tplname): void
404 {
405 $a_tpl->addBlockFile(
406 $a_var,
407 $a_block,
408 $this->getDirectory() . "/templates/" . $a_tplname
409 );
410 }
411
412
413 // ------------------------------------------
414 // Global Screen
415 // ------------------------------------------
416
418 {
420 }
421
422
423 // ------------------------------------------
424 // Initialisation
425 // ------------------------------------------
426
438 public function exchangeUIRendererAfterInitialization(\ILIAS\DI\Container $dic): Closure
439 {
440 //This returns the callable of $c['ui.renderer'] without executing it.
441 return $dic->raw('ui.renderer');
442 }
443
444
458 public function exchangeUIFactoryAfterInitialization(string $dic_key, \ILIAS\DI\Container $dic): Closure
459 {
460 //This returns the callable of $c[$key] without executing it.
461 return $dic->raw($dic_key);
462 }
463}
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()
@description Return the directory provided by ilPluginInfo, please note that this is a absolute path.
buildLanguageHandler()
beforeActivation()
@deprecate If you cannot get rid of the requirement to use this, adjust the activate method in your s...
getRelativeDirectory()
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