Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing decorators for event callables.
First commit, proof of concept like, tests will follow.
- Loading branch information
Florian Krämer
committed
May 17, 2016
1 parent
3e83eff
commit 86baa70
Showing
3 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
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