plugins as $kind){ foreach($kind as $plugin){ $success = $plugin['instance']->init($kind); if (!$success) { self::unregister($plugin['kind'], $plugin['name']); } } } } //init() /** * @brief registers new plugin - calls private method over the instance * @param $kind [Integer] type of the plugin * @param $name [String] class name of the plugin * @return [boolean] success/failure */ public static function register($kind, $name) { //check if plugin was not already registered if (self::isPluginRegistered($kind, $name)) { return false; } //get own instance $instance = &parent::getInstance(__CLASS__); //register plugin return $instance->registerPlugin($kind, $name); } //register() public static function unregister($kind, $name) { //check if plugin is registered if (!self::isPluginRegistered($kind, $name)) { return false; } //get own instance $instance = &parent::getInstance(__CLASS__); //register plugin return $instance->unregisterPlugin($kind, $name); } //unregister /** * @brief registers new plugin * @param $kind [Integer] type of the plugin * @param $name [String] class name of the plugin * @return [boolean] success/failure */ private function registerPlugin($kind, $name) { $plugin = new $name(); if (!isset($this->plugins[$kind])) { $this->plugins[$kind] = array(); } $this->plugins[$kind][$name] = array( 'name' => $name, 'kind' => $kind, 'instance' => &$plugin, 'version' => $plugin->getVersion(), ); switch($kind){ case self::INTERPRETER: $commands = $plugin->getCommands(); if (!is_array($commands)) { throw new Exception('Plugin '.$name.' does not return any commands!'); } foreach($commands as $command){ if (!isset($this->commands[$command])) { $this->commands[$command] = array( 'interpreter' => $name, 'instance' => &$plugin, ); //store new command } //if new command } //foreach command $this->plugins[$kind][$name]['commands'] = $commands; break; case self::OBSERVER: $events = $plugin->getEvents(); if (!is_array($events)) { throw new Exception('Plugin '.$name.' does not return any events!'); } foreach($events as $event){ if (!isset($this->events[$event])) { $this->events[$event] = array(); } $this->events[$event][$name] = array( 'observer' => $name, 'instance' => &$plugin, ); } //foreach event $this->plugins[$kind][$name]['events'] = $events; break; default: throw new Exception('Uknown plugin kind '.$kind.''); } // switch return true; } /** * @brief removes plugin from registration * @param $kind [Integer] type of the plugin * @param $name [String] class name of the plugin * @return [boolean] success/failure */ private function unregisterPlugin($kind, $name) { switch($kind){ case self::INTERPRETER: //remove plugin from commands foreach($this->plugins[$kind][$name]['commands'] as $command){ unset($this->commands[$command]); } break; case self::OBSERVER: //remove plugin from events foreach($this->plugins[$kind][$name]['events'] as $event){ unset($this->events[$event][$name]); } break; default: throw new Exception('Uknown plugin kind '.$kind.''); } // switch unset($this->plugins[$kind][$name]); return true; } /** * @brief returns true if the plugin was already registered * @param $kind [Integer] type of the plugin * @param $name [String] name of the plugin * @return [Boolean] */ public static function isPluginRegistered($kind, $name) { //ensure this singleton has an instance parent::createInstance(__CLASS__); $instance = &parent::getInstance(__CLASS__); //return true if the plugin was already registered return isset($instance->plugins[$kind][$name]); } //isPluginRegistered() /** * @brief returns reference to the instance of the plugin * @param $kind [Integer] type of the plugin * @param $name [String] name of the plugin * @return [&Plugin] reference to the plugin */ public static function &getPlugin($kind, $name) { if (self::isPluginRegistered($kind, $name)) { $instance = &parent::getInstance(__CLASS__); return $instance->plugins[$kind][$name]['instance']; } //plugin is registered else { return null; } } //getPlugin() /** * @brief rises event and notifies all observers * @param $event [Integer] event to rise * @param $params [Array] params for the event * @return [Array of Strings] results from all observers */ public function riseEvent($event, $params) { $result = array(); foreach($this->events[$event] as $plugin){ $result[$plugin['observer']] = $plugin['instance']->observe($event, $params); } return $result; } //riseEvent() /** * @brief executes command using registereg plugin * @param $command [String] * @param $params [String] * @return [Boolean/String] result of the command (false on unknown command) */ public static function executeCommand($command, $params) { parent::createInstance(__CLASS__); $instance = &parent::getInstance(__CLASS__); if (isset($instance->commands[$command])) { return $instance->commands[$command]['instance']->execute($command, $params); } else { false; } } } //class Plugins ?>