Skip to content

Commit

Permalink
dev: changed interface to work with pluginevent object and optional t…
Browse files Browse the repository at this point in the history
…arget pluginname for dispatch method
  • Loading branch information
mennodekker committed Jan 18, 2013
1 parent 4596d2b commit 98aaebf
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 18 deletions.
2 changes: 1 addition & 1 deletion application/core/LSYii_Application.php
Expand Up @@ -129,7 +129,7 @@ public function __construct($config = null)
$this->pluginManager->loadPlugins();

// @TODO: REMOVE TEST CODE BELOW
$this->getPluginManager()->dispatchEvent('dummyEvent');
$this->getPluginManager()->dispatchEvent(new PluginEvent('dummyEvent', $this));
}

/**
Expand Down
9 changes: 5 additions & 4 deletions application/extensions/Menu/MenuWidget.php
Expand Up @@ -83,12 +83,13 @@ public function run()
'alt' => $this->gT('LimeSurvey online manual'),
'image' => 'showhelp.png'
);

$event = new PluginEvent('afterAdminMenuLoaded', $this);
$event->set('menu', $menu);

$this->menu = $menu;

App()->getPluginManager()->dispatchEvent('afterAdminMenuLoaded', array($this));
$result = App()->getPluginManager()->dispatchEvent($event);

$menu = $this->menu;
$menu = $result->get('menu');

$this->render('adminmenu', compact('menu', 'imageUrl'));
}
Expand Down
69 changes: 69 additions & 0 deletions application/libraries/PluginManager/PluginEvent.php
@@ -0,0 +1,69 @@
<?php
class PluginEvent
{
protected $_event = '';

protected $_sender;

protected $_stop = false;

protected $_parameters = array();

/**
*
* @param string $event
* @param objeect $sender
* @return \PluginEvent
*/
public function __construct($event, $sender = null)
{
if (!is_null($sender) && is_object($sender))
{
$this->_sender = $sender;
}

$this->_event = $event;

return $this;
}

public function get($key, $default = null)
{
if (!array_key_exists($key, $this->_parameters))
{
return $default;
} else {
return $this->_parameters[$key];
}
}

public function getEventName()
{
return $this->_event;
}

public function getSender()
{
if (!is_null($this->_sender)) {
return $this->_sender;
} else {
return false;
}
}

public function set($key, $value)
{
$this->_parameters[$key] = $value;

return $this;
}

public function stop($bool = null)
{
if (!is_null($bool)) {
$this->_stop = (bool) $bool;
}

return (bool) $this->_stop;
}
}
29 changes: 20 additions & 9 deletions application/libraries/PluginManager/PluginManager.php
Expand Up @@ -79,20 +79,30 @@ public function unsubscribe(iPlugin $plugin, $event)

/**
* This function dispatches an event to all registered plugins.
* @param type $event Name of the event.
* @param type $params Parameters to be passed to the event handlers.
* @param PluginEvent $event Object holding all event properties
* @param string|array $target Optional name of plugin to fire the event on
*
* @return PluginEvent
*/
public function dispatchEvent($event, $params = array())
public function dispatchEvent(PluginEvent $event, $target = array())
{
$eventResults = array();
if (isset($this->subscriptions[$event]))
$eventName = $event->getEventName();
if (is_string($target)) {
$target = array($target);
}
if (isset($this->subscriptions[$eventName]))
{
foreach($this->subscriptions[$event] as $subscription)
foreach($this->subscriptions[$eventName] as $subscription)
{
$eventResults[get_class($subscription[0])] = call_user_func_array($subscription, $params);
if (!$event->stop()
&& (empty($target) || in_array($subscription[0], $target)))
{
call_user_func($subscription, $event);
}
}
}


return $event;
}

/**
Expand Down Expand Up @@ -174,7 +184,8 @@ public function loadPlugins()
{
$this->loadPlugin($pluginName, $id);
}
$this->dispatchEvent('afterPluginLoad'); // Alow plugins to do stuff after all plugins are loaded

//$this->dispatchEvent(new PluginEvent('afterPluginLoad', $this)); // Alow plugins to do stuff after all plugins are loaded
}
}
?>
11 changes: 7 additions & 4 deletions plugins/Example/Example.php
Expand Up @@ -12,7 +12,7 @@ public function __construct(PluginManager $pluginManager, $id)
$this->subscribe('afterAdminMenuLoaded');
}

public function helloWorld()
public function helloWorld(PluginEvent $event)
{
$count = (int) $this->get('count');
if ($count === false) $count = 0;
Expand All @@ -21,13 +21,16 @@ public function helloWorld()
$this->set('count', $count);
}

public function afterAdminMenuLoaded(MenuWidget $menu)
public function afterAdminMenuLoaded(PluginEvent $event)
{
$menu->menu['left'][]=array(
$menu = $event->get('menu', array());
$menu['left'][]=array(
'href' => "http://docs.limesurvey.org",
'alt' => $menu->gT('LimeSurvey online manual'),
'alt' => $event->getSender()->gT('LimeSurvey online manual'),
'image' => 'showhelp.png'
);

$event->set('menu', $menu);
}

}

0 comments on commit 98aaebf

Please sign in to comment.