Advertisement
fruffl

MethodFilter

Jan 13th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.21 KB | None | 0 0
  1.  
  2.     class Collection extends \ILLI\Core\Collection
  3.     {
  4.         protected function prototypeMethod($data, $bar)
  5.         {
  6.             return get_defined_vars();
  7.         }
  8.        
  9.         public function method($data, $bar)
  10.         {
  11.             return $this->prototype(__FUNCTION__, get_defined_vars());
  12.         }
  13.     }
  14.  
  15.  
  16.     $test = new Collection;
  17.    
  18.     $test->applyMethodFilter('method', function($self, $params, $chain)
  19.     {
  20.         $params['data'][] = 'Starting filter';
  21.         $params = $chain->next($self, $params, $chain);
  22.         $params['data'][] = 'Ending filter';
  23.         return $params;
  24.     });
  25.    
  26.     $test->applyMethodFilter('method', function(Collection $self, array $params = [], \ILLI\Core\Filter $chain)
  27.     {
  28.         $params['data'][] = 'Starting inner filter';
  29.         $params = $chain->next($self, $params, $chain);
  30.         $params['data'][] = 'Ending inner filter';
  31.         return $params;
  32.     });
  33.    
  34.     $result = $test->method(['Starting test'], 'Hello World');
  35.     var_dump(compact('result'));
  36.  
  37. array(1) {
  38.   ["result"]=>
  39.   array(2) {
  40.     ["data"]=>
  41.     array(8) {
  42.       [0]=>
  43.       string(13) "Starting test"
  44.       [1]=>
  45.       string(26) "Starting outer method call"
  46.       [2]=>
  47.       string(15) "Starting filter"
  48.       [3]=>
  49.       string(21) "Starting inner filter"
  50.       [4]=>
  51.       string(27) "Proto method implementation"
  52.       [5]=>
  53.       string(19) "Ending inner filter"
  54.       [6]=>
  55.       string(13) "Ending filter"
  56.       [7]=>
  57.       string(24) "Ending outer method call"
  58.     }
  59.     ["bar"]=>
  60.     string(11) "Hello World"
  61.   }
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.         protected function prototype($__function, $__parameters)
  70.         {
  71.             if(isset($__parameters['this']))
  72.                 unset($__parameters['this']);
  73.                
  74.             $class = get_called_class();
  75.            
  76.             if(TRUE === $this->Core_Object_tAdapter_exists($class, $__function))
  77.                 return $this->Core_Object_tAdapter_emit($class, $__function, $__parameters);
  78.            
  79.             $this->Core_Object_tSignal_emit($class, $__function, 'PRE_EVENT', $__parameters);
  80.            
  81.             $data   = $this->Core_Object_tFilter_emit($class, $__function, $__parameters, ['event' => 'DATA']);
  82.            
  83.             $result = NULL;
  84.            
  85.             $data['data'][] = 'Starting outer method call';
  86.            
  87.             $result = $this->Core_Object_tMethodFilter_emit(get_called_class(), $__function, $data, function($self, $params, $chain) use ($__function, &$data, &$result)
  88.             {
  89.                 $params['data'][] = 'Proto method implementation';
  90.                
  91.                 if(TRUE === $this->Core_Object_tMethod_exists($__function))
  92.                 {
  93.                     $result = $this->Core_Object_tMethod_emit($__function, $data);
  94.                 }
  95.                 else
  96.                 {
  97.                     $proto = $__function === '__construct'
  98.                         ? $this->__initConfig['__main']
  99.                         : $this->__initConfig['__protoprefix'].$__function;
  100.                
  101.                     if($proto === $__function)
  102.                         throw new \Exception('Proto is called method '.$__function);
  103.                        
  104.                     if(method_exists($this, $proto))
  105.                         $result = $this->Core_Object_tInvoke_method($proto, $data);
  106.                 }
  107.                
  108.                 return $params;
  109.             });
  110.            
  111.             $result['data'][] = 'Ending outer method call';
  112.            
  113.             $result = [$result];
  114.            
  115.             $result = $this->Core_Object_tFilter_emit($class, $__function, $result, ['event' => 'RESULT']);
  116.             $this->Core_Object_tObserver_emit($class, $__function, $result);
  117.             $this->Core_Object_tSignal_emit($class, $__function, 'POST_EVENT', $result);
  118.            
  119.             return $result[0];
  120.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement