Plugins.php 1.5 KB

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