ILIAS  release_8 Revision v8.23
class.ilPlugin.php
Go to the documentation of this file.
1 <?php
25 
31 abstract 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(
54  \ilDBInterface $db,
55  \ilComponentRepositoryWrite $component_repository,
56  string $id
57  ) {
58  if (!$component_repository->hasPluginId($id)) {
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 
154  protected function getPluginSlotInfo(): ilPluginSlotInfo
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->clearEventListening();
177  $this->component_repository->removeStateInformationOf($this->getId());
178  $this->afterUninstall();
179  return true;
180  }
181 
186  protected function afterInstall(): void
187  {
188  }
189 
194  protected function beforeUninstall(): bool
195  {
196  return true;
197  }
198 
203  protected function afterUninstall(): void
204  {
205  }
206 
207 
208  // ------------------------------------------
209  // (De-)Activation
210  // ------------------------------------------
211 
215  public function activate(): bool
216  {
217  if ($this->needsUpdate() && !$this->update()) {
218  return false;
219  }
220 
221  if (!$this->beforeActivation()) {
222  return false;
223  }
224 
225  $this->component_repository->setActivation($this->getId(), true);
226  $this->afterActivation();
227 
228  return true;
229  }
230 
231  public function deactivate(): bool
232  {
233  $this->component_repository->setActivation($this->getId(), false);
234  $this->afterDeactivation();
235 
236  return true;
237  }
238 
243  protected function beforeActivation(): bool
244  {
245  return true;
246  }
247 
252  protected function afterActivation(): void
253  {
254  }
255 
260  protected function afterDeactivation(): void
261  {
262  }
263 
264 
265  // ------------------------------------------
266  // Update
267  // ------------------------------------------
268 
269  public function update(): bool
270  {
271  global $DIC;
272  $ilDB = $DIC->database();
273 
274  $result = $this->beforeUpdate();
275  if ($result === false) {
276  return false;
277  }
278 
279  // Load language files
280  $this->getLanguageHandler()->updateLanguages();
281 
282  // DB update
283  $db_version = $this->updateDatabase();
284 
285  $this->readEventListening();
286 
287  // set last update version to current version
288  $this->component_repository->setCurrentPluginVersion(
289  $this->getPluginInfo()->getId(),
290  $this->getPluginInfo()->getAvailableVersion(),
291  $db_version
292  );
293  $this->afterUpdate();
294 
295  return true;
296  }
297 
298  protected function updateDatabase(): int
299  {
300  global $DIC;
301  $ilDB = $DIC->database();
302  $lng = $DIC->language();
303 
304  $dbupdate = new ilPluginDBUpdate(
305  $this->db,
306  $this->getPluginInfo()
307  );
308 
309  $dbupdate->applyUpdate();
310 
311  return $dbupdate->getCurrentVersion();
312  }
313 
318  protected function beforeUpdate(): bool
319  {
320  return true;
321  }
322 
323 
324  protected function afterUpdate(): void
325  {
326  }
327 
328 
329  // ------------------------------------------
330  // Language Handling
331  // ------------------------------------------
332 
333  protected function getLanguageHandler(): ilPluginLanguage
334  {
335  if ($this->language_handler === null) {
336  $this->language_handler = $this->buildLanguageHandler();
337  }
339  }
340 
342  {
343  return new ilPluginLanguage($this->getPluginInfo());
344  }
345 
351  public function loadLanguageModule(): void
352  {
353  $this->getLanguageHandler()->loadLanguageModule();
354  }
355 
359  public function txt(string $a_var): string
360  {
361  return $this->getLanguageHandler()->txt($a_var);
362  }
363 
364 
365  // ------------------------------------------
366  // Rendering and Style
367  // ------------------------------------------
368 
373  public function getTemplate(string $a_template, bool $a_par1 = true, bool $a_par2 = true): ilTemplate
374  {
375  return new ilTemplate($this->getDirectory() . "/templates/" . $a_template, $a_par1, $a_par2);
376  }
377 
382  public function getStyleSheetLocation(string $a_css_file): string
383  {
384  $d2 = $this->getComponentInfo()->getId() . "_" . $this->getPluginSlotInfo()->getId() . "_" . $this->getPluginInfo()->getId();
385 
386  $css = ilUtil::getStyleSheetLocation("output", $a_css_file, $d2);
387  if (is_int(strpos($css, "Customizing"))) {
388  return $css;
389  }
390 
391  return $this->getDirectory() . "/templates/" . $a_css_file;
392  }
393 
394 
399  public function addBlockFile($a_tpl, $a_var, $a_block, $a_tplname): void
400  {
401  $a_tpl->addBlockFile(
402  $a_var,
403  $a_block,
404  $this->getDirectory() . "/templates/" . $a_tplname
405  );
406  }
407 
408 
409  // ------------------------------------------
410  // Event Handling
411  // ------------------------------------------
412 
413  protected function readEventListening(): void
414  {
415  $reader = new ilPluginReader(
416  $this->getDirectory() . '/plugin.xml',
417  $this->getComponentInfo()->getType(),
418  $this->getComponentInfo()->getName(),
419  $this->getPluginSlotInfo()->getId(),
420  $this->getPluginInfo()->getName()
421  );
422  $reader->clearEvents();
423  $reader->startParsing();
424  }
425 
426  protected function clearEventListening(): void
427  {
428  $reader = new ilPluginReader(
429  $this->getDirectory() . '/plugin.xml',
430  $this->getComponentInfo()->getType(),
431  $this->getComponentInfo()->getName(),
432  $this->getPluginSlotInfo()->getId(),
433  $this->getPluginInfo()->getName()
434  );
435  $reader->clearEvents();
436  }
437 
438 
439  // ------------------------------------------
440  // Global Screen
441  // ------------------------------------------
442 
444  {
446  }
447 
448 
449  // ------------------------------------------
450  // Initialisation
451  // ------------------------------------------
452 
465  {
466  //This returns the callable of $c['ui.renderer'] without executing it.
467  return $dic->raw('ui.renderer');
468  }
469 
470 
484  public function exchangeUIFactoryAfterInitialization(string $dic_key, \ILIAS\DI\Container $dic): Closure
485  {
486  //This returns the callable of $c[$key] without executing it.
487  return $dic->raw($dic_key);
488  }
489 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
loadLanguageModule()
Load language module for plugin.
getVersion()
Only very little classes seem to care about this:
hasPluginId(string $id)
Check if a plugin exists.
string $id
Writeable part of repository interface to ilComponentDataDB.
$lng
beforeUninstall()
If you cannot get rid of the requirement to use this, adjust the uninstall method in your subclass in...
ilPluginLanguage $language_handler
Class ChatMainBarProvider .
Database Update class.
Simple value class for basic information about a pluginslot.
ProviderCollection $provider_collection
getTemplate(string $a_template, bool $a_par1=true, bool $a_par2=true)
ILIAS is moving towards UI components and plugins are expected to use these components.
afterUninstall()
If you cannot get rid of the requirement to use this, adjust the uninstall method in your subclass in...
string $message
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...
activate()
This will update (if required) and activate the plugin.
static getStyleSheetLocation(string $mode="output", string $a_css_name="", string $a_css_location="")
get full style sheet file name (path inclusive) of current user
beforeActivation()
If you cannot get rid of the requirement to use this, adjust the activate method in your subclass ins...
isActive()
Only very little classes seem to care about this:
ilComponentRepositoryWrite $component_repository
global $DIC
Definition: feed.php:28
init()
Object initialization.
afterInstall()
If you cannot get rid of the requirement to use this, adjust the install method in your subclass inst...
__construct(\ilDBInterface $db, \ilComponentRepositoryWrite $component_repository, string $id)
afterActivation()
If you cannot get rid of the requirement to use this, adjust the activate method in your subclass ins...
bool $lang_initialised
Class HTTPServicesTest.
getDirectory()
Only very little classes seem to care about this:
txt(string $a_var)
Get Language Variable (prefix will be prepended automatically)
Simple value class for information about a plugin.
buildLanguageHandler()
ilDBInterface $db
getGlobalScreenProviderCollection()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getStyleSheetLocation(string $a_css_file)
ILIAS is moving towards UI components and plugins are expected to use these components.
$dic
Definition: result.php:32
addBlockFile($a_tpl, $a_var, $a_block, $a_tplname)
ILIAS is moving towards UI components and plugins are expected to use these components.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
beforeUpdate()
If you cannot get rid of the requirement to use this, adjust the update method in your subclass inste...
Simple value class for basic information about a component.
afterDeactivation()
If you cannot get rid of the requirement to use this, adjust the activate method in your subclass ins...
exchangeUIRendererAfterInitialization(\ILIAS\DI\Container $dic)
This methods allows to replace the UI Renderer (see src/UI) of ILIAS after initialization by returnin...