2014-04-02 17:30:58 +00:00
|
|
|
<?php
|
|
|
|
|
2016-01-26 14:31:53 +00:00
|
|
|
namespace Lychee\Modules;
|
2014-04-02 17:30:58 +00:00
|
|
|
|
2016-01-26 14:31:53 +00:00
|
|
|
use SplSubject;
|
2016-01-29 23:07:22 +00:00
|
|
|
use SplObserver;
|
2014-04-16 08:13:30 +00:00
|
|
|
|
2016-01-26 14:31:53 +00:00
|
|
|
final class Plugins implements SplSubject {
|
2014-04-02 17:30:58 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
private static $instance = null;
|
2014-04-02 17:30:58 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
private $observers = array();
|
2014-04-02 17:30:58 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
public $action = null;
|
|
|
|
public $args = null;
|
2014-04-02 17:30:58 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function get() {
|
2014-04-02 17:30:58 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
if (!self::$instance) {
|
2014-04-02 17:30:58 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
$files = Settings::get()['plugins'];
|
|
|
|
|
|
|
|
self::$instance = new self($files);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
|
|
|
|
}
|
2014-05-17 14:51:56 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
private function __construct(array $plugins) {
|
2014-05-17 14:51:56 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Load plugins
|
2016-01-24 21:14:20 +00:00
|
|
|
foreach ($plugins as $plugin) {
|
2014-05-17 14:51:56 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
if ($plugin==='') continue;
|
2014-05-17 14:51:56 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
$this->attach(new $plugin);
|
2014-05-17 14:51:56 +00:00
|
|
|
|
2014-04-02 17:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-29 23:07:22 +00:00
|
|
|
public function attach(SplObserver $observer) {
|
2014-04-02 17:30:58 +00:00
|
|
|
|
|
|
|
if (!isset($observer)) return false;
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Add observer
|
2014-04-02 17:30:58 +00:00
|
|
|
$this->observers[] = $observer;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-29 23:07:22 +00:00
|
|
|
public function detach(SplObserver $observer) {
|
2014-04-02 17:30:58 +00:00
|
|
|
|
|
|
|
if (!isset($observer)) return false;
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Remove observer
|
2014-04-02 17:30:58 +00:00
|
|
|
$key = array_search($observer, $this->observers, true);
|
|
|
|
if ($key) unset($this->observers[$key]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function notify() {
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Notify each observer
|
2014-04-02 17:30:58 +00:00
|
|
|
foreach ($this->observers as $value) $value->update($this);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-29 23:27:50 +00:00
|
|
|
public function activate($name, $location, array $args) {
|
2014-04-02 17:30:58 +00:00
|
|
|
|
2016-01-29 23:27:50 +00:00
|
|
|
if (!isset($name, $location, $args)) return false;
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Parse
|
|
|
|
$location = ($location===0 ? 'before' : 'after');
|
|
|
|
$action = $name . ":" . $location;
|
2014-04-02 17:30:58 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Save vars
|
|
|
|
$this->action = $action;
|
|
|
|
$this->args = $args;
|
2014-04-02 17:30:58 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Notify observers
|
2014-04-02 17:30:58 +00:00
|
|
|
$this->notify();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
?>
|