Advertisement
fruffl

AOP

Aug 20th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.26 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_System
  6.      * @package    ILLI
  7.      * @link       http://illi.be
  8.      * @license    http://l.illi.be
  9.      * @copyright  ILLI Conference
  10.      */
  11.     NAMESPACE ILLI\System;
  12.     USE ReflectionClass;
  13.     USE IteratorAggregate;
  14.  
  15.     /**
  16.      * ILLI Object
  17.      *
  18.      * singleton-support
  19.      * <code>
  20.      * <?PHP
  21.      *  $t1 = \ILLI\System\Rand::instanceGet(200); 
  22.      *  var_dump($t1);
  23.      *  $t1->__destruct();
  24.      *  $t2 = \ILLI\System\Rand::instanceGet(50);
  25.      *  var_dump($t2);
  26.      *  $foo = new \ILLI\System\Rand(15);
  27.      *  var_dump($foo);
  28.      * ?>
  29.      * </code>
  30.      *
  31.      * @category   ILLI_System
  32.      * @package    ILLI
  33.      * @subpackage System
  34.      * @namespace  ILLI\System
  35.      * @link       http://illi.be
  36.      * @license    http://l.illi.be
  37.      * @copyright  ILLI Conference
  38.      * @since      2.0.1
  39.      * @version    3.0.1
  40.      */
  41.     CLASS Object EXTENDS System IMPLEMENTS IteratorAggregate,
  42.         //iQueue
  43.         iArrayList
  44.         //iStack
  45.     {
  46.         USE tSplHashRegister
  47.         {
  48.             tSplHashRegister___register     as private  __splRegister;
  49.             tSplHashRegister___unregister       as private  __splUnregister;
  50.             tSplHashRegister_getCode        as public   splGetHashCode;
  51.             tSplHashRegister_getInstances       as public   splGetInstances;
  52.             tSplHashRegister_getSiblings        as public   splGetSiblingInstances;
  53.         }
  54.        
  55.         USE tAdapter
  56.         {
  57.             tAdapter_registerTrigger        as private  adapterRegisterTrigger;
  58.             tAdapter_unregisterTrigger      as protected    adapterUnregisterTrigger;
  59.             tAdapter_callAlias          as protected    adapterCallAlias;
  60.             tAdapter_triggerExists          as protected    adapterTriggerExists;
  61.             tAdapter_registerTriggerMethod      as public   adapterRegisterTriggerMethod;
  62.             tAdapter_registerTriggerStaticMethod    as public   adapterRegisterTriggerStaticMethod;
  63.             tAdapter_registerTriggerClosure     as public   adapterRegisterTriggerClosure;
  64.         }
  65.        
  66.         USE tObserver
  67.         {
  68.             tObserver_registerEvent         as private  observerRegisterEvent;
  69.             tObserver_unregisterEvent       as protected    observerUnregisterEvent;
  70.             tObserver_notify            as protected    observerNotify;
  71.             tObserver_eventExists           as protected    observerEventExists;
  72.             tObserver_registerEventMethod       as public   observerRegisterEventMethod;
  73.             tObserver_registerEventStaticMethod as public   observerRegisterEventStaticMethod;
  74.             tObserver_registerEventClosure      as public   observerRegisterEventClosure;
  75.         }
  76.        
  77.         USE tFilter
  78.         {
  79.             tFilter_registerEvent           as private  filterRegisterEvent;
  80.             tFilter_unregisterTrigger       as protected    filterUnregisterEvent;
  81.             tFilter_triggerExists           as protected    filterTriggerExists;
  82.             tFilter_applyFilter         as protected    filterApply;
  83.             tFilter_registerEventMethod     as public   filterRegisterEventMethod;
  84.             tFilter_registerEventStaticMethod   as public   filterRegisterEventStaticMethod;
  85.             tFilter_registerEventClosure        as public   filterRegisterEventClosure;
  86.         }
  87.        
  88.         USE tVirtualMethod
  89.         {
  90.             tVirtualMethod_remove           as protected    virtualMethodRemove;
  91.             tVirtualMethod_add          as public   virtualMethodAdd;
  92.             tVirtualMethod_call         as public   virtualMethodCall;
  93.         }
  94.        
  95.         USE tInstance
  96.         {
  97.             tInstance___unregister          as private  __instanceUnregister;
  98.             tInstance_get               as public   instanceGet;
  99.             tInstance_register          as public   instanceRegister;
  100.             tInstance_getName           as public   instanceGetName;
  101.             tInstance_exists            as public   instanceExists;
  102.             tInstance_getNames          as public   instanceGetNames;
  103.         }
  104.        
  105.         USE tStorage
  106.         {
  107.             tStorage___register         as private  __storageRegister;
  108.             tStorage___unregister           as private  __storageUnregister;
  109.             tStorage_get                as private  storageGet;
  110.             tStorage_getIterator            as public   getIterator;
  111.             tStorage_push               as public   storagePush;
  112.             tStorage_pop                as public   storagePop;
  113.             tStorage_peek               as public   storagePeek;
  114.         }
  115.        
  116.         public function __set($index, $value)
  117.         {
  118.             if(TRUE === $this->adapterTriggerExists(__METHOD__))
  119.                 return $this->adapterCallAlias(__METHOD__, $index, $value);
  120.            
  121.             $filtered   = $this->filterApply('args', __METHOD__, array('index' => $index, 'value' => $value));
  122.             $index      = $filtered['index'];
  123.             $value      = $filtered['value'];
  124.            
  125.             $this->storageGet()->getCollection()->offsetSet($index, $value);
  126.            
  127.             $this->observerNotify(__METHOD__, $index, $value);
  128.         }
  129.        
  130.         public function push($value)
  131.         {
  132.             if(TRUE === $this->adapterTriggerExists(__METHOD__))
  133.                 return $this->adapterCallAlias(__METHOD__, $value);
  134.            
  135.             $filtered   = $this->filterApply('args', __METHOD__, array('value' => $value));
  136.             $value      = $filtered['value'];
  137.            
  138.             $this->storagePush($value);
  139.            
  140.             $this->observerNotify(__METHOD__, $value);
  141.            
  142.             $value = $this->filterApply('return', __METHOD__, $value);
  143.            
  144.             return $this;
  145.         }
  146.        
  147.         public function __get($index)
  148.         {
  149.             if(TRUE === $this->adapterTriggerExists(__METHOD__))
  150.                 return $this->adapterCallAlias(__METHOD__, $index);
  151.                
  152.             $filtered   = $this->filterApply('args', __METHOD__, array('index' => $index));
  153.             $index      = $filtered['index'];
  154.             $value      = $this->storageGet()->getCollection()->offsetGet($index);
  155.            
  156.             $this->observerNotify(__METHOD__, $index, $value);
  157.            
  158.             $value = $this->filterApply('return', __METHOD__, $value);
  159.            
  160.             return $value;
  161.         }
  162.        
  163.         public function pop()
  164.         {
  165.             if(TRUE === $this->adapterTriggerExists(__METHOD__))
  166.                 return $this->adapterCallAlias(__METHOD__);
  167.                
  168.             $value = $this->storagePop();
  169.            
  170.             $this->observerNotify(__METHOD__, $value);
  171.            
  172.             $value = $this->filterApply('return', __METHOD__, $value);
  173.            
  174.             return $value;
  175.         }
  176.        
  177.         public function peek()
  178.         {
  179.             if(TRUE === $this->adapterTriggerExists(__METHOD__))
  180.                 return $this->adapterCallAlias(__METHOD__);
  181.                
  182.             $value = $this->storagePeek();
  183.            
  184.             $this->observerNotify(__METHOD__, $value);
  185.            
  186.             $value = $this->filterApply('return', __METHOD__, $value);
  187.            
  188.             return $value;
  189.         }
  190.        
  191.         public function clear()
  192.         {
  193.             if(TRUE === $this->adapterTriggerExists(__METHOD__))
  194.                 return $this->adapterCallAlias(__METHOD__);
  195.                
  196.             $this->storageClear();
  197.             $this->observerNotify(__METHOD__);
  198.            
  199.             return $this;
  200.         }
  201.        
  202.         public function __call($name, array $args = array())
  203.         {
  204.             if(TRUE === $this->adapterTriggerExists(__METHOD__))
  205.                 return $this->adapterCallAlias(__METHOD__, $args);
  206.            
  207.             $filtered   = $this->filterApply('args', __METHOD__, array('method' => $name, 'args' => $args));
  208.             $name       = $filtered['method'];
  209.             $args       = $filtered['args'];
  210.            
  211.             $value = $this->virtualMethodCall($name, $args);
  212.            
  213.             $this->observerNotify(__METHOD__, $name, $value, $args);
  214.            
  215.             $value = $this->filterApply('return', __METHOD__, $value);
  216.            
  217.             return $value;
  218.         }
  219.        
  220.         public function __destruct()
  221.         {
  222.             $this->__splUnregister();
  223.             $this->__instanceUnregister($this);
  224.             $this->__storageUnregister();
  225.         }
  226.          
  227.         public function __construct()
  228.         {
  229.             $this->__splRegister();
  230.            
  231.             switch(TRUE):
  232.                 case($this instanceOf iStack):
  233.                     $this->__storageRegister(new Stack(new Collection));
  234.                     break;
  235.                 case($this instanceOf iQueue):
  236.                     $this->__storageRegister(new Queue(new Collection));
  237.                     break;
  238.                 case($this instanceOf iArrayList):
  239.                 default:
  240.                     $this->__storageRegister(new ArrayList(new Collection));
  241.                     break;
  242.             endswitch;
  243.         }
  244.     }
  245.  
  246.  
  247.  
  248.  
  249.     $foo = new \ILLI\System\Object;
  250.    
  251.     /*
  252.     $foo->virtualMethodAdd('myFunkyGetter', function($val) use ($foo)
  253.     {
  254.         return $foo->getCollection()->offsetGet($val);
  255.     });
  256.     */
  257.    
  258.     $foo
  259.     ->virtualMethodAdd('mypush', function($val) use ($foo)
  260.     {
  261.         $val['value'] = 'mypush----'.$val['value'];
  262.         return $val;
  263.     })
  264.     ->virtualMethodAdd('mypop', function($val) use ($foo)
  265.     {
  266.         $val = 'pop-----'.$val;
  267.         return $val;
  268.     })
  269.     ->virtualMethodAdd('mypeek', function($val) use ($foo)
  270.     {
  271.         $val = 'peek----'.$val;
  272.         return $val;
  273.     })
  274.     ->filterRegisterEventMethod('args', get_class($foo).'::push', get_class($foo).'::mypush')
  275.     ->filterRegisterEventMethod('return', get_class($foo).'::pop', get_class($foo).'::mypop')
  276.     ->filterRegisterEventMethod('return', get_class($foo).'::peek', get_class($foo).'::mypeek')
  277.     ->push('the')
  278.     ->push('quick')
  279.     ->push('brown')
  280.     ->push('fox')
  281.     ->push('jumps')
  282.     ->push('over')
  283.     ->push('the')
  284.     ->push('lazy')
  285.     ->push('dog');
  286.  
  287.  
  288.     foreach($foo as $baz) var_dump($baz);
  289.     var_dump('======');
  290.     foreach(['pop', 'pop', 'peek'] as $call) var_dump($foo->$call());
  291.     var_dump('======');
  292.     var_dump($foo);
  293.  
  294. ?>
  295. string(13) "mypush----the"
  296. string(15) "mypush----quick"
  297. string(15) "mypush----brown"
  298. string(13) "mypush----fox"
  299. string(15) "mypush----jumps"
  300. string(14) "mypush----over"
  301. string(13) "mypush----the"
  302. string(14) "mypush----lazy"
  303. string(13) "mypush----dog"
  304. string(6) "======"
  305. string(21) "pop-----mypush----dog"
  306. string(22) "pop-----mypush----lazy"
  307. string(21) "peek----mypush----the"
  308. string(6) "======"
  309. object(ILLI\System\Object)#20 (12) {
  310.   ["__tSplHashRegister_Address":"ILLI\System\Object":private]=>
  311.   string(32) "00000000215f9259000000005d5519e9"
  312.   ["__tAdapter_tracesEnabled":"ILLI\System\Object":private]=>
  313.   bool(false)
  314.   ["__tAdapter_traces":"ILLI\System\Object":private]=>
  315.   array(0) {
  316.   }
  317.   ["__tAdapter_hook":protected]=>
  318.   array(0) {
  319.   }
  320.   ["__tObserver_tracesEnabled":"ILLI\System\Object":private]=>
  321.   bool(false)
  322.   ["__tObserver_traces":"ILLI\System\Object":private]=>
  323.   array(0) {
  324.   }
  325.   ["__tObserver_hook":protected]=>
  326.   array(0) {
  327.   }
  328.   ["__tFilter_tracesEnabled":"ILLI\System\Object":private]=>
  329.   bool(false)
  330.   ["__tFilter_traces":"ILLI\System\Object":private]=>
  331.   array(0) {
  332.   }
  333.   ["__tFilter_hook":protected]=>
  334.   array(3) {
  335.     ["ILLI\System\Object::push"]=>
  336.     array(1) {
  337.       [0]=>
  338.       object(ILLI\System\ConcreteFilter)#35 (3) {
  339.         ["__event":"ILLI\System\ConcreteFilter":private]=>
  340.         string(4) "args"
  341.         ["__Trigger":"ILLI\System\ConcreteFilter":private]=>
  342.         object(ILLI\System\Method)#37 (3) {
  343.           ["__class":protected]=>
  344.           string(18) "ILLI\System\Object"
  345.           ["__function":protected]=>
  346.           string(4) "push"
  347.           ["__method":protected]=>
  348.           string(24) "ILLI\System\Object::push"
  349.         }
  350.         ["__Callable":protected]=>
  351.         object(ILLI\System\InvokeMethod)#39 (2) {
  352.           ["__Instance":"ILLI\System\InvokeMethod":private]=>
  353.           *RECURSION*
  354.           ["__Callable":protected]=>
  355.           object(ILLI\System\Method)#40 (3) {
  356.             ["__class":protected]=>
  357.             string(18) "ILLI\System\Object"
  358.             ["__function":protected]=>
  359.             string(6) "mypush"
  360.             ["__method":protected]=>
  361.             string(26) "ILLI\System\Object::mypush"
  362.           }
  363.         }
  364.       }
  365.     }
  366.     ["ILLI\System\Object::pop"]=>
  367.     array(1) {
  368.       [0]=>
  369.       object(ILLI\System\ConcreteFilter)#41 (3) {
  370.         ["__event":"ILLI\System\ConcreteFilter":private]=>
  371.         string(6) "return"
  372.         ["__Trigger":"ILLI\System\ConcreteFilter":private]=>
  373.         object(ILLI\System\Method)#42 (3) {
  374.           ["__class":protected]=>
  375.           string(18) "ILLI\System\Object"
  376.           ["__function":protected]=>
  377.           string(3) "pop"
  378.           ["__method":protected]=>
  379.           string(23) "ILLI\System\Object::pop"
  380.         }
  381.         ["__Callable":protected]=>
  382.         object(ILLI\System\InvokeMethod)#43 (2) {
  383.           ["__Instance":"ILLI\System\InvokeMethod":private]=>
  384.           *RECURSION*
  385.           ["__Callable":protected]=>
  386.           object(ILLI\System\Method)#44 (3) {
  387.             ["__class":protected]=>
  388.             string(18) "ILLI\System\Object"
  389.             ["__function":protected]=>
  390.             string(5) "mypop"
  391.             ["__method":protected]=>
  392.             string(25) "ILLI\System\Object::mypop"
  393.           }
  394.         }
  395.       }
  396.     }
  397.     ["ILLI\System\Object::peek"]=>
  398.     array(1) {
  399.       [0]=>
  400.       object(ILLI\System\ConcreteFilter)#45 (3) {
  401.         ["__event":"ILLI\System\ConcreteFilter":private]=>
  402.         string(6) "return"
  403.         ["__Trigger":"ILLI\System\ConcreteFilter":private]=>
  404.         object(ILLI\System\Method)#46 (3) {
  405.           ["__class":protected]=>
  406.           string(18) "ILLI\System\Object"
  407.           ["__function":protected]=>
  408.           string(4) "peek"
  409.           ["__method":protected]=>
  410.           string(24) "ILLI\System\Object::peek"
  411.         }
  412.         ["__Callable":protected]=>
  413.         object(ILLI\System\InvokeMethod)#47 (2) {
  414.           ["__Instance":"ILLI\System\InvokeMethod":private]=>
  415.           *RECURSION*
  416.           ["__Callable":protected]=>
  417.           object(ILLI\System\Method)#48 (3) {
  418.             ["__class":protected]=>
  419.             string(18) "ILLI\System\Object"
  420.             ["__function":protected]=>
  421.             string(6) "mypeek"
  422.             ["__method":protected]=>
  423.             string(26) "ILLI\System\Object::mypeek"
  424.           }
  425.         }
  426.       }
  427.     }
  428.   }
  429.   ["__tVirtualMethod_hook":protected]=>
  430.   array(1) {
  431.     ["ILLI\System\Object"]=>
  432.     array(3) {
  433.       ["mypush"]=>
  434.       object(ILLI\System\InvokeClosure)#28 (1) {
  435.         ["__Callable":protected]=>
  436.         object(Closure)#25 (2) {
  437.           ["static"]=>
  438.           array(1) {
  439.             ["foo"]=>
  440.             *RECURSION*
  441.           }
  442.           ["parameter"]=>
  443.           array(1) {
  444.             ["$val"]=>
  445.             string(10) "<required>"
  446.           }
  447.         }
  448.       }
  449.       ["mypop"]=>
  450.       object(ILLI\System\InvokeClosure)#30 (1) {
  451.         ["__Callable":protected]=>
  452.         object(Closure)#29 (2) {
  453.           ["static"]=>
  454.           array(1) {
  455.             ["foo"]=>
  456.             *RECURSION*
  457.           }
  458.           ["parameter"]=>
  459.           array(1) {
  460.             ["$val"]=>
  461.             string(10) "<required>"
  462.           }
  463.         }
  464.       }
  465.       ["mypeek"]=>
  466.       object(ILLI\System\InvokeClosure)#32 (1) {
  467.         ["__Callable":protected]=>
  468.         object(Closure)#31 (2) {
  469.           ["static"]=>
  470.           array(1) {
  471.             ["foo"]=>
  472.             *RECURSION*
  473.           }
  474.           ["parameter"]=>
  475.           array(1) {
  476.             ["$val"]=>
  477.             string(10) "<required>"
  478.           }
  479.         }
  480.       }
  481.     }
  482.   }
  483.   ["__tStorage_Data":"ILLI\System\Object":private]=>
  484.   object(ILLI\System\ArrayList)#22 (1) {
  485.     ["__Collection":protected]=>
  486.     object(ILLI\System\Collection)#24 (6) {
  487.       ["__data":"ILLI\System\Collection":private]=>
  488.       array(7) {
  489.         [0]=>
  490.         string(13) "mypush----the"
  491.         [1]=>
  492.         string(15) "mypush----quick"
  493.         [2]=>
  494.         string(15) "mypush----brown"
  495.         [3]=>
  496.         string(13) "mypush----fox"
  497.         [4]=>
  498.         string(15) "mypush----jumps"
  499.         [5]=>
  500.         string(14) "mypush----over"
  501.         [6]=>
  502.         string(13) "mypush----the"
  503.       }
  504.       ["__offsets":"ILLI\System\Collection":private]=>
  505.       array(7) {
  506.         [0]=>
  507.         int(0)
  508.         [1]=>
  509.         int(1)
  510.         [2]=>
  511.         int(2)
  512.         [3]=>
  513.         int(3)
  514.         [4]=>
  515.         int(4)
  516.         [5]=>
  517.         int(5)
  518.         [6]=>
  519.         int(6)
  520.       }
  521.       ["__indexes":"ILLI\System\Collection":private]=>
  522.       array(7) {
  523.         [0]=>
  524.         int(0)
  525.         [1]=>
  526.         int(1)
  527.         [2]=>
  528.         int(2)
  529.         [3]=>
  530.         int(3)
  531.         [4]=>
  532.         int(4)
  533.         [5]=>
  534.         int(5)
  535.         [6]=>
  536.         int(6)
  537.       }
  538.       ["__iteratorIndex":"ILLI\System\Collection":private]=>
  539.       int(9)
  540.       ["__lastOffsetUpdate":"ILLI\System\Collection":private]=>
  541.       NULL
  542.       ["__lastOffsetInsert":"ILLI\System\Collection":private]=>
  543.       NULL
  544.     }
  545.   }
  546. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement