[ Pobierz całość w formacie PDF ]
.Lastly out of the standard parameters is the published flag.When an event triggersplug-ins, there is a choice as to whether unpublished plug ins should be activated.For some events, there is no reason to activate a plug in that is not published.But forsomething like a plug in that processes text about to be sent to the browser, it maywell be necessary to activate an unpublished plug in.The reason for this is that plugins that operate on text may well process special codes in the text.If the plug in is not published, although it should not perform its normal functions, it should strip outany special codes that are intended for it.A plug in of this kind must, therefore, takeaccount of the published flag and work in one way if it is published, and another if itis not.[ 160 ]Chapter 7Any parameters beyond the first three are specific to the event, and each event mustdefine its own set of parameters.When the circumstances arise where plug ins areneeded, a "trigger" call is made to the plug in handler.The first parameter provided to the trigger will be the fourth parameter to the plug in, the second parameter to thetrigger will be the fifth parameter to the plug in, and so on.A plug in that handlesmore than one event may need to use the PHP function func_get_args to get anuncertain number of parameters.Plug ins are so varied in their functions that little else can be said about the internals.So let's turn to the mechanisms for triggering plug ins.Invoking Plug insThe Aliro class that manages plug ins is called aliroMambotHandler, in line with thehistoric name for plug ins of "mambots".The class is shown here shorn of backwards compatibility methods with the constructor code replaced by a comment:class aliroMambotHandler extends aliroCommonExtHandler{private static $instance;private static $defaults = array ('onIniEditor' =>'bot_nulleditor', 'onGetEditorContents' => 'bot_nulleditor','onEditorArea' => 'bot_nulleditor');private $_events=array();private $_bots=null;private $_bot_objects = array();public $timer;protected $extensiondir = '/mambots/';protected function __construct(){// Constructor code}public static function getInstance(){if (null == self::$instance) self::$instance =parent::getCachedSingleton('aliroMambotHandler');return self::$instance;}// The bulk of the work of running plugins is done here// The main method for invoking Aliro pluginspublic function trigger( $event, $args=null,$doUnpublished=false, $maxbot=0 ){[ 161 ]Handling Extensionsif ($args === null) $args = array();elseif (!is_array($args)) $args = array($args);$result = array();$botcount = 0;if (isset( $this->_events[$event] )) foreach ($this->_events[$event] as $botkey){$bot = $this->_bots[$botkey];if ($bot->isdefault){if (!isset($defaultbotkey)) $defaultbotkey = $botkey;}else{$botparams = new aliroParameters($bot->params);if ($doUnpublished OR $bot->published){$result[] = $this->runOneBot($botkey, $args, $event,$botparams, $bot->published);$botcount ++;if ($maxbot AND $botcount >= $maxbot) break;}}}if (0 == $botcount AND isset($defaultbotkey)) $result[] =$this->runOneBot($defaultbotkey, $args, $event, '', '1');return $result;}private function runOneBot ($botkey, $args, $event, $botparams,$published){if (isset($this->_bot_objects[$botkey])) $botobject =$this->_bot_objects[$botkey];else $botobject = $this->_bot_objects[$botkey] = new$this->_bots[$botkey]->class;array_unshift($args, $event, $botparams, $published);return call_user_func_array(array($botobject, 'perform'),$args);}// Trigger function for activating just one bot - provided forconvenience in callingpublic function triggerOnce($event, $args=null,$doUnpublished=false){return $this->trigger ($event, $args, $doUnpublished, 1);}}[ 162 ]Chapter 7As with other extension handlers, the class inherits from aliroCommonExtHandler,and is a singleton.It caters for the possibility that some events have default plug ins.So far, this applies to the system editor, where a default plain text processing "editor"is provided if no other is installed.The protected property $extensiondir is used bymethods in the parent class
[ Pobierz całość w formacie PDF ]