Skip to content

Commit

Permalink
Addresses getgrav#154 and finalize theme inheritance implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sommerregen committed Oct 12, 2015
1 parent 22054e2 commit 8595b79
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions system/src/Grav/Common/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function __construct(Grav $grav)
{
$this->grav = $grav;
$this->config = $grav['config'];

// Register instance as autoloader for theme inheritance
spl_autoload_register([$this, 'autoloadTheme']);
}

public function init()
Expand Down Expand Up @@ -236,4 +239,35 @@ protected function loadConfiguration($name, Config $config)
}
}
}

/**
* 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;
}
}

0 comments on commit 8595b79

Please sign in to comment.