mirror of
https://github.com/getgrav/grav.git
synced 2026-07-20 03:42:15 +02:00
Addresses #154 and finalize theme inheritance implementation
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user