Plugins.php 1.5 KB

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