<?PHP
NAMESPACE tri4m\Wp;
USE tri4m\Wp\Theme\Application;
USE tri4m\Wp\Theme\Hook;
USE tri4m\Wp\Theme\Trace;
USE tri4m\Wp\Theme\Wp;
USE ILLI\Core\Std\Def;
USE ILLI\Core\Std\Def\__const_Type;
USE ILLI\Core\Std\Exception;
USE ILLI\Core\Std\Invoke;
USE ILLI\Core\Std\Throwable;
USE ILLI\Core\Util\Inflector;
USE ILLI\Core\Util\String;
CLASS Theme
{
protected static $__running = FALSE;
protected static $__config = [];
protected static $__themePath = NULL;
protected static $__themeUri = NULL;
protected static $__wpIncludePath = NULL;
protected static $__wpIncludeUri = NULL;
protected static $__slug = NULL;
protected static $__name = NULL;
protected static $__fullName = NULL;
protected static $__version = NULL;
protected static $__versionID = NULL;
protected static $__debug = NULL;
protected static $__Hook = NULL;
protected static $__pathScheme =
[
// theme base path
'root' => '{:root}',
'core' => '{:root}/core',
'coreInc' => '{:root}/core/inc/{:class}/{:method}/{:file}.php',
// core lib contains 3rd party stuff
'coreLib' => '{:root}/core/lib',
'coreJs' => '{:root}/core/lib/js',
'coreCss' => '{:root}/core/lib/css',
'coreImg' => '{:root}/core/lib/img',
'coreIetf' => '{:root}/core/lib/ietf',
'corePie' => '{:root}/core/lib/pie',
'api' => '{:root}/core/api',
'apiScheme' => '{:root}/core/api/{:class}/{:method}/{:slug}/{:file}.xml',
// std contains fallbacks for user replacable files
'std' => '{:root}/core/std',
'stdLib' => '{:root}/core/std/lib',
'stdImg' => '{:root}/core/std/lib/img',
// wordpress includes
'wp' => '{:wp}',
'wpJs' => '{:wp}/js',
'wpCss' => '{:wp}/css'
];
public function __construct(array $__config)
{
Throwable::run();
if(TRUE === static::$__running)
{
if(TRUE === Theme::isDebug())
Trace::add(__METHOD__.': bypass');
return;
}
$__config +=
[
'debug' => FALSE,
'versionID' => 0,
'version' => '0',
];
static::$__running = TRUE;
static::$__config = &$__config;
static::$__Hook = new Hook;
try
{
$_ =
[
Def::v(__const_Type::SPL_BOOLEAN),
Def::v(__const_Type::SPL_STRING),
Def::v(__const_Type::SPL_DIRECTORY),
Def::v(__const_Type::SPL_INTEGER),
];
foreach(['slug', 'name', 'fullName', 'version', 'versionID', 'themePath', 'themeUri', 'wpIncludePath', 'wpIncludeUri', 'debug'] as $config)
{
if(!isset($__config[$config]))
throw new Exception(String::insert('Missing configuration {:config}.', compact('config')));
switch($config):
case 'themePath':
case 'wpIncludePath':
$__config[$config] = '/'.trim(rtrim($__config[$config], '/'), '/');
break;
case 'themeUri':
case 'wpIncludeUri':
$__config[$config] = trim(rtrim($__config[$config], '/'), '/');
break;
endswitch;
if(TRUE === Theme::isDebug())
Trace::add(__METHOD__.': '.$config.': '.$__config[$config]);
try
{
switch($config):
case 'debug':
$_[0]->set($__config[$config]);
break;
case 'slug':
case 'name':
case 'fullName':
case 'version':
case 'themeUri':
case 'wpIncludeUri':
$_[1]->set($__config[$config]);
break;
case 'themePath':
case 'wpIncludePath':
$_[2]->set($__config[$config]);
break;
case 'versionID':
$_[3]->set($__config[$config]);
break;
endswitch;
}
catch(Exception $E)
{
throw new Exception('Illegal type for {:config} detected.', compact('config'), $E);
}
static::${'__'.$config} = $__config[$config];
unset($__config[$config]);
}
unset($_);
}
catch(\Exception $E)
{
throw new Exception('Theme Config Error', $E);
}
/*
- current event: Application::boot()
- after_setup_theme:
- Application::run()
- dequeue Actions
- dequeue Filters
- dequeue Callables
- register shutdown showTrace()
*/
try
{
try
{
Application::boot();
}
catch(\Exception $E)
{
throw new Exception('Boot Exec Error', $E);
}
try
{
Wp::addAction(Hook::ACTION_AFTER_SETUP_THEME, function()
{
try
{
Application::run();
}
catch(\Exception $E)
{
Theme::e('Run Exec Error', $E);
}
try
{
Hook::dequeueFilters();
Hook::dequeueActions();
Hook::dequeueCallables();
}
catch(\Exception $E)
{
Theme::e('Hook Dequeue Error', $E);
}
try
{
Wp::addAction(Hook::ACTION_SHUTDOWN, function()
{
if(FALSE === static::$__debug)
return;
print static::showTrace();
});
}
catch(\Exception $E)
{
Theme::e('Registration Hook::ACTION_SHUTDOWN Error', $E);
}
});
}
catch(\Exception $E)
{
throw new Exception('Registration Hook::ACTION_AFTER_SETUP_THEME Error', $E);
}
}
catch(\Exception $E)
{
throw new Exception('Theme Application Error', $E);
}
Throwable::stop();
}
static function __callStatic($__name, $__args)
{
$r = explode('_', $n = Inflector::underscore($__name));
switch($prefix = $r[0]):
case 'path':
case 'uri':
array_shift($r);
return isset(static::$__pathScheme[$r = lcfirst(Inflector::camelize(implode('_', $r)))])
? String::insert(static::$__pathScheme[$r], [
'root' => static::${'__theme'.('uri' === $prefix ? 'Uri' : 'Path')},
'wp' => static::${'__wpInclude'.('uri' === $prefix ? 'Uri' : 'Path')}])
.(([] !== $__args) ? '/'.trim(rtrim(implode('/', $__args), '/'), '/') : '')
: NULL;
endswitch;
}
static function slug($__value = NULL)
{
return NULL === $__value
? static::$__slug
: static::$__slug.'_'.$__value;
}
static function name()
{
return static::$__name;
}
static function fullname()
{
return static::$__fullname;
}
static function version()
{
return static::$__version;
}
static function versionID()
{
return static::$__versionID;
}
static function isDebug()
{
return static::$__debug;
}
static function config($__key = NULL, $__value = NULL)
{
if(TRUE === is_scalar($__key)
&& NULL !== $key)
static::$__config[$__key] = $__value;
return static::$__config;
}
static function showTrace()
{
return '<pre class="log">'.(new Trace).'</pre>';
}
/**
* handle exceptions thrown in closures.
* create throwable instance and forward track to wp_die()
*/
static function e()
{
$T = Invoke::emitClass('ILLI\Core\Std\Exception', func_get_args());
$m = '<pre class="error">'.$T->toTrack()->asText().'</pre>';
$t = '<pre class="log">'.(new Trace).'</pre>';
Throwable::stop();
Wp::wpDie($m.$t);
}
}
Comments