fruffl icon

AOP Bootstrap

fruffl | 02/03/13 10:28:45 PM UTC | 0 ⭐ | 272 👁️ | Never ⏰ | []
PHP |

6.14 KB

|

None

|

0 👍

/

0 👎

<?PHP
    NAMESPACE ILLI\Core;
    USE ILLI\Core\Exception;
    
    CLASS Object EXTENDS \ILLI\Core\A_Object
    {
        USE \ILLI\Core\Object\T_Boot;
        
        private static $__classPattern =
        [
            'class' => [],
            '__run' => true,
            '__exec' => NULL,
        ];
        
        protected $__initConfig = [];
        
        final public function __construct(array $__options = [])
        {
            $__options = $this->extend(TRUE, self::$__classPattern, $__options);
            
            $__boot = 
            [
                '__Core_Object_T_Boot_propDef' =>
                [
                    'lookup' => function(array $c, $n, array $l)
                    {
                        array_walk($l, function($v, $p) use ($n, &$c)
                        {
                            $c[$p] = $this->Core_Object_T_Boot_handleLookup($p, $n, $c) +
                            [
                                'value' => $v
                            ];
                            
                            $value = &$c[$p]['value'];
                            
                            if(isset($value)
                            && is_array($value)
                            && is_array($v)
                            && in_array($p, ['class']))
                                $value = $this->extend(TRUE, $v, $value);
                                
                        });
                        
                        return $c;
                    },
                    'init' => function(array $c)
                    {
                        array_walk($c, function($s, $p)
                        {
                            extract($s);
                            $this->__initConfig[$p] = $value;
                        });
                        
                        return $c;
                    }
                ],
                '__Core_Object_T_Boot_propAsp' =>
                [
                    'lookup' => function(array $c, $n, array $l)
                    {
                        array_walk($l, function($v, $p) use ($n, &$c)
                        {
                            if(is_integer($p))
                            {
                                $p = $v;
                                $v = TRUE;
                            }
                            
                            $c[$p] = $this->Core_Object_T_Boot_handleLookup($p, $n, $c) +
                            [
                                'asp' => $v
                            ];
                        });
                        
                        return $c;
                    },
                    'complete' => function(array $c)
                    {
                        array_walk($c, function(&$s, $p)
                        {
                            extract($s);
                            
                            if(FALSE === $exists
                            || FALSE === $asp)
                                return;
                            
                            $m = ['set', 'unset', 'get', 'isset'];
                            
                            array_walk($m, function($a) use (&$s, $p)
                            {
                                $s += [(($e = method_exists($this, $m = 'aspectable'.$a.$p)) ? 'asp' : 'dir').$a => ($e ? $m : $p)];
                            });
                        });
                        
                        return $c;
                    }
                ],
                '__Core_Object_T_Boot_propCom' =>
                [
                    'lookup' => function(array $c, $n, array $l)
                    {
                        array_walk($l, function($v, $p) use ($n, &$c)
                        {
                            if(is_integer($p))
                            {
                                $p = $v;
                                $v = FALSE;
                            }
                            
                            $c[$p] = $this->Core_Object_T_Boot_handleLookup($p, $n, $c) +
                            [
                                'flag' => $v
                            ];
                        });
                        
                        return $c;
                    }
                ]
            ];
            
            $__exec = is_callable($__options['__exec']) ? $__options['__exec'] : function()
            {
                $_ = self::$__Core_Object_T_Boot_Static[get_called_class()];
                $c = $_['__Core_Object_T_Boot_propCom'];
                $d = $_['__Core_Object_T_Boot_propDef'];
                $a = $_['__Core_Object_T_Boot_propAsp'];
                $i = $this->__initConfig;
                
                $write = function($p, $v) use ($a)
                {
                    if(!isset($a[$p]))
                        return $v;
                        
                    extract($a[$p]);
                    
                    if(isset($dirset) || FALSE === $asp)
                        return $this->$p = $v;
                        
                    if(isset($aspset))
                        return $this->$aspset($v);
                };
                
                array_walk($d, function($s, $p) use ($i, $a, $write)
                {
                    extract($s);
                    
                    if(FALSE === $exists)
                        return;
                        
                    $write($p, $value);
                    
                    if(isset($this->$p))
                        return;
                    
                    $this->$p = $value;
                });
                
                array_walk($c, function($s, $p) use ($i, $write)
                {
                    if(FALSE === isset($i[$p]))
                        return;
                    
                    $value = $i[$p];
                        
                    extract($s);
                    
                    switch(TRUE):
                        case is_array($value) && is_array($this->$p):
                            switch($flag):
                                case 'merge':
                                    $value = array_merge($this->$p, $value);
                                    break;
                                case 'extend':
                                    $value = $this->extend($this->$p, $value);
                                    break;
                                case 'recursive':
                                    $value = $this->extend(TRUE, $this->$p, $value);
                                    break;
                            endswitch;
                            break;
                        case is_string($value) && is_string($this->$p):
                            switch($flag):
                                case 'append':
                                    $value = $this->$p.$value;
                                    break;
                                case 'prepend':
                                    $value = $value.$this->$p;
                                    break;
                            endswitch;
                            break;
                    endswitch;
                    
                    $write($p, $value);
                });
                
                $this->__initConfig['__run'] = TRUE;
                unset($this->__initConfig['__exec']);
            };
            
            unset($__options['__exec']);
            
            $this->Core_Object_T_Boot_emit($__boot)->__initConfig = $this->extend(TRUE, $this->__initConfig, $__options);
            
            TRUE === $this->__initConfig['__run']
                ? $__exec()
                : $this->__initConfig['__exec'] = $__exec;
        }
        
        public function extend()
        {
            $a = func_get_args();
            $t = $a[0];
            $l = count($a);
            $r = FALSE;
            $i = 1;
            $f = function($v, $p) use (&$t, $r)
            {
                switch(TRUE):
                    case is_array($t):
                        $t[$p] = (TRUE === $r && is_array($v) && isset($t[$p]) && is_array($t[$p]))
                            ? $this->extend($r, $t[$p], $v)
                            : $v;
                        break;
                    case is_object($t):
                        $t->$p = (TRUE === $r && is_array($v) && isset($t->$p) && is_array($t->$p))
                            ? $this->extend($r, $t->$p, $v)
                            : $v;
                        break;
                    case is_string($t):
                        if(FALSE === class_exists($t))
                            throw new Exception('Class '.$t.' not found.');
                            
                        if(FALSE === property_exists($t, $p))
                            throw new Exception('Property '.$t.'::$'.$p.' not found.');
                            
                        $t::$$p = (TRUE === $r && is_array($v) && is_array($t::${$p}))
                            ? $this->extend($r, $t::$$p, $v)
                            : $v;
                        break;
                    default:
                        throw new Exception('Target not writable.');
                endswitch;
            };
            
            if(is_bool($t))
            {
                $t = isset($a[1]) ? $a[1] : [];
                $r = $t;
                $i = 2;
            }
            
            if(FALSE === is_array($t)
            && FALSE === is_object($t)
            && FALSE === is_string($t))
                $t = [];
                
            if($l === $i)
            {
                $t = $this;
                --$i;
            }
            
            for( ; $i < $l; $i++)
            {
                if(NULL === ($o = $a[$i]))
                    continue;
                
                array_walk($o, $f);
            }
            
            return $t;
        }
        
        public function dump()
        {
            var_dump(self::$__Core_Object_T_Boot_Static);
        }
    }

Comments