Plugins.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. ###
  3. # @name Plugins Module
  4. # @copyright 2015 by Tobias Reich
  5. ###
  6. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  7. final class Plugins implements \SplSubject {
  8. private $files = array();
  9. private $observers = array();
  10. public $action = null;
  11. public $args = null;
  12. public function __construct(array $files, $database, array $settings) {
  13. if (!isset($files)) return false;
  14. # Init vars
  15. $this->files = $files;
  16. # Load plugins
  17. foreach ($this->files as $file) {
  18. if ($file==='') continue;
  19. $file = LYCHEE_PLUGINS . $file;
  20. if (file_exists($file)===false) {
  21. Log::warning($database, __METHOD__, __LINE__, 'Could not include plugin. File does not exist (' . $file . ').');
  22. continue;
  23. }
  24. include($file);
  25. }
  26. return true;
  27. }
  28. public function attach(\SplObserver $observer) {
  29. if (!isset($observer)) return false;
  30. # Add observer
  31. $this->observers[] = $observer;
  32. return true;
  33. }
  34. public function detach(\SplObserver $observer) {
  35. if (!isset($observer)) return false;
  36. # Remove observer
  37. $key = array_search($observer, $this->observers, true);
  38. if ($key) unset($this->observers[$key]);
  39. return true;
  40. }
  41. public function notify() {
  42. # Notify each observer
  43. foreach ($this->observers as $value) $value->update($this);
  44. return true;
  45. }
  46. public function activate($action, $args) {
  47. if (!isset($action, $args)) return false;
  48. # Save vars
  49. $this->action = $action;
  50. $this->args = $args;
  51. # Notify observers
  52. $this->notify();
  53. return true;
  54. }
  55. }
  56. ?>