From 8595b7972b3164ea064c2ab4daa336a3838675b2 Mon Sep 17 00:00:00 2001 From: Sommerregen Date: Mon, 12 Oct 2015 18:47:40 +0200 Subject: [PATCH] Addresses #154 and finalize theme inheritance implementation --- system/src/Grav/Common/Themes.php | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/system/src/Grav/Common/Themes.php b/system/src/Grav/Common/Themes.php index 3461180e5..4dd26120d 100644 --- a/system/src/Grav/Common/Themes.php +++ b/system/src/Grav/Common/Themes.php @@ -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; + } }