View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

{zone-data:component-name}
Zend_Plugin
{zone-data}

{zone-data:proposer-list}
[Keith Pope|mailto:keith@clevercherry.com] [Shaun Mitchell|mailto:shaun@verticalevolution.com]
{zone-data}

{zone-data:revision}
0.1 - 07 July 2006: Initial Submission
0.5 - 21 July 2006: First Draft
0.6 - 02 September: Added Listeners
0.7 - 12 September: Updated Use-case
{zone-data}

{zone-data:overview}
Zend_Plugin provides a simple way for developers to add a plugin architecture to their application.
{zone-data}

{zone-data:references}
* PHP|Architect Magazine Article - Developing a plugin architecture by Titus Barik
{zone-data}

{zone-data:requirements}
* Implement a plugin contract that all plugins will abide by.
* Have a plugin loader / Manager.
* Enable developers to add hooks and filters into their application.
* Allow plugins to send notifications and optional data
{zone-data}

{zone-data:dependencies}
* Zend_Exception
{zone-data}

{zone-data:operation}
Developers should be able to load a plugin or set of plugins and then add hooks/filters to these plugins anywhere in their application.
Secondly plugins can be send notifications of their state and send optional data with the notification.
{zone-data}

{zone-data:class-list}
* Zend_Plugin_Exception
* Zend_Plugin
* Zend_Plugin_Interface
* Zend_Plugin_Event
{zone-data}

{zone-data:use-cases}
||UC-01 Simple Use||

* Creating plugins
{code}

class myPlugin implements Zend_Plugin_Interface
{
public function introspect()
{
Zend_Plugin::register('filter', 'preBlogPost', array($this, 'codeHighlight'), 1);
Zend_Plugin::register('filter', 'preBlogPost', array($this, 'wikifi'), 1);
Zend_Plugin::register('action', 'postBlogPost', array($this, 'logPost'), 1);
}

public function codeHighlight($blogPost)
{
$blogPost = $blogPost . "|highlighted|";
return $blogPost;
}

public function wikifi($blogPost)
{
$blogPost = $blogPost . "|wikified|";
return $blogPost;
}

public function logPost($username)
{
//write log file....
$data = chr(10) . "post from " . $username . ' logged @ ' . date(DATE_COOKIE) . chr(10);
//...writing log
}

public function __sleep()
{

}

public function __wakeup()
{

}
}

{code}

* Sending events

{code}

include 'Zend.php';
include 'Plugin.php';

Zend_Plugin::addPlugin('myPlugin',1);

$blog = "<p>My new blog post</p>";

$blog = Zend_Plugin::applyFilters('preBlogPost',$blog);

Zend_Plugin::doAction('postBlogPost','Keith Pope');

echo $blog;

{code}

||UC-02 Use with notifications||

* Creating plugins
{code}

class myPlugin implements Zend_Plugin_Interface
{
public function introspect()
{
Zend_Plugin::register('filter', 'preBlogPost', array($this, 'codeHighlight'), 1);
Zend_Plugin::register('filter', 'preBlogPost', array($this, 'wikifi'), 1);
Zend_Plugin::register('action', 'postBlogPost', array($this, 'logPost'), 1);
}

public function codeHighlight($blogPost)
{
$blogPost = $blogPost . "|highlighted|";
return $blogPost;
}

public function wikifi($blogPost)
{
$blogPost = $blogPost . "|wikified|";
return $blogPost;
}

public function logPost($username)
{
//write log file....
$data = chr(10) . "post from " . $username . ' logged @ ' . date(DATE_COOKIE) . chr(10);
Zend_Plugin::notify('myPlugin','postBlogPost',true,$data);
}

public function __sleep()
{

}

public function __wakeup()
{

}
}


class myPlugin2 implements Zend_Plugin_Interface
{
public function introspect()
{
Zend_Plugin::register('filter', 'preBlogPost', array($this, 'appendDate'), 1);
Zend_Plugin::register('filter', 'preBlogPost', array($this, 'prependDate'), 1);
}

public function appendDate($blogPost)
{
$blogPost = $blogPost . chr(10) . date(DATE_COOKIE);
return $blogPost;
}

public function prependDate($blogPost)
{
$blogPost = date(DATE_COOKIE) . chr(10) . $blogPost;
return $blogPost;
}

public function __sleep()
{

}

public function __wakeup()
{

}
}

{code}

* Sending events

{code}

include 'Zend.php';
include 'Plugin.php';

//handler class for listeners - this can be a function also
class myHandlers
{
public function myPluginHandler($success,$data)
{
if($success) {
echo $data;
} else {
echo chr(10) . "log failed";
}
}
}
$mh = new myHandlers();

Zend_Plugin::addPluginGroup(array('myPlugin','myPlugin2'),array(1,2));
Zend_Plugin::addListener('myPlugin','postBlogPost',array($mh,'myPluginHandler')); //callback can be function - use string instead of array


$blog = "<p>My new blog post</p>";
$blog = Zend_Plugin::applyFilters('preBlogPost',$blog);

echo $blog;

Zend_Plugin::doAction('postBlogPost','Keith Pope');


{code}

||UC-03 Use with serialization||

Coming Soon...

{zone-data}

{zone-data:skeletons}
* Zend_Plugin_Exception
{code}
/**
* Class Zend_Plugin_Exception
*/
class Zend_Plugin_Exception extends Zend_Exception {}
{code}

* Zend_Plugin_Event
{code}
/**
* Class Zend_Plugin_Event
*/
class Zend_Plugin_Event
{
protected $eventId;
protected $eventType;
protected $eventName;
protected $callback;
protected $numArgs;

/**
* Zend_Plugin_Event Construct
*
* @param String $eType
* @param String $eName
* @param Array $eCallback
* @param Int $nArgs
* @param Int $ePriority
*/
public function __construct($eId,$eType,$eName,$eCallback,$nArgs)


/**
* Getter for our properties
*
* @param String $prop
* @return mixed
*/
public function __get($prop)

}
{code}
* Zend_Plugin_Interface
{code}

/**
* Interface Zend_Plugin_Interface
*/
interface Zend_Plugin_Interface
{
/**
* This is called by Zend_Plugin when the plugin is loaded
*
* Plugins should use this to register with hooks
*
*/
public function introspect();
public function __wakeup();
public function __sleep();

}
{code}
* Zend_Plugin
{code}
class Zend_Plugin
{
/**
* An Array containing our registered plugins
*
* @var Array
*/
static protected $_events = array();

/**
* An Array containing our plugin instances
*
* @var Array
*/
static protected $_plugins = array();

/**
* An Array containing paths to our plugin files
* This is used for serialization
*
* @var Array
*/
static protected $_pluginPaths = array();

/**
* An Array containing listeners for our events
*
* @var array
*/
static protected $_listeners = array();

/**
* Loads a plugin and stores an instance of the plugin in $_plugins
*
* @param String $pluginName
* @param Int $priority
* @param String $location
* @throws Zend_Plugin_Exception
*/
static public function addPlugin($pluginName, $priority = null, $location = null)


/**
* Loads a group of plugins
*
* @param Array $plugins
* @param Array $priority
* @param String $location
* @throws Zend_Plugin_Exception
*/
static public function addPluginGroup($plugins = array(), $priority = array(), $location = null)


/**
* Removes a loaded plugin from $_plugins by name
*
* @param String $pluginName
* @throws Zend_Plugin_Exception
*/
static public function removePlugin($pluginName)


/**
* Removes a group of loaded plugins from $_plugins
*
* @param Array $plugins
* @throws Zend_Plugin_Exception
*/
static public function removePluginGroup($plugins = array())


/**
* doAction calls our event
*
* @param String $eventName
* @param String $eventType
* @throws Zend_Plugin_Exception
*/
static public function doAction($eventName)


/**
* Applys filter
*
* @param String $eventName
* @param String $string
* @throws Zend_Plugin_Exception
* @return mixed
*/
static public function applyFilters($eventName, $string)


/**
* Registers a plugin to an event (see Zend_Plugin_Interface)
*
* Returns a unique identifier for the event
*
* @param String $eventType
* @param String $eventName
* @param Array $callBack
* @param Int $numArgs
* @return String
* @throws Zend_Plugin_Exception
*/
static public function register($eventType, $eventName, $callBack, $numArgs)


/**
* Unregister an event using its unique identifier
*
* @param String $eventId
* @throws Zend_Plugin_Exception
*/
static public function unregister($eventId)


/**
* Add a listener for a plugin event
*
* @param string $pluginName The plugin we want to listen to
* @param string $eventName The event we want to listen to
* @param array $callBack The callback function to call
* @return string listenerId A unique id for this listener
* @throws Zend_Plugin_Exception
*/
static public function addListener($pluginName,$eventName,$callBack)


/**
* Removes a listener
*
* @param string $listenerId
* @throws Zend_Plugin_Exception
* @todo Add error checking
*/
static public function removeListener($listenerId)


/**
* Notify listeners that an event has taken place,
* then call the callback function
*
* @param string $pluginName The plugin that is notifying
* @param string $eventName The event that is notifying
* @return Int $notifiedCount The number of listeners notified
*/
static public function notify($pluginName,$eventName)


/**
* Returns a serialized string of the $_events,$_plugins
*
* @return Array
*/
static public function sleep()



/**
* Restore from sleep
*
* @param String $serialized
*/
static public function wakeup($serialized)
{code}
{zone-data}

{zone-data:milestones}
...
{zone-template-instance}]]></ac:plain-text-body></ac:macro>