Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- /**
- * ILLI
- *
- * @category ILLI_Core_Pattern
- * @package ILLI
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- */
- NAMESPACE ILLI\Core\Pattern;
- USE ILLI\Core\Pattern\Concrete\Signal AS ConcreteSignal;
- USE ILLI\Core\Member\Method AS MemberMethod;
- USE ILLI\Core\Invoke\Method AS InvokeMethod;
- USE ILLI\Core\Invoke\Closure AS InvokeClosure;
- USE Closure AS __Closure;
- /**
- * ILLI Pattern TRAIT Signal
- *
- * @category ILLI_Core_Pattern
- * @package ILLI
- * @subpackage Core\Pattern
- * @namespace ILLI\Core\Pattern
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- * @since 2.0.1
- * @version 3.0.1
- */
- TRAIT tSignal
- {
- /**
- * @var array trace adapters
- */
- private $__tSignal_tracesEnabled = FALSE;
- /**
- * @var array trace adapters
- */
- private $__tSignal_traces = [];
- /**
- * @var array signals: spl::method => event => slot => ILLI\Core\Pattern\Concrete\Signal
- */
- protected $__tSignal_hook = [];
- /**
- * @var array results: spl::method => event => slot => mixed
- */
- protected $__tSignal_results = [];
- protected function tSignal_registerConcrete(ConcreteSignal $__ConcreteSignal)
- {
- $method = $__ConcreteSignal->getTrigger()->getMethod();
- $event = $__ConcreteSignal->getEvent();
- $slot = $__ConcreteSignal->getSlot();
- $stack = & $this->__tSignal_hook[$method][$event];
- if($slot === '*')
- {
- $stack[] = $__ConcreteSignal;
- }
- else
- {
- $stack[$slot] = $__ConcreteSignal;
- }
- return $this;
- }
- protected function tSignal_register
- (
- $__trigger,
- $__event,
- $__method,
- & $__DelegateInstance = NULL,
- array $__delegateConstructorArguments = [],
- $__slot = '*'
- )
- {
- if(is_string($__trigger))
- {
- if(FALSE === strpos($__trigger, '::'))
- $__trigger = get_called_class().'::'.$__trigger;
- $__trigger = new MemberMethod($__trigger);
- }
- else
- if(FALSE === ($__trigger instanceOf MemberMethod))
- {
- throw new \Exception('Type-Conflict in trigger');
- }
- if(is_string($__method))
- {
- if(FALSE === strpos($__method, '::'))
- $__method = get_called_class().'::'.$__method;
- $__method = new MemberMethod($__method);
- }
- if($__method instanceOf MemberMethod)
- {
- $__method = new InvokeMethod
- (
- $__method,
- ($__method->getClass() === get_called_class())
- ? $this
- : $__DelegateInstance,
- $__delegateConstructorArguments
- );
- }
- else
- if($__method instanceOf __Closure)
- {
- $__method = new InvokeClosure($__method, $this);
- }
- else
- if(FALSE === ($__method instanceOf InvokeMethod))
- {
- throw new \Exception('Type-Conflict in invoke');
- }
- $this->tSignal_registerConcrete(new ConcreteSignal
- (
- $__trigger,
- $__event,
- $__method,
- $__slot
- ));
- return $this;
- }
- protected function tSignal_unregister($__method, $__event, $__slot)
- {
- if(is_string($__method))
- {
- if(FALSE === strpos($__method, '::'))
- $__method = get_called_class().'::'.$__method;
- $__method = (new MemberMethod($__method))->getMethod();
- }
- if(FALSE === $this->tSignal_exists($__method))
- return $this;
- if(FALSE === $this->tSignal_exists($__method, $__event))
- {
- unset($this->__tSignal_hook[$__method]);
- return $this;
- }
- if(FALSE === $this->tSignal_exists($__method, $__event, $__slot))
- {
- unset($this->__tSignal_hook[$__method][$__event]);
- return $this;
- }
- unset($this->__tSignal_hook[$__method][$__event][$__slot]);
- return $this;
- }
- protected function tSignal_exists($__method, $__event = NULL, $__slot = NULL)
- {
- if(is_string($__method))
- {
- if(FALSE === strpos($__method, '::'))
- $__method = get_called_class().'::'.$__method;
- $__method = (new MemberMethod($__method))->getMethod();
- }
- $stack = & $this->__tSignal_hook;
- $m_exists = array_key_exists($__method, $stack);
- if($__event === NULL
- || FALSE === $m_exists)
- return $m_exists;
- $stack = & $stack[$__method];
- $e_exists = array_key_exists($__event, $stack);
- if($__slot === NULL
- || FALSE === $_exists)
- return $e_exists;
- $stack = & $stack[$__method][$__event];
- $s_exists = array_key_exists($__slot, $stack);
- return $s_exists;
- }
- protected function tSignal_send($__method, $__event)
- {
- if(is_string($__method))
- {
- if(FALSE === strpos($__method, '::'))
- $__method = get_called_class().'::'.$__method;
- $__method = (new MemberMethod($__method))->getMethod();
- }
- if(FALSE === $this->tSignal_exists($__method, $__event))
- return $this;
- $args = func_get_args();
- array_shift($args);
- array_shift($args);
- $referenced_args = [];
- foreach($args as $key => &$arg)
- $referenced_args[$key] = &$arg;
- if(TRUE === $this->__tSignal_tracesEnabled)
- $this->__tSignal_traces[$__method][] = debug_backtrace(0, 2);
- $stack = & $this->__tSignal_hook [$__method][$__event];
- $result = & $this->__tSignal_results [$__method][$__event];
- foreach($stack as $slot => $Slot)
- $result[$slot] = $Slot->invoke($referenced_args);
- }
- }
- ABSTRACT CLASS aPage EXTENDS aController
- {
- public function __construct($__method, $__DelegateInstance = NULL, array $__delegateConstructorArguments = [])
- {
- parent::__construct();
- $this->tSignal_register('render', 'preRender', 'exec', $this, func_get_args(), -555);
- $this->tSignal_register('render', 'postRender', 'finalize', $this, func_get_args(), -554);
- }
- public function exec()
- {
- var_dump(__METHOD__);
- }
- public function finalize($val)
- {
- var_dump(__METHOD__);
- var_dump($val);
- }
- public function render()
- {
- $this->tSignal_send('render', 'preRender');
- var_dump(__METHOD__);
- $this->tSignal_send('render', 'postRender', ['val']);
- }
- }
- class foo extends \ILLI\Core\Web\Ctrl\aPage
- {
- public function myAction($test = null)
- {
- $this->br = $test;
- }
- }
- $f = new foo('my', null, ['foobarbaz']);
- $f->render();
- var_dump($f);
- ?>
- string(30) "ILLI\Core\Web\Ctrl\aPage::exec"
- string(32) "ILLI\Core\Web\Ctrl\aPage::render"
- string(34) "ILLI\Core\Web\Ctrl\aPage::finalize"
- array(1) {
- [0]=>
- string(3) "val"
- }
- object(localhost\illiFW\dev\foo)#18 (11) {
- ["__tSignal_tracesEnabled":"ILLI\Core\Web\Ctrl\aController":private]=>
- bool(false)
- ["__tSignal_traces":"ILLI\Core\Web\Ctrl\aController":private]=>
- array(0) {
- }
- ["__tSignal_hook":protected]=>
- array(1) {
- ["localhost\illiFW\dev\foo::render"]=>
- array(2) {
- ["preRender"]=>
- array(1) {
- [-555]=>
- object(ILLI\Core\Pattern\Concrete\Signal)#321 (4) {
- ["__Trigger":"ILLI\Core\Pattern\Concrete\Signal":private]=>
- object(ILLI\Core\Member\Method)#315 (3) {
- ["__function":protected]=>
- string(6) "render"
- ["__method":protected]=>
- string(32) "localhost\illiFW\dev\foo::render"
- ["__class":protected]=>
- string(24) "localhost\illiFW\dev\foo"
- }
- ["__slot":"ILLI\Core\Pattern\Concrete\Signal":private]=>
- int(-555)
- ["__event":"ILLI\Core\Pattern\Concrete\Signal":private]=>
- string(9) "preRender"
- ["__Callable":protected]=>
- object(ILLI\Core\Invoke\Method)#317 (3) {
- ["__Instance":"ILLI\Core\Invoke\Method":private]=>
- *RECURSION*
- ["__Invokable":protected]=>
- object(ILLI\Core\Member\Method)#316 (3) {
- ["__function":protected]=>
- string(4) "exec"
- ["__method":protected]=>
- string(30) "localhost\illiFW\dev\foo::exec"
- ["__class":protected]=>
- string(24) "localhost\illiFW\dev\foo"
- }
- ["__tConstructMethod__constructorArguments":protected]=>
- array(3) {
- [0]=>
- string(2) "my"
- [1]=>
- NULL
- [2]=>
- array(1) {
- [0]=>
- string(9) "foobarbaz"
- }
- }
- }
- }
- }
- ["postRender"]=>
- array(1) {
- [-554]=>
- object(ILLI\Core\Pattern\Concrete\Signal)#325 (4) {
- ["__Trigger":"ILLI\Core\Pattern\Concrete\Signal":private]=>
- object(ILLI\Core\Member\Method)#322 (3) {
- ["__function":protected]=>
- string(6) "render"
- ["__method":protected]=>
- string(32) "localhost\illiFW\dev\foo::render"
- ["__class":protected]=>
- string(24) "localhost\illiFW\dev\foo"
- }
- ["__slot":"ILLI\Core\Pattern\Concrete\Signal":private]=>
- int(-554)
- ["__event":"ILLI\Core\Pattern\Concrete\Signal":private]=>
- string(10) "postRender"
- ["__Callable":protected]=>
- object(ILLI\Core\Invoke\Method)#324 (3) {
- ["__Instance":"ILLI\Core\Invoke\Method":private]=>
- *RECURSION*
- ["__Invokable":protected]=>
- object(ILLI\Core\Member\Method)#323 (3) {
- ["__function":protected]=>
- string(8) "finalize"
- ["__method":protected]=>
- string(34) "localhost\illiFW\dev\foo::finalize"
- ["__class":protected]=>
- string(24) "localhost\illiFW\dev\foo"
- }
- ["__tConstructMethod__constructorArguments":protected]=>
- array(3) {
- [0]=>
- string(2) "my"
- [1]=>
- NULL
- [2]=>
- array(1) {
- [0]=>
- string(9) "foobarbaz"
- }
- }
- }
- }
- }
- }
- }
- ["__tSignal_results":protected]=>
- array(1) {
- ["localhost\illiFW\dev\foo::render"]=>
- array(2) {
- ["preRender"]=>
- array(1) {
- [-555]=>
- NULL
- }
- ["postRender"]=>
- array(1) {
- [-554]=>
- NULL
- }
- }
- }
- ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement