Plugins.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Lychee\Modules;
  3. use SplSubject;
  4. final class Plugins implements SplSubject {
  5. private static $instance = null;
  6. private $observers = array();
  7. public $action = null;
  8. public $args = null;
  9. public static function get() {
  10. if (!self::$instance) {
  11. $files = Settings::get()['plugins'];
  12. self::$instance = new self($files);
  13. }
  14. return self::$instance;
  15. }
  16. private function __construct(array $plugins) {
  17. # Load plugins
  18. foreach ($plugins as $plugin) {
  19. if ($plugin==='') continue;
  20. $this->attach(new $plugin);
  21. }
  22. return true;
  23. }
  24. public function attach(\SplObserver $observer) {
  25. if (!isset($observer)) return false;
  26. # Add observer
  27. $this->observers[] = $observer;
  28. return true;
  29. }
  30. public function detach(\SplObserver $observer) {
  31. if (!isset($observer)) return false;
  32. # Remove observer
  33. $key = array_search($observer, $this->observers, true);
  34. if ($key) unset($this->observers[$key]);
  35. return true;
  36. }
  37. public function notify() {
  38. # Notify each observer
  39. foreach ($this->observers as $value) $value->update($this);
  40. return true;
  41. }
  42. public function activate($action, $args) {
  43. if (!isset($action, $args)) return false;
  44. # Save vars
  45. $this->action = $action;
  46. $this->args = $args;
  47. # Notify observers
  48. $this->notify();
  49. return true;
  50. }
  51. }
  52. ?>