Skip to content
Permalink
Browse files

Implementing decorators for event callables.

First commit, proof of concept like, tests will follow.
  • Loading branch information...
burzum committed May 17, 2016
1 parent 3e83eff commit 86baa700bc409e2846702c2de6de6966cdef917e
Showing with 144 additions and 2 deletions.
  1. +78 −0 src/Event/Decorator/EventDecorator.php
  2. +63 −0 src/Event/Decorator/FilterDecorator.php
  3. +3 −2 src/Event/EventManager.php
@@ -0,0 +1,78 @@
<?php
/**
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP Project
* @since 3.3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Event\Decorator;
class EventDecorator {
/**
* Callable
*
* @var callable
*/
protected $_callable;
/**
* Decorator options
*
* @var array
*/
protected $_options = [];
/**
* Constructor.
*
* @param $callable $callable Callable.
* @param array $options Decorator options.
*/
public function __construct(callable $callable, array $options = [])
{
$this->_callable = $callable;
$this->_options = $options;
}
/**
* Invoke
*
* @link http://php.net/manual/en/language.oop5.magic.php#object.invoke
* @return mixed
*/
public function __invoke()
{
return $this->call($this->_callable, func_get_args());
}
/**
* Calls a callable with the passed arguments.
*
* @param $callable The callable.
* @param array $args Arguments for the callable.
* @return mixed
*/
public function call(callable $callable, $args)
{
switch (count($args)) {
case 0:
return $callable();
case 1:
return $callable($args[0]);
case 2:
return $callable($args[0], $args[1]);
case 3:
return $callable($args[0], $args[1], $args[2]);
default:
return call_user_func_array($callable, $args);
}
}
}
@@ -0,0 +1,63 @@
<?php
/**
* CakePHP : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP Project
* @since 3.3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Event\Decorator;
use RuntimeException;
class FilterDecorator extends EventDecorator {
/**
* @inheritdoc
*/
public function __invoke() {
$args = func_get_args();
if (!$this->canTrigger($args[0])) {
return false;
}
return call_user_func_array('parent::__invoke', $args);
}
/**
* Checks if the event is triggered for this listener.
*
* @param \Cake\Event\Event $event Event object.
* @return bool
*/
public function canTrigger(Event $event)
{
$if = $this->_evaluateCondition('if', $event);
$unless = $this->_evaluateCondition('unless', $event);
return $if && !$unless;
}
/**
* Evaluates the filter conditions
*
* @param string $condition Condition type
* @param \Cake\Event\Event $event Event objekt
* @return bool
*/
protected function _evaluateCondition($condition, Event $event)
{
if (!isset($this->_options[$condition])) {
return true;
}
if (!is_callable($this->_options[$condition])) {
throw new RuntimeException('Is not a callable!');
}
return $this->_options[$condition]($event);
}
}
@@ -14,6 +14,7 @@
*/
namespace Cake\Event;
use Cake\Event\Decorator\FilterDecorator;
use InvalidArgumentException;
/**
@@ -157,14 +158,14 @@ public function on($eventKey = null, $options = [], $callable = null)
$argCount = func_num_args();
if ($argCount === 2) {
$this->_listeners[$eventKey][static::$defaultPriority][] = [
'callable' => $options
'callable' => new FilterDecorator($options),
];
return;
}
if ($argCount === 3) {
$priority = isset($options['priority']) ? $options['priority'] : static::$defaultPriority;
$this->_listeners[$eventKey][$priority][] = [
'callable' => $callable
'callable' => new FilterDecorator($callable, $options)
];
return;
}

0 comments on commit 86baa70

Please sign in to comment.
You can’t perform that action at this time.