My favorites | Sign in
Logo
             
Search
for
Updated Nov 26, 2008 by haschek
Labels: Phase-Implementation, Plugins, Events
PluginManagerAndEventDispatcher  
How plugin manager and event dispatcher work.

The SandboxPluginmanager class combines plugin manager and event dispatcher to keep it simple.

Plugin manager

The plugin manager controls the plugin environment of the Sandbox Publisher. Beside storing the plugin folders, it includes plugin files, instantiates plugin classes and store those object instances. It can be accessed in local file, plugins and templates through the $this->pm object interface.

Adding folders

When a plugin class is requested for inclusion or instantiation, the class file is searched in all folders stored in a stack saved within the plugin manager object. Adding new folders to the stack will be done by

$this->pm->addFolder($folder);
// or
$this->pm->addFolder($folder, $sub);

Include a plugin

For library plugins it makes sense to include their files but instantiate their classes by yourself in your own object variables. Just say that you need a plugin:

$this->pm->need('PluginClassName');
$myPl = new PluginClassName();

The parameter specifies the name of the plugin file without .php. It can include an absolute server path or a path relative to the Sandbox root directory. If the plugin class is included successfully the method will return the absolte path name of the directory the plugin class file is located in. Otherwise it will return a false.

Get object of plugin class

You can get the object of an plugin class and use it instantly.

$pluginObject = $this->pm->load('PluginClassName'); // without .php extension
$pluginObject->usePluginMethod();

There is even a shortcut for the code above: $this->pm->PluginClassName->usePluginMethod().

The load method will return the object of the instantiated plugin class, or on failure it will throw an exception. The difference between need plus manual instantiation and load is that load will return the same object all the time because objects of instantiated plugin classes are stored by the plugin manager. This might be important when the plugin need to know its earlier running history (or even to safe memory cache and time).

Event dispatcher

The event dispatcher is inlcuded in the plugin manager. Currently it has 2 methods:

Subscribe an event handler

Publish an event

Other sources


Sign in to add a comment
Hosted by Google Code