Advertisement
fruffl

AOP/Injectable Object

Jan 16th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.92 KB | None | 0 0
  1. <?PHP
  2.     NAMESPACE ILLI\Core;
  3.     USE ILLI\Core\StaticObject\iAdapter;
  4.     USE ILLI\Core\StaticObject\iClass;
  5.     USE ILLI\Core\StaticObject\iFilter;
  6.     USE ILLI\Core\StaticObject\iMethod;
  7.     USE ILLI\Core\StaticObject\iMethodFacade;
  8.     USE ILLI\Core\StaticObject\iMethodProto;
  9.     USE ILLI\Core\StaticObject\iObserver;
  10.     USE ILLI\Core\StaticObject\iSignal;
  11.     USE ILLI\Core\StaticObject\iTo;
  12.     USE ILLI\Core\StaticObject\tAdapter;
  13.     USE ILLI\Core\StaticObject\tFilter;
  14.     USE ILLI\Core\StaticObject\tInvoke;
  15.     USE ILLI\Core\StaticObject\tObserver;
  16.     USE ILLI\Core\StaticObject\tSignal;
  17.     USE ILLI\Core\StaticObject\tClass;
  18.     USE ILLI\Core\StaticObject\tTo;
  19.     USE ILLI\Core\StaticObject\tMethod;
  20.     USE ILLI\Core\StaticObject\tMethodFacade;
  21.     USE ILLI\Core\StaticObject\tMethodProto;
  22.     USE ILLI\Core\Filter;
  23.    
  24.     INTERFACE iStaticObject EXTENDS iAdapter, iClass, iFilter, iMethod, iMethodFacade, iMethodProto, iObserver, iSignal, iTo
  25.     {
  26.         public static function __load();
  27.        
  28.         public static function parents($__className = NULL);
  29.         public static function traits($__className = NULL);
  30.        
  31.         public static function construct(array $__options = []);
  32.         public static function __callstatic($__name, array $__parameters = []);
  33.        
  34.         public static function env(array $__options = []);
  35.         public static function extend();
  36.         public static function to($__format, array $__options = []);
  37.     }
  38.    
  39.     CLASS StaticObject IMPLEMENTS iStaticObject
  40.     {
  41.         USE tMethodFacade
  42.         {
  43.             Core_StaticObject_tMethodFacade_register    AS public registerMethodFacade;
  44.             Core_StaticObject_tMethodFacade_unregister  AS public unregisterMethodFacade;
  45.             Core_StaticObject_tMethodFacade_exists      AS public isMethodFacade;
  46.         }
  47.        
  48.         USE tAdapter
  49.         {
  50.             Core_StaticObject_tAdapter_register     AS public registerAdapter;
  51.             Core_StaticObject_tAdapter_unregister       AS public unregisterAdapter;
  52.             Core_StaticObject_tAdapter_exists       AS public isAdapter;
  53.         }
  54.        
  55.         USE tClass
  56.         {
  57.             Core_StaticObject_tClass_register       AS public registerClass;
  58.             Core_StaticObject_tClass_unregister     AS public unregisterClass;
  59.             Core_StaticObject_tClass_exists         AS public isClass;
  60.         }
  61.        
  62.         USE tFilter
  63.         {
  64.             Core_StaticObject_tFilter_register      AS public registerFilter;
  65.             Core_StaticObject_tFilter_unregister        AS public unregisterFilter;
  66.             Core_StaticObject_tFilter_exists        AS public isFilter;
  67.         }
  68.        
  69.         USE tMethod
  70.         {
  71.             Core_StaticObject_tMethod_register      AS public registerMethod;
  72.             Core_StaticObject_tMethod_unregister        AS public unregisterMethod;
  73.             Core_StaticObject_tMethod_exists        AS public isMethod;
  74.         }
  75.        
  76.         USE tObserver
  77.         {
  78.             Core_StaticObject_tObserver_register        AS public registerObserver;
  79.             Core_StaticObject_tObserver_unregister      AS public unregisterObserver;
  80.             Core_StaticObject_tObserver_exists      AS public isObserver;
  81.         }
  82.        
  83.         USE tSignal
  84.         {
  85.             Core_StaticObject_tSignal_register      AS public registerSignal;
  86.             Core_StaticObject_tSignal_unregister        AS public unregisterSignal;
  87.             Core_StaticObject_tSignal_exists        AS public isSignal;
  88.         }
  89.        
  90.         USE tTo
  91.         {
  92.             Core_StaticObject_tTo_register          AS public registerTo;
  93.             Core_StaticObject_tTo_unregister        AS public unregisterTo;
  94.             Core_StaticObject_tTo_exists            AS public isTo;
  95.         }
  96.        
  97.         USE tMethodProto
  98.         {
  99.             Core_StaticObject_tMethodProto_register     AS public registerMethodProto;
  100.             Core_StaticObject_tMethodProto_unregister   AS public unregisterMethodProto;
  101.             Core_StaticObject_tMethodProto_exists       AS public isMethodProto;
  102.         }
  103.        
  104.         USE tInvoke;
  105.        
  106.         protected static $__defaultConfig   = [];
  107.         protected static $__autoConfig      = [];
  108.         protected static $__defaultOptions  = [];
  109.        
  110.         protected static $__env         = [];
  111.         protected static $__initConfig      = [];
  112.        
  113.         protected static $__parentClasses   = [];
  114.         protected static $__usedTraits      = [];
  115.        
  116.         private static $__baseOptions       =
  117.         [
  118.             '__run'     => TRUE,
  119.             '__main'    => '__main',
  120.             '__delegate'    => NULL,
  121.             '__adapter' => [],
  122.             '__filter'  => [],
  123.             '__proto'   => [],
  124.             '__method'  => [],
  125.             '__observer'    => [],
  126.             '__signal'  => [],
  127.             '__class'   => [],
  128.             '__to'      =>
  129.             [
  130.                 'string'    => 'toString',
  131.                 'array'     => 'toArray'
  132.             ],
  133.             '__protoprefix' => 'prototype'
  134.         ];
  135.        
  136.         protected static function __main()
  137.         {
  138.             var_dump(__METHOD__);
  139.         }
  140.        
  141.         public static function __load()
  142.         {
  143.             $class = get_called_class();
  144.             self::$__env[$class]        = [];
  145.             self::$__initConfig[$class] = [];
  146.         }
  147.        
  148.         private function __construct()
  149.         {
  150.         }
  151.        
  152.         final public static function construct(array $__options = [])
  153.         {
  154.             $class = get_called_class();
  155.            
  156.             var_dump(__METHOD__);
  157.            
  158.             self::$__initConfig[$class] = static::extend(TRUE, self::$__baseOptions, static::$__defaultOptions, $__options);
  159.            
  160.             if(TRUE === self::$__initConfig[$class]['__run'])
  161.                 static::__run();
  162.         }
  163.        
  164.         public static function env(array $__env = [])
  165.         {
  166.             $class = get_called_class();
  167.            
  168.             foreach(self::$__env[$class] as $passable => $val)
  169.             {
  170.                 if(FALSE === isset($__env[$passable]))
  171.                     continue;
  172.                    
  173.                 self::$__env[$class][$passable] = is_array(self::$__env[$class][$passable]) && is_array($__env[$passable])
  174.                     ? static::extend(TRUE, self::$__env[$class][$passable], $__env[$passable])
  175.                     : $__env[$passable];
  176.                
  177.                 $key = "{$passable}";
  178.                 if(TRUE === isset(static::${$key}))
  179.                     static::${$key} = &self::$__env[$class][$passable];
  180.             }
  181.         }
  182.        
  183.         protected static function __run()
  184.         {
  185.             $class = get_called_class();
  186.            
  187.             static::__configure();
  188.             var_dump(__METHOD__);
  189.                         static::Core_StaticObject_tMethod_install       (self::$__initConfig[$class]['__method']);
  190.                         static::Core_StaticObject_tMethodProto_install      (self::$__initConfig[$class]['__proto']);
  191.                         static::Core_StaticObject_tAdapter_install      (self::$__initConfig[$class]['__delegate']);
  192.                         static::Core_StaticObject_tAdapter_install      (self::$__initConfig[$class]['__adapter']);
  193.                         static::Core_StaticObject_tObserver_install     (self::$__initConfig[$class]['__observer']);
  194.                         static::Core_StaticObject_tSignal_install       (self::$__initConfig[$class]['__signal']);
  195.                         static::Core_StaticObject_tFilter_install       (self::$__initConfig[$class]['__filter']);
  196.                         static::Core_StaticObject_tTo_install           (self::$__initConfig[$class]['__to']);
  197.                         static::Core_StaticObject_tClass_install        (self::$__initConfig[$class]['__class']);
  198.             static::prototype('construct', self::$__env[$class]);
  199.         }
  200.        
  201.         protected static function __configure()
  202.         {
  203.             var_dump(__METHOD__);
  204.             $class = get_called_class();
  205.            
  206.             $defaultConfig      =& static::$__defaultConfig;
  207.             $autoConfig     =& static::$__autoConfig;
  208.             $initConfig     =& self::$__initConfig[$class];
  209.             $env            =& self::$__env[$class];
  210.            
  211.             foreach($autoConfig as $key => $flag)
  212.             {
  213.                 // option not given. use default value
  214.                 if(!isset($initConfig[$key])
  215.                 && isset($defaultConfig[$key]))
  216.                 {
  217.                     $env[$key] = $defaultConfig[$key];
  218.                     continue;
  219.                 }
  220.                
  221.                 if(!isset($initConfig[$flag])
  222.                 && isset($defaultConfig[$flag]))
  223.                 {
  224.                     $env[$flag] = $defaultConfig[$flag];
  225.                     continue;
  226.                 }
  227.                
  228.                 if($flag === 'merge') // initConfig + defaultConfig
  229.                 {
  230.                     if(isset($defaultConfig[$key]))
  231.                     {
  232.                         if(is_array($initConfig[$key])
  233.                         && is_array($defaultConfig[$key]))
  234.                         {
  235.                             $env[$key] = static::extend
  236.                             (
  237.                                 TRUE,
  238.                                 $initConfig[$key],
  239.                                 $defaultConfig[$key]
  240.                             );
  241.                         }
  242.                         else
  243.                         {
  244.                             $env[$key] =
  245.                                 $initConfig[$key]
  246.                                 + $defaultConfig[$key];
  247.                         }
  248.                        
  249.                     }
  250.                     else
  251.                     {
  252.                         $env[$key] = $initConfig[$key];
  253.                     }
  254.                 }
  255.                 else // initConfig[${key}]
  256.                 if(isset($initConfig[$key]))
  257.                 {
  258.                     $env[$key] = $initConfig[$key];
  259.                 }
  260.                 else // initConfig[${flag}]
  261.                 if(isset($initConfig[$flag]))
  262.                 {
  263.                     $env[$flag] = $initConfig[$flag];
  264.                 }
  265.                 else // defaultConfig[${key}]
  266.                 if(isset($defaultConfig[$key]))
  267.                 {
  268.                     $env[$key] = $defaultConfig[$key];
  269.                 }
  270.                 else // default[${flag}]
  271.                 if(isset($defaultConfig[$flag]))
  272.                 {
  273.                     $env[$flag] = $defaultConfig[$flag];
  274.                 }
  275.                 else
  276.                 {
  277.                     $env[$flag] = NULL;
  278.                 }
  279.             }
  280.            
  281.             // accessible as property
  282.             foreach($env as $passable => &$value)
  283.             {
  284.                 $key = "{$passable}";
  285.                
  286.                 if(FALSE === isset(static::${$key}))
  287.                     continue;
  288.                    
  289.                 static::${$key} = &$value;
  290.                
  291.                 if(isset($initConfig[$passable]))
  292.                     unset($initConfig[$passable]);
  293.             }
  294.         }
  295.        
  296.         protected static function prototype($__function, array $__parameters = [])
  297.         {
  298.             var_dump(__METHOD__);
  299.             $__class = get_called_class();
  300.            
  301.             if(isset($__parameters['this']))
  302.                 unset($__parameters['this']);
  303.            
  304.             if(TRUE === static::Core_StaticObject_tAdapter_exists($__function))
  305.                 return static::Core_StaticObject_tAdapter_emit($__function, $__parameters);
  306.            
  307.             $__parameters = static::Core_StaticObject_tFilter_emit($__function, 'PARAMS', $__parameters, ['return' => $__parameters]);
  308.            
  309.             static::Core_StaticObject_tSignal_emit($__function, 'PRE_EVENT', [$__parameters], ['return' => $__parameters]);
  310.            
  311.             $__result = static::Core_StaticObject_tMethodFacade_emit($__function, $__parameters, function(array $__params) use ($__class, $__function)
  312.             {
  313.                 $result = NULL;
  314.                
  315.                 if(TRUE === static::Core_StaticObject_tMethodProto_exists($__function))
  316.                 {
  317.                     $result = static::Core_StaticObject_tMethodProto_emit($__function, $params);
  318.                 }
  319.                 else
  320.                 {
  321.                     $proto = $__function === 'construct'
  322.                         ? self::$__initConfig[$__class]['__main']
  323.                         : self::$__initConfig[$__class]['__protoprefix'].$__function;
  324.                
  325.                     if($proto === $__function)
  326.                     {
  327.                     }
  328.                     else
  329.                     if(method_exists($__class, $proto))
  330.                     {
  331.                         $result = static::Core_StaticObject_tInvoke_method($proto, $__params);
  332.                     }
  333.                 }
  334.                
  335.                 return $result;
  336.             });
  337.            
  338.             $__result = static::Core_StaticObject_tFilter_emit($__function, 'RETURN', $__result, ['return' => $__result]);
  339.            
  340.             static::Core_StaticObject_tObserver_emit($__function, [$__result, $__parameters]);
  341.             static::Core_StaticObject_tSignal_emit($__function, 'POST_EVENT',  [$__result, $__parameters], ['return' => $__result]);
  342.            
  343.             return $__result;
  344.         }
  345.        
  346.         public static function parents($__className = NULL)
  347.         {
  348.             $parentClasses = &self::$__parentClasses;
  349.            
  350.             $class = NULL === $__className ? get_called_class() : $__className;
  351.            
  352.             if(isset($parentClasses[$class]))
  353.                 return $parentClasses[$class];
  354.            
  355.             $classes = [$class];
  356.                 while($class = get_parent_class($class))
  357.                     $classes[] = $class;
  358.                
  359.                 $classes = array_reverse($classes);
  360.                 $parentClasses[$class] = $classes;
  361.                
  362.                 return $classes;
  363.         }
  364.        
  365.         public static function traits($__className = NULL)
  366.         {
  367.             $usedTraits = &self::$__usedTraits;
  368.            
  369.             $class = NULL === $__className ? get_called_class() : $__className;
  370.             $classes = self::parents($class);
  371.            
  372.             if(isset($usedTraits[$class]))
  373.                 return $usedTraits[$class];
  374.            
  375.             $traits = [];
  376.             foreach($classes as $class)
  377.                 foreach((new \ReflectionClass($class))->getTraits() as $trait)
  378.                     $traits[] = $trait->name;
  379.            
  380.                 $usedTraits[$class] = $traits;
  381.                
  382.                 return $traits;
  383.         }
  384.        
  385.         public static function extend()
  386.         {
  387.             $class      = get_called_class();
  388.             $args       = func_get_args();
  389.             $tar        = $args[0];
  390.             $i      = 1;
  391.             $length     = count($args);
  392.             $rec        = false;
  393.            
  394.             if($tar === true || $tar === false)
  395.             {
  396.                 $rec = $tar;
  397.                 $tar = isset($args[1]) ? $args[1] : [];
  398.                 $i = 2;
  399.             }
  400.            
  401.             if(!is_array($tar) && !is_object($tar))
  402.                 $tar = [];
  403.                
  404.             if($length === $i)
  405.             {
  406.                 //if(FALSE === ($this instanceof \Iterator))
  407.                 //  throw new \Exception('Unable to iterate throw '.get_called_class());
  408.                
  409.                 $tar = $class;
  410.                 --$i;
  411.             }
  412.            
  413.             for( ; $i < $length; $i++)
  414.             {
  415.                 if(NULL === ($opt = $args[$i]))
  416.                     continue;
  417.                
  418.                 foreach($opt as $key => $value)
  419.                 {
  420.                     if(is_array($tar))
  421.                     {
  422.                         if(TRUE === $rec && is_array($value) && isset($tar[$key]) && is_array($tar[$key]))
  423.                         {
  424.                             $tar[$key] = static::extend($rec, $tar[$key], $value);
  425.                             continue;
  426.                         }
  427.                        
  428.                         if(is_object($value))
  429.                         {
  430.                             //$tar[$key] = TRUE === $rec ? clone $value : $value;
  431.                             $tar[$key] = $value;
  432.                             continue;
  433.                         }
  434.                        
  435.                         $tar[$key] = $opt[$key];
  436.                         continue;
  437.                     }
  438.                    
  439.                     if(is_object($tar))
  440.                     {
  441.                         if(TRUE === $rec && is_array($value) && isset($tar->$key) && is_array($tar->$key))
  442.                         {
  443.                             $tar->$key = static::extend($rec, $tar->$key, $value);
  444.                             continue;
  445.                         }
  446.                        
  447.                         if(is_object($value))
  448.                         {
  449.                             //$tar[$key] = TRUE === $rec ? clone $value : $value;
  450.                             $tar->$key = $value;
  451.                             continue;
  452.                         }
  453.                        
  454.                         $tar->$key = $opt[$key];
  455.                         continue;
  456.                     }
  457.                    
  458.                     if(is_string($tar))
  459.                     {
  460.                         if(isset(self::$__env[$class][$key]))
  461.                         {
  462.                             static::env([$key => $value]);
  463.                             continue;
  464.                         }
  465.                        
  466.                         if(TRUE === $rec && is_array($value) && isset(static::${$key}) && is_array(static::${$key}))
  467.                         {
  468.                             static::${$key} = static::extend($rec, static::${$key}, $value);
  469.                             continue;
  470.                         }
  471.                        
  472.                         if(is_object($value))
  473.                         {
  474.                             //static::${$key} = TRUE === $rec ? clone $value : $value;
  475.                             static::${$key} = $value;
  476.                             continue;
  477.                         }
  478.                        
  479.                         static::${$key} = $opt[$key];
  480.                     }
  481.                 }
  482.             }
  483.            
  484.             return $tar;
  485.         }
  486.        
  487.         public static function __callstatic($__name, array $__parameters = [])
  488.         {
  489.             if(FALSE === static::Core_StaticObject_tMethod_exists($__name))
  490.                 throw new \Exception(get_called_class().'::'.$__name.'() is not a function.');
  491.                
  492.             return static::Core_StaticObject_tMethod_emit($__name, $__parameters);
  493.         }
  494.        
  495.         public static function to($__format, array $__options = [])
  496.         {
  497.             $__options +=
  498.             [
  499.                 'handlers'  => []
  500.             ];
  501.            
  502.             return static::Core_StaticObject_tTo_emit($__format, [$__options]);
  503.         }
  504.        
  505.         protected static function toString(array $__options = [])
  506.         {
  507.             return get_called_class();
  508.         }
  509.        
  510.         protected static function toArray(array $__options = [])
  511.         {
  512.             $__options +=
  513.             [
  514.                 'filter'    => function($__propertyName, $__value)
  515.                 {
  516.                     return !(substr($__propertyName, 0, 2) === '__');
  517.                 }
  518.             ];
  519.            
  520.             $props = [];
  521.             $lookup = $class = get_called_class();
  522.            
  523.             do
  524.             {
  525.                 foreach((new \ReflectionClass($lookup))->getStaticProperties() as $property => $value)
  526.                 {
  527.                     // normalize $space[ClassName] => $data to $space => $data
  528.                     if(isset(self::${$property})
  529.                     && is_array(self::${$property})
  530.                     && in_array($property, ['__initConfig', '__env'])
  531.                     && isset(self::${$property}[$class]))
  532.                         $value = self::${$property}[$class];
  533.                    
  534.                     $props[$property] = (is_array($value) && TRUE === isset($props[$property]))
  535.                         ? array_merge($value, $props[$property])
  536.                         : $value;
  537.                 }
  538.             }
  539.             while($lookup = get_parent_class($lookup));
  540.            
  541.             $result = [];
  542.            
  543.             foreach($props as $property => $value)
  544.             {
  545.                 if(is_callable($__options['filter'])
  546.                 && FALSE === $__options['filter']($property, $value))
  547.                     continue;
  548.                
  549.                 switch(true):
  550.                     case is_array($value):
  551.                     case (!is_object($property)):
  552.                         $result[$property] = $value;
  553.                         break;
  554.                     case (isset($__options['handlers'][$class = get_class($value)])):
  555.                         $result[$property] = $__options['handlers'][$class]($value);
  556.                         break;
  557.                     case (method_exists($value, 'to')):
  558.                         $result[$property] = $value->to('array', $__options);
  559.                         break;
  560.                     case ($vars = get_object_vars($value)):
  561.                         $result[$property] = $vars;
  562.                         break;
  563.                     case (method_exists($value, '__toString')):
  564.                         $result[$property] = (string) $value;
  565.                         break;
  566.                     default:
  567.                         $result[$property] = $value;
  568.                         break;
  569.                 endswitch;
  570.             }
  571.            
  572.             return [$class => $result];
  573.         }
  574.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement