Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class adapterTarget
- {
- public function target($__world)
- {
- var_dump('EMIT: '.__METHOD__.':'.$__world.' World!');
- var_dump('ENDOF: '.__METHOD__);
- }
- }
- class signalConstructor
- {
- public function preEvent($arg1) { var_dump('EMIT: '.__METHOD__.':'.$arg1); }
- public function postEvent($arg1) { var_dump('EMIT: '.__METHOD__.':'.$arg1); }
- }
- class signalTarget_1
- {
- public function preEvent($arg1) { var_dump('EMIT: '.__METHOD__.':'.$arg1); }
- public function postEvent($arg1) { var_dump('EMIT: '.__METHOD__.':'.$arg1); }
- }
- class signalTarget_2
- {
- public function preEvent($arg1) { var_dump('EMIT: '.__METHOD__.':'.$arg1); }
- public function postEvent($arg1) { var_dump('EMIT: '.__METHOD__.':'.$arg1); }
- }
- $SignalTarget_1 = new SignalTarget_1;
- $SignalTarget_2 = new SignalTarget_2;
- $signalConstructor = new signalConstructor;
- $foo = new foo([
- 'myInitArg1' => 'my Init Arg 1', // overwrite init option myInitArg1
- '__signal' => [
- '__construct' =>
- [
- 'PRE_EVENT' =>
- [
- 0 => [$signalConstructor, 'preEvent'],
- ],
- 'POST_EVENT' =>
- [
- 0 => [$signalConstructor, 'postEvent']
- ],
- ],
- 'baz' =>
- [
- 'PRE_EVENT' =>
- [
- 0 => [$SignalTarget_1, 'preEvent'],
- 557 => [$SignalTarget_2, 'preEvent']
- ],
- 'POST_EVENT' =>
- [
- 1337 => [$SignalTarget_1, 'postEvent'],
- 42 => [$SignalTarget_2, 'postEvent']
- ]
- ]
- ],
- '__adapter' => [
- 'baz' => [new adapterTarget, 'target'] // register an adapter for method foo::baz
- ],
- ]);
- $foo
- /*
- ->defineAdapterDelegate('baz', 'target', new adapterTarget)
- ->defineSignalDelegate('baz', 'pre', 1, 'method_1', $SignalTarget_1)
- ->defineSignalDelegate('baz', 'pre', 2, 'method_1', $SignalTarget_2)
- ->defineSignalDelegate('baz', 'post', 1, 'method_2', $SignalTarget_1)
- ->defineSignalDelegate('baz', 'post', 2, 'method_2', $SignalTarget_2)
- */
- ->baz('Hello')
- ->enableSignal()
- ->baz('Hello')
- ->enableAdapter()
- ->baz('Hi')
- ;
- /*
- string(46) "BEGIN: ILLI\Base\tBase::Base_tBase___construct"
- string(57) "EMIT: root\signalConstructor::preEvent:my Init Arg 1"
- string(31) "EXEC: root\foo::__main"
- string(58) "EMIT: root\signalConstructor::postEvent:my Init Arg 1"
- string(46) "ENDOF: ILLI\Base\tBase::Base_tBase___construct"
- string(20) "BEGIN: root\foo::baz"
- string(34) "EXEC: root\foo::baz:Hello"
- string(20) "ENDOF: root\foo::baz"
- string(29) "SET: root\foo::enableSignal"
- string(20) "BEGIN: root\foo::baz"
- string(46) "EMIT: root\signalTarget_1::preEvent:Hello"
- string(46) "EMIT: root\signalTarget_2::preEvent:Hello"
- string(34) "EXEC: root\foo::baz:Hello"
- string(47) "EMIT: root\signalTarget_2::postEvent:Hello"
- string(47) "EMIT: root\signalTarget_1::postEvent:Hello"
- string(20) "ENDOF: root\foo::baz"
- string(30) "SET: root\foo::enableAdapter"
- string(20) "BEGIN: root\foo::baz"
- string(47) "EMIT: root\adapterTarget::target:Hi World!"
- string(33) "ENDOF: root\adapterTarget::target"
- */
- class foo EXTENDS \ILLI\Base\aBase
- {
- protected $__autoConfig =
- [
- 'myInitArg1'
- ];
- protected $__defaultConfig =
- [
- 'myInitArg1' => 'user'
- ];
- public $a;
- public $s;
- public function enableAdapter() { var_dump('SET: '.__METHOD__); $this->a = TRUE; return $this; }
- public function enableSignal() { var_dump('SET: '.__METHOD__); $this->s = TRUE; return $this; }
- protected function __main()
- {
- var_dump('EXEC: '.__METHOD__);
- }
- public function baz($__hello)
- {
- var_dump('BEGIN: '.__METHOD__);
- // forward
- if(TRUE === $this->a)
- // send params
- return $this->Base_Component_Delegate_Hook_tAdapter_emit(__METHOD__, ['__world' => $__hello]); // strict args: true
- // send args to pre-signal
- if(TRUE === $this->s)
- // send params
- $this->Base_Component_Delegate_Hook_tSignal_emit(__Method__, 'PRE_EVENT', [$__hello]); // strict args: false
- // do method stuff
- var_dump('EXEC: '.__METHOD__.':'.$__hello);
- // end method stuff
- // filter result
- // ...
- // observer notify filtered result
- // ...
- // send filtered result to post-signal
- if(TRUE === $this->s)
- $this->Base_Component_Delegate_Hook_tSignal_emit(__Method__, 'POST_EVENT', [$__hello]);
- var_dump('ENDOF: '.__METHOD__);
- // return filtered result or instance
- return $this;
- }
- }
- ABSTRACT CLASS aBase
- {
- use tBase
- {
- //Base_tBase___main AS protected __main;
- Base_tBase___set_state AS public __set_state;
- Base_tBase_toArray AS public toArray;
- Base_tBase_getParents AS public getParents;
- }
- USE tAdapter
- {
- Base_Component_Delegate_Hook_tAdapter_defineInvoke as public defineAdapterInvoke;
- Base_Component_Delegate_Hook_tAdapter_defineDelegate as public defineAdapterDelegate;
- }
- USE tSignal
- {
- Base_Component_Delegate_Hook_tSignal_defineInvoke as public defineSignalInvoke;
- Base_Component_Delegate_Hook_tSignal_defineDelegate as public defineSignalDelegate;
- }
- ### USE tFilter ... // stub
- ### USE tObserver ... // stub
- protected $__runtimeConfig = [];
- protected $__defaultConfig = [];
- protected $__autoConfig = [];
- /**
- * @discuss final constructor with protected construct-event
- * @see construct()
- */
- public function __construct(array $__options = [])
- {
- // reference from protected to private scope
- $this->__Base_tBase_runtimeConfig =& $this->__runtimeConfig;
- // copy from protected into private scope
- $this->__Base_tBase_autoConfig = $this->__autoConfig;
- $this->__Base_tBase_defaultConfig = $this->__defaultConfig;
- $this->Base_tBase___construct($__options);
- }
- /**
- * construct-event instead of __construct()-overwrites
- */
- protected function __main()
- {
- }
- }
- TRAIT tBase
- {
- protected static $__Base_tBase_parentClasses = [];
- /**
- * allowed/expected fields: [optionName1 => flag, optionName2, ...]
- */
- private $__Base_tBase_autoConfig = [];
- /**
- * the method-argument
- */
- private $__Base_tBase_initConfig = [];
- /**
- * default options in environment
- */
- private $__Base_tBase_defaultConfig = [];
- /**
- * runtime options in environment
- */
- private $__Base_tBase_runtimeConfig = [];
- /**
- * copy values from init-scope to runtime-scope by auto-scope.
- *
- * auto-scope as expected optionName:
- * init-scope not set, use default-scope
- * init-scope and default-scope not set, create 'runtime[optionName] = NULL'
- *
- * @return this
- */
- protected function Base_tBase_init()
- {
- foreach($this->__Base_tBase_autoConfig as $key => $flag)
- {
- // option not given. use default
- if(!isset($this->__Base_tBase_initConfig[$key])
- && isset($this->__Base_tBase_defaultConfig[$key]))
- {
- $this->__Base_tBase_runtimeConfig[$key] = $this->__Base_tBase_defaultConfig[$key];
- continue;
- }
- if(!isset($this->__Base_tBase_initConfig[$flag])
- && isset($this->__Base_tBase_defaultConfig[$flag]))
- {
- $this->__Base_tBase_runtimeConfig[$flag] = $this->__Base_tBase_defaultConfig[$flag];
- continue;
- }
- if($flag === 'merge') // init + default
- {
- if(isset($this->__Base_tBase_defaultConfig[$key])) // if exists...
- {
- $this->__Base_tBase_runtimeConfig[$key] = $this->__Base_tBase_initConfig[$key] + $this->__Base_tBase_defaultConfig[$key];
- }
- else
- {
- $this->__Base_tBase_runtimeConfig[$key] = $this->__Base_tBase_initConfig[$key];
- }
- }
- else // init[${key}]
- if(isset($this->__Base_tBase_initConfig[$key]))
- {
- $this->__Base_tBase_runtimeConfig[$flag] = $this->__Base_tBase_initConfig[$flag];
- }
- else // init[${flag}]
- if(isset($this->__Base_tBase_initConfig[$flag]))
- {
- $this->__Base_tBase_runtimeConfig[$flag] = $this->__Base_tBase_initConfig[$flag];
- }
- else // default[${key}]
- if(isset($this->__Base_tBase_defaultConfig[$key]))
- {
- $this->__Base_tBase_runtimeConfig[$key] = $this->__Base_tBase_defaultConfig[$key];
- }
- else // default[${flag}]
- if(isset($this->__Base_tBase_defaultConfig[$flag]))
- {
- $this->__Base_tBase_runtimeConfig[$flag] = $this->__Base_tBase_defaultConfig[$flag];
- }
- else
- {
- $this->__Base_tBase_runtimeConfig[$flag] = NULL;
- }
- }
- return $this;
- }
- protected static function Base_tBase___set_state($data)
- {
- $this->__export = $data;
- $class = get_called_class();
- $object = new $class();
- foreach($object as $property => $value)
- $object->{$property} = $value;
- return $object;
- }
- protected static function Base_tBase_getParents($__className = NULL)
- {
- $class = NULL === $__className ? get_called_class() : $__className;
- if(!isset(self::$__Base_tBase_parentClasses[$class]))
- self::$__Base_tBase_parentClasses[$class] = class_parents($class);
- return self::$__Base_tBase_parentClasses[$class] ;
- }
- protected function Base_tBase_toArray()
- {
- $array = [];
- foreach($this as $property => $value)
- {
- if(substr($property, 0, 2) === '__')
- continue;
- if(is_object($value))
- if(TRUE === ($value instanceOf aBase))
- {
- $array[$property] = $value->toArray();
- continue;
- }
- else
- {
- $array[$property] = (array) $value;
- continue;
- }
- $array[$property] = $value;
- }
- return $array;
- }
- protected function Base_tBase___construct(array $__options = [])
- {
- var_dump('BEGIN: '.__METHOD__);
- $defaults =
- [
- '__init' => TRUE, // excecute init
- '__main' => '__main', // onConstructEvent: a protected method
- '__signal' => [
- __FUNCTION__ => [
- 'PRE_EVENT' => [], // signal 1, signal 2, ...
- 'POST_EVENT' => [] // signal 1, signal 2, ...
- ],
- ],
- '__observer' =>
- [
- __FUNCTION__ => [] // notify 1, notify 2, ...
- ],
- '__filter' =>
- [
- __FUNCTION__ => [] // filter 1, filter 2, ...
- ],
- '__adapter' =>
- [
- __FUNCTION__ => [] // 0 => class|instance, 1 => method
- ],
- '__method' =>
- [
- [] // closures
- ]
- ];
- $this->__Base_tBase_initConfig = $__options + $defaults;
- if(TRUE === $this->__Base_tBase_initConfig['__init'])
- {
- // load runtime args
- $this->Base_tBase_init();
- // setup
- foreach($this->__Base_tBase_initConfig['__signal'] as $method => $events)
- foreach($events as $event => $hook)
- foreach($hook as $slotIdent => $delegate)
- $this->Base_Component_Delegate_Hook_tSignal_register($method, $event, $slotIdent, $delegate, FALSE);
- foreach($this->__Base_tBase_initConfig['__observer'] as $methodEvent => $observers)
- foreach($observers as $delegate); // stub
- ### $this->Base_Component_Delegate_Hook_tObserver_register($methodEvent, $delegate, FALSE);
- foreach($this->__Base_tBase_initConfig['__adapter'] as $method => $delegate)
- //foreach($adapters as $target => $delegate)
- $this->Base_Component_Delegate_Hook_tAdapter_register($method, $delegate, FALSE);
- foreach($this->__Base_tBase_initConfig['__filter'] as $method => $filters)
- foreach($filters as $filter); // stub
- ### $this->Base_Component_Delegate_Hook_tFilter_register($method, $filter, FALSE);
- // execute PRE_EVENT
- $this->Base_Component_Delegate_Hook_tSignal_emit(get_called_class().'::__construct', 'PRE_EVENT', $this->__runtimeConfig);
- // execute __main
- $this->{$this->__Base_tBase_initConfig['__main']}();
- // filter result
- ### $filtered = $this->Base_Component_Delegate_Hook_tFilter_emit(__Method__, [$this->__runtimeConfig]);
- $filtered = $this->__runtimeConfig; // stub
- // observer notify with filtered result
- ### $this->Base_Component_Delegate_Hook_tObserver_emit(__Method__, $filtered);
- // execute POST_EVENT
- $this->Base_Component_Delegate_Hook_tSignal_emit(get_called_class().'::__construct', 'POST_EVENT', $this->__runtimeConfig);
- }
- var_dump('ENDOF: '.__METHOD__);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement