Created
July 6, 2013 18:12
-
-
Save oligriffiths/5940720 to your computer and use it in GitHub Desktop.
Koowa default plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @version $Id$ | |
* @package Nooku_Plugins | |
* @subpackage Koowa | |
* @copyright Copyright (C) 2007 - 2012 Johan Janssens. All rights reserved. | |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> | |
* @link http://www.nooku.org | |
*/ | |
/** | |
* Default Koowa plugin | |
* | |
* Koowa plugins can handle a number of events that are dynamically generated. The following | |
* is a list of available events. This list is not meant to be exclusive. | |
* | |
* onBeforeController[Action] | |
* onAfterController[Action] | |
* where [Action] is Browse, Read, Edit, Add, Delete or any custom controller action | |
* | |
* onBeforeDatabase[Action] | |
* onAfterDatabase[Action] | |
* where [Action] is Select, Insert, Update or Delete | |
* | |
* You can create your own Koowa plugins very easily : | |
* | |
* <code> | |
* <?php | |
* class plgKoowaFoo extends plgKoowaDefault | |
* { | |
* public function onBeforeControllerBrowse(KEvent $event) | |
* { | |
* //The caller is a reference to the object that is triggering this event | |
* $caller = $event->caller; | |
* | |
* //The result is the actual result of the event, if this is an after event | |
* //the result will contain the result of the action. | |
* $result = $event->result; | |
* | |
* //The context object can also contain a number of custom properties | |
* print_r($context); | |
* } | |
* } | |
* </code> | |
* | |
* @author Johan Janssens <[email protected]> | |
* @package Nooku_Plugins | |
* @subpackage Koowa | |
*/ | |
abstract class PlgKoowaDefault extends KEventListener | |
{ | |
/** | |
* A JRegistry object holding the parameters for the plugin | |
* | |
* @var A JRegistry object | |
*/ | |
protected $_params = null; | |
/** | |
* The name of the plugin name, usually the component name. If different to the component name, $_package must also be specified. | |
* | |
* @var string | |
*/ | |
protected $_name = null; | |
/** | |
* The plugin type | |
* | |
* @var string | |
*/ | |
protected $_type = 'koowa'; | |
/** | |
* The package this plugin should respond to | |
* @var | |
*/ | |
protected $_package; | |
/** | |
* The events this plugin should respond do | |
* @var array | |
*/ | |
protected $_responders = array(); | |
/** | |
* Constructor | |
*/ | |
function __construct($dispatcher, $config = array()) | |
{ | |
if (isset($config['params'])) | |
{ | |
if ($config['params'] instanceof JRegistry) { | |
$this->_params = $config['params']; | |
} else { | |
$this->_params = new JRegistry; | |
if (version_compare(JVERSION, '1.6', '<')) { | |
$this->_params->loadINI($config['params']); | |
} else { | |
$this->_params->loadString($config['params'], 'INI'); | |
} | |
} | |
} | |
if ( isset( $config['name'] ) ) { | |
$this->_name = $config['name']; | |
} | |
if ( isset( $config['type'] ) ) { | |
$this->_type = $config['type']; | |
} | |
//Inject the identifier | |
$config['service_identifier'] = KService::getIdentifier('plg:koowa.'.$this->_name); | |
//Inject the service container | |
$config['service_container'] = KService::getInstance(); | |
//Inject the dispatcher | |
$config['dispatcher'] = KService::get('com://admin/default.event.dispatcher'); | |
parent::__construct(new KConfig($config)); | |
} | |
/** | |
* Loads the plugin language file | |
* | |
* @param string $extension The extension for which a language file should be loaded | |
* @param string $basePath The basepath to use | |
* @return boolean True, if the file has successfully loaded. | |
*/ | |
public function loadLanguage($extension = '', $basePath = JPATH_BASE) | |
{ | |
if(empty($extension)) { | |
$extension = 'plg_'.$this->_type.'_'.$this->_name; | |
} | |
return JFactory::getLanguage()->load( strtolower($extension), $basePath); | |
} | |
/** | |
* Get the event handlers of the listener and converts custom event handlers into generic ones, | |
* e.g. onBeforeDocumentControllerAdd is mapped to onBeforeControllerAdd. This is then converted back in __call() | |
* | |
* Event handlers always start with 'on' and need to be public methods | |
* | |
* @return array An array of public methods | |
*/ | |
public function getEventHandlers() | |
{ | |
$handlers = parent::getEventHandlers(); | |
foreach($handlers AS &$handler) | |
{ | |
if(!preg_match('#^on(Before|After)#', $handler)){ | |
//Get event parts | |
$parts = explode(' ', preg_replace('/(?<=\\w)([A-Z])/', ' \\1', $handler)); | |
if(count($parts) == 5){ | |
$name = strtolower($parts[2]); | |
$type = strtolower($parts[3]); | |
unset($parts[2]); | |
$newHandler = implode('',$parts); | |
//Ensure event doesn't already exist | |
if(!in_array($newHandler, $handlers)){ | |
if(!isset($this->_responders[$type])) $this->_responders[$type] = array(); | |
if(!isset($this->_responders[$type][$name])) $this->_responders[$type][$name] = array(); | |
$this->_responders[$type][$name][$handler] = $newHandler; | |
$handler = $newHandler; | |
} | |
} | |
} | |
} | |
return $handlers; | |
} | |
/** | |
* Determines if this plugin responds to this specific event | |
* | |
* @param $eventName | |
* @param KEvent $event | |
* @return bool | |
*/ | |
public function respondsTo($eventName, KEvent $event) | |
{ | |
return method_exists($this, $eventName) ?: ($this->_getCustomEvent($eventName, $event) ? true : false); | |
} | |
/** | |
* Returns the original custom event name or false if none exists | |
* | |
* @param $eventName | |
* @param KEvent $event | |
* @return bool|mixed | |
*/ | |
protected function _getCustomEvent($eventName, KEvent $event) | |
{ | |
$identifier = $event->caller->getIdentifier(); | |
$path = is_array($identifier->path) ? $identifier->path : explode('.',$identifier->path); | |
$type = $path[0]; | |
$name = $identifier->name; | |
//Ensure the package matches | |
$package = $this->_package ?: $this->_name; | |
if($this->_package && $identifier->package != $package) return false; | |
//Check if we have any events registered for the identifer type and name | |
if( isset($this->_responders[$type]) && | |
isset($this->_responders[$type][$name]) && | |
in_array($eventName, $this->_responders[$type][$name])){ | |
return array_search($eventName, $this->_responders[$type][$name]); | |
}else{ | |
return false; | |
} | |
} | |
/** | |
* Overridden call to allow custom event names like onBeforeDocumentControllerRead | |
* | |
* @param $method | |
* @param $arguments | |
* @return mixed | |
*/ | |
public function __call($method, $arguments) | |
{ | |
if(preg_match('#^on[A-Z]#', $method)){ | |
if($event = $this->_getCustomEvent($method, $arguments[0])){ | |
return call_user_func_array(array($this, $event), $arguments); | |
}else{ | |
return null; | |
} | |
} | |
return parent::__call($method, $arguments); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment