Advertisement
fruffl

AOP Bootstrap

Feb 3rd, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.14 KB | None | 0 0
  1. <?PHP
  2.     NAMESPACE ILLI\Core;
  3.     USE ILLI\Core\Exception;
  4.    
  5.     CLASS Object EXTENDS \ILLI\Core\A_Object
  6.     {
  7.         USE \ILLI\Core\Object\T_Boot;
  8.        
  9.         private static $__classPattern =
  10.         [
  11.             'class' => [],
  12.             '__run' => true,
  13.             '__exec' => NULL,
  14.         ];
  15.        
  16.         protected $__initConfig = [];
  17.        
  18.         final public function __construct(array $__options = [])
  19.         {
  20.             $__options = $this->extend(TRUE, self::$__classPattern, $__options);
  21.            
  22.             $__boot =
  23.             [
  24.                 '__Core_Object_T_Boot_propDef' =>
  25.                 [
  26.                     'lookup' => function(array $c, $n, array $l)
  27.                     {
  28.                         array_walk($l, function($v, $p) use ($n, &$c)
  29.                         {
  30.                             $c[$p] = $this->Core_Object_T_Boot_handleLookup($p, $n, $c) +
  31.                             [
  32.                                 'value' => $v
  33.                             ];
  34.                            
  35.                             $value = &$c[$p]['value'];
  36.                            
  37.                             if(isset($value)
  38.                             && is_array($value)
  39.                             && is_array($v)
  40.                             && in_array($p, ['class']))
  41.                                 $value = $this->extend(TRUE, $v, $value);
  42.                                
  43.                         });
  44.                        
  45.                         return $c;
  46.                     },
  47.                     'init' => function(array $c)
  48.                     {
  49.                         array_walk($c, function($s, $p)
  50.                         {
  51.                             extract($s);
  52.                             $this->__initConfig[$p] = $value;
  53.                         });
  54.                        
  55.                         return $c;
  56.                     }
  57.                 ],
  58.                 '__Core_Object_T_Boot_propAsp' =>
  59.                 [
  60.                     'lookup' => function(array $c, $n, array $l)
  61.                     {
  62.                         array_walk($l, function($v, $p) use ($n, &$c)
  63.                         {
  64.                             if(is_integer($p))
  65.                             {
  66.                                 $p = $v;
  67.                                 $v = TRUE;
  68.                             }
  69.                            
  70.                             $c[$p] = $this->Core_Object_T_Boot_handleLookup($p, $n, $c) +
  71.                             [
  72.                                 'asp' => $v
  73.                             ];
  74.                         });
  75.                        
  76.                         return $c;
  77.                     },
  78.                     'complete' => function(array $c)
  79.                     {
  80.                         array_walk($c, function(&$s, $p)
  81.                         {
  82.                             extract($s);
  83.                            
  84.                             if(FALSE === $exists
  85.                             || FALSE === $asp)
  86.                                 return;
  87.                            
  88.                             $m = ['set', 'unset', 'get', 'isset'];
  89.                            
  90.                             array_walk($m, function($a) use (&$s, $p)
  91.                             {
  92.                                 $s += [(($e = method_exists($this, $m = 'aspectable'.$a.$p)) ? 'asp' : 'dir').$a => ($e ? $m : $p)];
  93.                             });
  94.                         });
  95.                        
  96.                         return $c;
  97.                     }
  98.                 ],
  99.                 '__Core_Object_T_Boot_propCom' =>
  100.                 [
  101.                     'lookup' => function(array $c, $n, array $l)
  102.                     {
  103.                         array_walk($l, function($v, $p) use ($n, &$c)
  104.                         {
  105.                             if(is_integer($p))
  106.                             {
  107.                                 $p = $v;
  108.                                 $v = FALSE;
  109.                             }
  110.                            
  111.                             $c[$p] = $this->Core_Object_T_Boot_handleLookup($p, $n, $c) +
  112.                             [
  113.                                 'flag' => $v
  114.                             ];
  115.                         });
  116.                        
  117.                         return $c;
  118.                     }
  119.                 ]
  120.             ];
  121.            
  122.             $__exec = is_callable($__options['__exec']) ? $__options['__exec'] : function()
  123.             {
  124.                 $_ = self::$__Core_Object_T_Boot_Static[get_called_class()];
  125.                 $c = $_['__Core_Object_T_Boot_propCom'];
  126.                 $d = $_['__Core_Object_T_Boot_propDef'];
  127.                 $a = $_['__Core_Object_T_Boot_propAsp'];
  128.                 $i = $this->__initConfig;
  129.                
  130.                 $write = function($p, $v) use ($a)
  131.                 {
  132.                     if(!isset($a[$p]))
  133.                         return $v;
  134.                        
  135.                     extract($a[$p]);
  136.                    
  137.                     if(isset($dirset) || FALSE === $asp)
  138.                         return $this->$p = $v;
  139.                        
  140.                     if(isset($aspset))
  141.                         return $this->$aspset($v);
  142.                 };
  143.                
  144.                 array_walk($d, function($s, $p) use ($i, $a, $write)
  145.                 {
  146.                     extract($s);
  147.                    
  148.                     if(FALSE === $exists)
  149.                         return;
  150.                        
  151.                     $write($p, $value);
  152.                    
  153.                     if(isset($this->$p))
  154.                         return;
  155.                    
  156.                     $this->$p = $value;
  157.                 });
  158.                
  159.                 array_walk($c, function($s, $p) use ($i, $write)
  160.                 {
  161.                     if(FALSE === isset($i[$p]))
  162.                         return;
  163.                    
  164.                     $value = $i[$p];
  165.                        
  166.                     extract($s);
  167.                    
  168.                     switch(TRUE):
  169.                         case is_array($value) && is_array($this->$p):
  170.                             switch($flag):
  171.                                 case 'merge':
  172.                                     $value = array_merge($this->$p, $value);
  173.                                     break;
  174.                                 case 'extend':
  175.                                     $value = $this->extend($this->$p, $value);
  176.                                     break;
  177.                                 case 'recursive':
  178.                                     $value = $this->extend(TRUE, $this->$p, $value);
  179.                                     break;
  180.                             endswitch;
  181.                             break;
  182.                         case is_string($value) && is_string($this->$p):
  183.                             switch($flag):
  184.                                 case 'append':
  185.                                     $value = $this->$p.$value;
  186.                                     break;
  187.                                 case 'prepend':
  188.                                     $value = $value.$this->$p;
  189.                                     break;
  190.                             endswitch;
  191.                             break;
  192.                     endswitch;
  193.                    
  194.                     $write($p, $value);
  195.                 });
  196.                
  197.                 $this->__initConfig['__run'] = TRUE;
  198.                 unset($this->__initConfig['__exec']);
  199.             };
  200.            
  201.             unset($__options['__exec']);
  202.            
  203.             $this->Core_Object_T_Boot_emit($__boot)->__initConfig = $this->extend(TRUE, $this->__initConfig, $__options);
  204.            
  205.             TRUE === $this->__initConfig['__run']
  206.                 ? $__exec()
  207.                 : $this->__initConfig['__exec'] = $__exec;
  208.         }
  209.        
  210.         public function extend()
  211.         {
  212.             $a = func_get_args();
  213.             $t = $a[0];
  214.             $l = count($a);
  215.             $r = FALSE;
  216.             $i = 1;
  217.             $f = function($v, $p) use (&$t, $r)
  218.             {
  219.                 switch(TRUE):
  220.                     case is_array($t):
  221.                         $t[$p] = (TRUE === $r && is_array($v) && isset($t[$p]) && is_array($t[$p]))
  222.                             ? $this->extend($r, $t[$p], $v)
  223.                             : $v;
  224.                         break;
  225.                     case is_object($t):
  226.                         $t->$p = (TRUE === $r && is_array($v) && isset($t->$p) && is_array($t->$p))
  227.                             ? $this->extend($r, $t->$p, $v)
  228.                             : $v;
  229.                         break;
  230.                     case is_string($t):
  231.                         if(FALSE === class_exists($t))
  232.                             throw new Exception('Class '.$t.' not found.');
  233.                            
  234.                         if(FALSE === property_exists($t, $p))
  235.                             throw new Exception('Property '.$t.'::$'.$p.' not found.');
  236.                            
  237.                         $t::$$p = (TRUE === $r && is_array($v) && is_array($t::${$p}))
  238.                             ? $this->extend($r, $t::$$p, $v)
  239.                             : $v;
  240.                         break;
  241.                     default:
  242.                         throw new Exception('Target not writable.');
  243.                 endswitch;
  244.             };
  245.            
  246.             if(is_bool($t))
  247.             {
  248.                 $t = isset($a[1]) ? $a[1] : [];
  249.                 $r = $t;
  250.                 $i = 2;
  251.             }
  252.            
  253.             if(FALSE === is_array($t)
  254.             && FALSE === is_object($t)
  255.             && FALSE === is_string($t))
  256.                 $t = [];
  257.                
  258.             if($l === $i)
  259.             {
  260.                 $t = $this;
  261.                 --$i;
  262.             }
  263.            
  264.             for( ; $i < $l; $i++)
  265.             {
  266.                 if(NULL === ($o = $a[$i]))
  267.                     continue;
  268.                
  269.                 array_walk($o, $f);
  270.             }
  271.            
  272.             return $t;
  273.         }
  274.        
  275.         public function dump()
  276.         {
  277.             var_dump(self::$__Core_Object_T_Boot_Static);
  278.         }
  279.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement