Addresses #154 and finalize theme inheritance implementation

This commit is contained in:
Sommerregen
2015-10-12 18:47:40 +02:00
parent 22054e232f
commit 8595b7972b

View File

@@ -27,6 +27,9 @@ class Themes extends Iterator
{
$this->grav = $grav;
$this->config = $grav['config'];
// Register instance as autoloader for theme inheritance
spl_autoload_register([$this, 'autoloadTheme']);
}
public function init()
@@ -236,4 +239,35 @@ class Themes extends Iterator
}
}
}
/**
* Autoload theme classes for inheritance
*
* @param string $class Class name
*
* @return mixed false FALSE if unable to load $class; Class name if
* $class is successfully loaded
*/
protected function autoloadTheme($class)
{
/** @var UniformResourceLocator $locator */
$locator = $this->grav['locator'];
$prefix = "Grav\\Theme";
if (false !== strpos($class, $prefix)) {
// Remove prefix from class
$class = substr($class, strlen($prefix));
// Replace namespace tokens to directory separators
$path = ltrim(preg_replace('#\\\|_(?!.+\\\)#', '/', $class), '/');
$file = $locator->findResource("themes://{$path}/{$path}.php");
// Load class
if (stream_resolve_include_path($file)) {
return include_once($file);
}
}
return false;
}
}