Advertisement
fruffl

Understand Signal

Nov 29th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.25 KB | None | 0 0
  1.     class adapterTarget
  2.     {
  3.         public function target($__world)
  4.         {
  5.             var_dump('EMIT:      '.__METHOD__.':'.$__world.' World!');
  6.             var_dump('ENDOF: '.__METHOD__);
  7.         }
  8.     }
  9.    
  10.     class signalConstructor
  11.     {
  12.         public function preEvent($arg1) { var_dump('EMIT:      '.__METHOD__.':'.$arg1); }
  13.         public function postEvent($arg1) { var_dump('EMIT:      '.__METHOD__.':'.$arg1); }
  14.     }
  15.    
  16.     class signalTarget_1
  17.     {
  18.         public function preEvent($arg1) { var_dump('EMIT:      '.__METHOD__.':'.$arg1); }
  19.         public function postEvent($arg1) { var_dump('EMIT:      '.__METHOD__.':'.$arg1); }
  20.     }
  21.    
  22.     class signalTarget_2
  23.     {
  24.         public function preEvent($arg1) { var_dump('EMIT:      '.__METHOD__.':'.$arg1); }
  25.         public function postEvent($arg1) { var_dump('EMIT:      '.__METHOD__.':'.$arg1); }
  26.     }
  27.    
  28.     $SignalTarget_1     = new SignalTarget_1;
  29.     $SignalTarget_2     = new SignalTarget_2;
  30.     $signalConstructor  = new signalConstructor;
  31.    
  32.     $foo = new foo([
  33.         'myInitArg1' => 'my Init Arg 1', // overwrite init option myInitArg1
  34.         '__signal' => [
  35.             '__construct' =>
  36.             [
  37.                 'PRE_EVENT' =>
  38.                 [
  39.                     0   => [$signalConstructor, 'preEvent'],
  40.                 ],
  41.                 'POST_EVENT' =>
  42.                 [
  43.                     0   => [$signalConstructor, 'postEvent']
  44.                 ],
  45.                
  46.             ],
  47.             'baz' =>
  48.             [
  49.                 'PRE_EVENT' =>
  50.                 [
  51.                     0   => [$SignalTarget_1, 'preEvent'],
  52.                     557 => [$SignalTarget_2, 'preEvent']
  53.                 ],
  54.                 'POST_EVENT' =>
  55.                 [
  56.                     1337    => [$SignalTarget_1, 'postEvent'],
  57.                     42  => [$SignalTarget_2, 'postEvent']
  58.                 ]
  59.             ]
  60.         ],
  61.         '__adapter' => [
  62.             'baz' => [new adapterTarget, 'target']  // register an adapter for method foo::baz
  63.         ],
  64.     ]);
  65.    
  66.    
  67.     $foo
  68.     /*
  69.     ->defineAdapterDelegate('baz', 'target', new adapterTarget)
  70.     ->defineSignalDelegate('baz', 'pre', 1, 'method_1', $SignalTarget_1)
  71.     ->defineSignalDelegate('baz', 'pre', 2, 'method_1', $SignalTarget_2)
  72.     ->defineSignalDelegate('baz', 'post', 1, 'method_2', $SignalTarget_1)
  73.     ->defineSignalDelegate('baz', 'post', 2, 'method_2', $SignalTarget_2)
  74.     */
  75.     ->baz('Hello')
  76.     ->enableSignal()
  77.     ->baz('Hello')
  78.     ->enableAdapter()
  79.     ->baz('Hi')
  80.     ;
  81.  
  82. /*
  83. string(46) "BEGIN: ILLI\Base\tBase::Base_tBase___construct"
  84. string(57) "EMIT:      root\signalConstructor::preEvent:my Init Arg 1"
  85. string(31) "EXEC:          root\foo::__main"
  86. string(58) "EMIT:      root\signalConstructor::postEvent:my Init Arg 1"
  87. string(46) "ENDOF: ILLI\Base\tBase::Base_tBase___construct"
  88. string(20) "BEGIN: root\foo::baz"
  89. string(34) "EXEC:          root\foo::baz:Hello"
  90. string(20) "ENDOF: root\foo::baz"
  91. string(29) "SET:   root\foo::enableSignal"
  92. string(20) "BEGIN: root\foo::baz"
  93. string(46) "EMIT:      root\signalTarget_1::preEvent:Hello"
  94. string(46) "EMIT:      root\signalTarget_2::preEvent:Hello"
  95. string(34) "EXEC:          root\foo::baz:Hello"
  96. string(47) "EMIT:      root\signalTarget_2::postEvent:Hello"
  97. string(47) "EMIT:      root\signalTarget_1::postEvent:Hello"
  98. string(20) "ENDOF: root\foo::baz"
  99. string(30) "SET:   root\foo::enableAdapter"
  100. string(20) "BEGIN: root\foo::baz"
  101. string(47) "EMIT:      root\adapterTarget::target:Hi World!"
  102. string(33) "ENDOF: root\adapterTarget::target"
  103. */
  104.  
  105.  
  106.  
  107.  
  108.  
  109.     class foo EXTENDS \ILLI\Base\aBase
  110.     {
  111.         protected $__autoConfig =
  112.         [
  113.             'myInitArg1'
  114.         ];
  115.        
  116.         protected $__defaultConfig =
  117.         [
  118.             'myInitArg1' => 'user'
  119.         ];
  120.        
  121.         public $a;
  122.         public $s;
  123.         public function enableAdapter() { var_dump('SET:   '.__METHOD__); $this->a = TRUE; return $this; }
  124.         public function enableSignal()  { var_dump('SET:   '.__METHOD__); $this->s = TRUE; return $this; }
  125.        
  126.        
  127.         protected function __main()
  128.         {
  129.             var_dump('EXEC:          '.__METHOD__);
  130.         }
  131.        
  132.         public function baz($__hello)
  133.         {
  134.             var_dump('BEGIN: '.__METHOD__);
  135.            
  136.             // forward
  137.             if(TRUE === $this->a)
  138.                 // send params
  139.                 return $this->Base_Component_Delegate_Hook_tAdapter_emit(__METHOD__, ['__world' => $__hello]); // strict args: true
  140.            
  141.             // send args to pre-signal
  142.             if(TRUE === $this->s)
  143.                 // send params
  144.                 $this->Base_Component_Delegate_Hook_tSignal_emit(__Method__, 'PRE_EVENT', [$__hello]); // strict args: false
  145.                
  146.             // do method stuff
  147.             var_dump('EXEC:          '.__METHOD__.':'.$__hello);
  148.             // end method stuff
  149.            
  150.            
  151.             // filter result
  152.             // ...
  153.            
  154.             // observer notify filtered result
  155.             // ...
  156.            
  157.             // send filtered result to post-signal
  158.             if(TRUE === $this->s)
  159.                 $this->Base_Component_Delegate_Hook_tSignal_emit(__Method__, 'POST_EVENT', [$__hello]);
  160.            
  161.             var_dump('ENDOF: '.__METHOD__);
  162.            
  163.             // return filtered result or instance
  164.             return $this;
  165.         }
  166.     }
  167.  
  168.  
  169.     ABSTRACT CLASS aBase
  170.     {
  171.         use tBase
  172.         {
  173.             //Base_tBase___main AS protected __main;
  174.             Base_tBase___set_state  AS public __set_state;
  175.             Base_tBase_toArray  AS public toArray;
  176.             Base_tBase_getParents   AS public getParents;
  177.         }
  178.        
  179.         USE tAdapter
  180.         {
  181.             Base_Component_Delegate_Hook_tAdapter_defineInvoke  as public defineAdapterInvoke;
  182.             Base_Component_Delegate_Hook_tAdapter_defineDelegate    as public defineAdapterDelegate;
  183.         }
  184.        
  185.         USE tSignal
  186.         {
  187.             Base_Component_Delegate_Hook_tSignal_defineInvoke   as public defineSignalInvoke;
  188.             Base_Component_Delegate_Hook_tSignal_defineDelegate as public defineSignalDelegate;
  189.         }
  190.        
  191.         ### USE tFilter ... // stub
  192.         ### USE tObserver ... // stub
  193.        
  194.         protected $__runtimeConfig      = [];
  195.         protected $__defaultConfig      = [];
  196.         protected $__autoConfig         = [];
  197.        
  198.         /**
  199.          * @discuss final constructor with protected construct-event
  200.          * @see construct()
  201.          */
  202.         public function __construct(array $__options = [])
  203.         {
  204.             // reference from protected to private scope
  205.             $this->__Base_tBase_runtimeConfig   =& $this->__runtimeConfig;
  206.            
  207.             // copy from protected into private scope
  208.             $this->__Base_tBase_autoConfig      = $this->__autoConfig;
  209.             $this->__Base_tBase_defaultConfig   = $this->__defaultConfig;
  210.            
  211.             $this->Base_tBase___construct($__options);
  212.         }
  213.        
  214.         /**
  215.          * construct-event instead of __construct()-overwrites
  216.          */
  217.         protected function __main()
  218.         {
  219.         }
  220.     }
  221.  
  222.  
  223.  
  224.     TRAIT tBase
  225.     {
  226.        
  227.         protected static $__Base_tBase_parentClasses    = [];
  228.        
  229.         /**
  230.          * allowed/expected fields: [optionName1 => flag, optionName2, ...]
  231.          */
  232.         private $__Base_tBase_autoConfig    = [];
  233.        
  234.         /**
  235.          * the method-argument
  236.          */
  237.         private $__Base_tBase_initConfig    = [];
  238.        
  239.         /**
  240.          * default options in environment
  241.          */
  242.         private $__Base_tBase_defaultConfig = [];
  243.        
  244.         /**
  245.          * runtime options in environment
  246.          */
  247.         private $__Base_tBase_runtimeConfig = [];
  248.        
  249.         /**
  250.          * copy values from init-scope to runtime-scope by auto-scope.
  251.          *
  252.          * auto-scope as expected optionName:
  253.          *  init-scope not set, use default-scope
  254.          *  init-scope and default-scope not set, create 'runtime[optionName] = NULL'
  255.          *
  256.          * @return this
  257.          */
  258.         protected function Base_tBase_init()
  259.         {
  260.             foreach($this->__Base_tBase_autoConfig as $key => $flag)
  261.             {
  262.                 // option not given. use default
  263.                 if(!isset($this->__Base_tBase_initConfig[$key])
  264.                 && isset($this->__Base_tBase_defaultConfig[$key]))
  265.                 {
  266.                     $this->__Base_tBase_runtimeConfig[$key] = $this->__Base_tBase_defaultConfig[$key];
  267.                     continue;
  268.                 }
  269.                
  270.                 if(!isset($this->__Base_tBase_initConfig[$flag])
  271.                 && isset($this->__Base_tBase_defaultConfig[$flag]))
  272.                 {
  273.                     $this->__Base_tBase_runtimeConfig[$flag] = $this->__Base_tBase_defaultConfig[$flag];
  274.                     continue;
  275.                 }
  276.                
  277.                 if($flag === 'merge') // init + default
  278.                 {
  279.                     if(isset($this->__Base_tBase_defaultConfig[$key])) // if exists...
  280.                     {
  281.                         $this->__Base_tBase_runtimeConfig[$key] = $this->__Base_tBase_initConfig[$key] + $this->__Base_tBase_defaultConfig[$key];
  282.                     }
  283.                     else
  284.                     {
  285.                         $this->__Base_tBase_runtimeConfig[$key] = $this->__Base_tBase_initConfig[$key];
  286.                     }
  287.                 }
  288.                 else // init[${key}]
  289.                 if(isset($this->__Base_tBase_initConfig[$key]))
  290.                 {
  291.                     $this->__Base_tBase_runtimeConfig[$flag] = $this->__Base_tBase_initConfig[$flag];
  292.                 }
  293.                 else // init[${flag}]
  294.                 if(isset($this->__Base_tBase_initConfig[$flag]))
  295.                 {
  296.                     $this->__Base_tBase_runtimeConfig[$flag] = $this->__Base_tBase_initConfig[$flag];
  297.                 }
  298.                 else // default[${key}]
  299.                 if(isset($this->__Base_tBase_defaultConfig[$key]))
  300.                 {
  301.                     $this->__Base_tBase_runtimeConfig[$key] = $this->__Base_tBase_defaultConfig[$key];
  302.                 }
  303.                 else // default[${flag}]
  304.                 if(isset($this->__Base_tBase_defaultConfig[$flag]))
  305.                 {
  306.                     $this->__Base_tBase_runtimeConfig[$flag] = $this->__Base_tBase_defaultConfig[$flag];
  307.                 }
  308.                 else
  309.                 {
  310.                     $this->__Base_tBase_runtimeConfig[$flag] = NULL;
  311.                 }
  312.             }
  313.            
  314.             return $this;
  315.         }
  316.        
  317.         protected static function Base_tBase___set_state($data)
  318.         {
  319.             $this->__export = $data;
  320.             $class = get_called_class();
  321.             $object = new $class();
  322.        
  323.             foreach($object as $property => $value)
  324.                 $object->{$property} = $value;
  325.            
  326.             return $object;
  327.         }
  328.        
  329.         protected static function Base_tBase_getParents($__className = NULL)
  330.         {
  331.             $class = NULL === $__className ? get_called_class() : $__className;
  332.        
  333.             if(!isset(self::$__Base_tBase_parentClasses[$class]))
  334.                 self::$__Base_tBase_parentClasses[$class] = class_parents($class);
  335.            
  336.             return self::$__Base_tBase_parentClasses[$class] ;
  337.         }
  338.        
  339.         protected function Base_tBase_toArray()
  340.         {
  341.             $array  = [];
  342.        
  343.             foreach($this as $property => $value)
  344.             {
  345.                 if(substr($property, 0, 2) === '__')
  346.                     continue;
  347.                
  348.                 if(is_object($value))
  349.                     if(TRUE === ($value instanceOf aBase))
  350.                     {
  351.                         $array[$property] = $value->toArray();
  352.                         continue;
  353.                     }
  354.                     else
  355.                     {
  356.                         $array[$property] = (array) $value;
  357.                         continue;
  358.                     }
  359.                
  360.                 $array[$property] = $value;
  361.             }
  362.            
  363.             return $array;
  364.         }
  365.        
  366.         protected function Base_tBase___construct(array $__options = [])
  367.         {
  368.             var_dump('BEGIN: '.__METHOD__);
  369.             $defaults =
  370.             [
  371.                 '__init'    => TRUE,        // excecute init
  372.                 '__main'    => '__main',        // onConstructEvent: a protected method
  373.                 '__signal' =>   [
  374.                     __FUNCTION__ => [
  375.                         'PRE_EVENT' =>  [], // signal 1, signal 2, ...
  376.                         'POST_EVENT' => []  // signal 1, signal 2, ...
  377.                     ],
  378.                 ],
  379.                 '__observer'    =>
  380.                 [
  381.                     __FUNCTION__ => []  // notify 1, notify 2, ...
  382.                 ],
  383.                 '__filter'  =>
  384.                 [
  385.                     __FUNCTION__ => []  // filter 1, filter 2, ...
  386.                 ],
  387.                 '__adapter' =>
  388.                 [
  389.                     __FUNCTION__ => []  // 0 => class|instance, 1 => method
  390.                 ],
  391.                 '__method'  =>
  392.                 [
  393.                     []          // closures
  394.                 ]
  395.             ];
  396.            
  397.             $this->__Base_tBase_initConfig = $__options + $defaults;
  398.            
  399.             if(TRUE === $this->__Base_tBase_initConfig['__init'])
  400.             {
  401.                 // load runtime args
  402.                 $this->Base_tBase_init();
  403.                
  404.                 // setup
  405.                
  406.                 foreach($this->__Base_tBase_initConfig['__signal'] as $method => $events)
  407.                     foreach($events as $event => $hook)
  408.                         foreach($hook as $slotIdent => $delegate)
  409.                             $this->Base_Component_Delegate_Hook_tSignal_register($method, $event, $slotIdent, $delegate, FALSE);
  410.                        
  411.                 foreach($this->__Base_tBase_initConfig['__observer'] as $methodEvent => $observers)
  412.                     foreach($observers as $delegate); // stub
  413.                         ### $this->Base_Component_Delegate_Hook_tObserver_register($methodEvent, $delegate, FALSE);
  414.                        
  415.                 foreach($this->__Base_tBase_initConfig['__adapter'] as $method => $delegate)
  416.                     //foreach($adapters as $target => $delegate)
  417.                         $this->Base_Component_Delegate_Hook_tAdapter_register($method, $delegate, FALSE);
  418.                        
  419.                 foreach($this->__Base_tBase_initConfig['__filter'] as $method => $filters)
  420.                     foreach($filters as $filter); // stub
  421.                         ### $this->Base_Component_Delegate_Hook_tFilter_register($method, $filter, FALSE);
  422.                
  423.                 // execute PRE_EVENT
  424.                 $this->Base_Component_Delegate_Hook_tSignal_emit(get_called_class().'::__construct', 'PRE_EVENT', $this->__runtimeConfig);
  425.                
  426.                 // execute __main
  427.                 $this->{$this->__Base_tBase_initConfig['__main']}();
  428.                
  429.                 // filter result
  430.                 ### $filtered = $this->Base_Component_Delegate_Hook_tFilter_emit(__Method__, [$this->__runtimeConfig]);
  431.                 $filtered = $this->__runtimeConfig; // stub
  432.                
  433.                 // observer notify with filtered result
  434.                 ### $this->Base_Component_Delegate_Hook_tObserver_emit(__Method__, $filtered);
  435.                
  436.                 // execute POST_EVENT
  437.                 $this->Base_Component_Delegate_Hook_tSignal_emit(get_called_class().'::__construct', 'POST_EVENT', $this->__runtimeConfig);
  438.             }
  439.             var_dump('ENDOF: '.__METHOD__);
  440.         }
  441.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement