Advertisement
fruffl

WP Theme Bootstrap

Mar 8th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.22 KB | None | 0 0
  1. <?PHP
  2.     NAMESPACE tri4m\Wp;
  3.     USE tri4m\Wp\Theme\Application;
  4.     USE tri4m\Wp\Theme\Hook;
  5.     USE tri4m\Wp\Theme\Trace;
  6.     USE tri4m\Wp\Theme\Wp;
  7.     USE ILLI\Core\Std\Def;
  8.     USE ILLI\Core\Std\Def\__const_Type;
  9.     USE ILLI\Core\Std\Exception;
  10.     USE ILLI\Core\Std\Invoke;
  11.     USE ILLI\Core\Std\Throwable;
  12.     USE ILLI\Core\Util\Inflector;
  13.     USE ILLI\Core\Util\String;
  14.    
  15.     CLASS Theme
  16.     {
  17.         protected static $__running     = FALSE;
  18.         protected static $__config      = [];
  19.         protected static $__themePath       = NULL;
  20.         protected static $__themeUri        = NULL;
  21.         protected static $__wpIncludePath   = NULL;
  22.         protected static $__wpIncludeUri    = NULL;
  23.         protected static $__slug        = NULL;
  24.         protected static $__name        = NULL;
  25.         protected static $__fullName        = NULL;
  26.         protected static $__version     = NULL;
  27.         protected static $__versionID       = NULL;
  28.         protected static $__debug       = NULL;
  29.         protected static $__Hook        = NULL;
  30.         protected static $__pathScheme      =
  31.         [
  32.             // theme base path
  33.             'root'      => '{:root}',
  34.             'core'      => '{:root}/core',
  35.             'coreInc'   => '{:root}/core/inc/{:class}/{:method}/{:file}.php',
  36.            
  37.             // core lib contains 3rd party stuff
  38.             'coreLib'   => '{:root}/core/lib',
  39.             'coreJs'    => '{:root}/core/lib/js',
  40.             'coreCss'   => '{:root}/core/lib/css',
  41.             'coreImg'   => '{:root}/core/lib/img',
  42.             'coreIetf'  => '{:root}/core/lib/ietf',
  43.             'corePie'   => '{:root}/core/lib/pie',
  44.             'api'       => '{:root}/core/api',
  45.             'apiScheme' => '{:root}/core/api/{:class}/{:method}/{:slug}/{:file}.xml',
  46.            
  47.             // std contains fallbacks for user replacable files
  48.             'std'       => '{:root}/core/std',
  49.             'stdLib'    => '{:root}/core/std/lib',
  50.             'stdImg'    => '{:root}/core/std/lib/img',
  51.            
  52.             // wordpress includes
  53.             'wp'        => '{:wp}',
  54.             'wpJs'      => '{:wp}/js',
  55.             'wpCss'     => '{:wp}/css'
  56.         ];
  57.        
  58.         public function __construct(array $__config)
  59.         {
  60.             Throwable::run();
  61.            
  62.             if(TRUE === static::$__running)
  63.             {
  64.                 if(TRUE === Theme::isDebug())
  65.                     Trace::add(__METHOD__.': bypass');
  66.                 return;
  67.             }
  68.            
  69.             $__config +=
  70.             [
  71.                 'debug'     => FALSE,
  72.                 'versionID' => 0,
  73.                 'version'   => '0',
  74.             ];
  75.            
  76.             static::$__running  = TRUE;
  77.             static::$__config   = &$__config;
  78.             static::$__Hook     = new Hook;
  79.            
  80.            
  81.             try
  82.             {
  83.                 $_ =
  84.                 [
  85.                     Def::v(__const_Type::SPL_BOOLEAN),
  86.                     Def::v(__const_Type::SPL_STRING),
  87.                     Def::v(__const_Type::SPL_DIRECTORY),
  88.                     Def::v(__const_Type::SPL_INTEGER),
  89.                 ];
  90.                
  91.                 foreach(['slug', 'name', 'fullName', 'version', 'versionID', 'themePath', 'themeUri', 'wpIncludePath', 'wpIncludeUri', 'debug'] as $config)
  92.                 {
  93.                     if(!isset($__config[$config]))
  94.                         throw new Exception(String::insert('Missing configuration {:config}.', compact('config')));
  95.                    
  96.                     switch($config):
  97.                         case 'themePath':
  98.                         case 'wpIncludePath':
  99.                             $__config[$config] = '/'.trim(rtrim($__config[$config], '/'), '/');
  100.                             break;
  101.                         case 'themeUri':
  102.                         case 'wpIncludeUri':
  103.                             $__config[$config] = trim(rtrim($__config[$config], '/'), '/');
  104.                             break;
  105.                     endswitch;
  106.                    
  107.                     if(TRUE === Theme::isDebug())
  108.                         Trace::add(__METHOD__.': '.$config.': '.$__config[$config]);
  109.                    
  110.                     try
  111.                     {
  112.                         switch($config):
  113.                             case 'debug':
  114.                                 $_[0]->set($__config[$config]);
  115.                                 break;
  116.                             case 'slug':
  117.                             case 'name':
  118.                             case 'fullName':
  119.                             case 'version':
  120.                             case 'themeUri':
  121.                             case 'wpIncludeUri':
  122.                                 $_[1]->set($__config[$config]);
  123.                                 break;
  124.                             case 'themePath':
  125.                             case 'wpIncludePath':
  126.                                 $_[2]->set($__config[$config]);
  127.                                 break;
  128.                             case 'versionID':
  129.                                 $_[3]->set($__config[$config]);
  130.                                 break;
  131.                         endswitch;
  132.                     }
  133.                     catch(Exception $E)
  134.                     {
  135.                         throw new Exception('Illegal type for {:config} detected.', compact('config'), $E);
  136.                     }
  137.                    
  138.                     static::${'__'.$config} = $__config[$config];
  139.                    
  140.                     unset($__config[$config]);
  141.                 }
  142.            
  143.                 unset($_);
  144.             }
  145.             catch(\Exception $E)
  146.             {
  147.                 throw new Exception('Theme Config Error', $E);
  148.             }
  149.            
  150.             /*
  151.                 - current event: Application::boot()
  152.                 - after_setup_theme:
  153.                     - Application::run()
  154.                     - dequeue Actions
  155.                     - dequeue Filters
  156.                     - dequeue Callables
  157.                     - register shutdown showTrace()
  158.             */
  159.             try
  160.             {
  161.                 try
  162.                 {
  163.                     Application::boot();
  164.                 }
  165.                 catch(\Exception $E)
  166.                 {
  167.                     throw new Exception('Boot Exec Error', $E);
  168.                 }
  169.            
  170.                 try
  171.                 {
  172.                     Wp::addAction(Hook::ACTION_AFTER_SETUP_THEME, function()
  173.                     {
  174.                         try
  175.                         {
  176.                             Application::run();
  177.                         }
  178.                         catch(\Exception $E)
  179.                         {
  180.                             Theme::e('Run Exec Error', $E);
  181.                         }
  182.                        
  183.                         try
  184.                         {
  185.                             Hook::dequeueFilters();
  186.                             Hook::dequeueActions();
  187.                             Hook::dequeueCallables();
  188.                         }
  189.                         catch(\Exception $E)
  190.                         {
  191.                             Theme::e('Hook Dequeue Error', $E);
  192.                         }
  193.                        
  194.                         try
  195.                         {
  196.                             Wp::addAction(Hook::ACTION_SHUTDOWN, function()
  197.                             {
  198.                                 if(FALSE === static::$__debug)
  199.                                     return;
  200.                                        
  201.                                 print static::showTrace();
  202.                             });
  203.                         }
  204.                         catch(\Exception $E)
  205.                         {
  206.                             Theme::e('Registration Hook::ACTION_SHUTDOWN Error', $E);
  207.                         }
  208.                        
  209.                     });
  210.                 }
  211.                 catch(\Exception $E)
  212.                 {
  213.                     throw new Exception('Registration Hook::ACTION_AFTER_SETUP_THEME Error', $E);
  214.                 }
  215.                
  216.             }
  217.             catch(\Exception $E)
  218.             {
  219.                 throw new Exception('Theme Application Error', $E);
  220.             }
  221.            
  222.             Throwable::stop();
  223.         }
  224.        
  225.         static function __callStatic($__name, $__args)
  226.         {
  227.             $r = explode('_', $n = Inflector::underscore($__name));
  228.            
  229.             switch($prefix = $r[0]):
  230.                 case 'path':
  231.                 case 'uri':
  232.                     array_shift($r);
  233.                    
  234.                     return isset(static::$__pathScheme[$r = lcfirst(Inflector::camelize(implode('_', $r)))])
  235.                         ? String::insert(static::$__pathScheme[$r], [
  236.                             'root'  => static::${'__theme'.('uri' === $prefix ? 'Uri' : 'Path')},
  237.                             'wp'    => static::${'__wpInclude'.('uri' === $prefix ? 'Uri' : 'Path')}])
  238.                             .(([] !== $__args) ? '/'.trim(rtrim(implode('/', $__args), '/'), '/') : '')
  239.                         : NULL;
  240.             endswitch;
  241.         }
  242.        
  243.         static function slug($__value = NULL)
  244.         {
  245.             return NULL === $__value
  246.                 ? static::$__slug
  247.                 : static::$__slug.'_'.$__value;
  248.         }
  249.        
  250.         static function name()
  251.         {
  252.             return static::$__name;
  253.         }
  254.        
  255.         static function fullname()
  256.         {
  257.             return static::$__fullname;
  258.         }
  259.        
  260.         static function version()
  261.         {
  262.             return static::$__version;
  263.         }
  264.        
  265.         static function versionID()
  266.         {
  267.             return static::$__versionID;
  268.         }
  269.        
  270.         static function isDebug()
  271.         {
  272.             return static::$__debug;
  273.         }
  274.        
  275.         static function config($__key = NULL, $__value = NULL)
  276.         {
  277.             if(TRUE === is_scalar($__key)
  278.             && NULL !== $key)
  279.                 static::$__config[$__key] = $__value;
  280.                
  281.             return static::$__config;
  282.         }
  283.        
  284.         static function showTrace()
  285.         {
  286.             return '<pre class="log">'.(new Trace).'</pre>';
  287.         }
  288.        
  289.         /**
  290.          * handle exceptions thrown in closures.
  291.          * create throwable instance and forward track to wp_die()
  292.          */
  293.         static function e()
  294.         {
  295.             $T = Invoke::emitClass('ILLI\Core\Std\Exception', func_get_args());
  296.            
  297.             $m = '<pre class="error">'.$T->toTrack()->asText().'</pre>';
  298.             $t = '<pre class="log">'.(new Trace).'</pre>';
  299.            
  300.             Throwable::stop();
  301.             Wp::wpDie($m.$t);
  302.         }
  303.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement