Plugins.php 1.4 KB

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