From fae64f5b7a89b3bd0aa14dd768c9d441a8eb5a40 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Wed, 20 Aug 2014 20:51:49 +0300 Subject: [PATCH] Add basic theme configuration support --- system/src/Grav/Common/Grav.php | 1 - system/src/Grav/Common/Plugins.php | 1 + system/src/Grav/Common/Theme.php | 55 ++++++++++++++++++++++++++++++ system/src/Grav/Common/Themes.php | 4 +-- 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/system/src/Grav/Common/Grav.php b/system/src/Grav/Common/Grav.php index 16854a486..fc709de51 100644 --- a/system/src/Grav/Common/Grav.php +++ b/system/src/Grav/Common/Grav.php @@ -5,7 +5,6 @@ use Grav\Common\Service\StreamsServiceProvider; use Grav\Component\DI\Container; use Grav\Component\EventDispatcher\Event; use Grav\Component\EventDispatcher\EventDispatcher; -use Grav\Component\Filesystem\ResourceLocator; /** * Grav diff --git a/system/src/Grav/Common/Plugins.php b/system/src/Grav/Common/Plugins.php index 624c76c4c..5990ca0c4 100644 --- a/system/src/Grav/Common/Plugins.php +++ b/system/src/Grav/Common/Plugins.php @@ -61,6 +61,7 @@ class Plugins extends Iterator } $instance = $this->grav['themes']->load(); + $instance->configure(); if ($instance instanceof EventSubscriberInterface) { $events->addSubscriber($instance); } diff --git a/system/src/Grav/Common/Theme.php b/system/src/Grav/Common/Theme.php index 0b3e17017..d47e9b4fa 100644 --- a/system/src/Grav/Common/Theme.php +++ b/system/src/Grav/Common/Theme.php @@ -1,6 +1,61 @@ name = $name; + + parent::__construct($grav, $config); + } + + public function configure() { + $themeConfig = Yaml::instance(THEMES_DIR . "{$this->name}/{$this->name}.yaml")->content(); + + $this->config->merge(['themes' => [$this->name => $themeConfig]]); + + /** @var ResourceLocator $locator */ + $locator = $this->grav['locator']; + + // TODO: move + $registered = stream_get_wrappers(); + $schemes = $this->config->get("themes.{$this->name}.streams.scheme"); + + foreach ($schemes as $scheme => $config) { + if (isset($config['paths'])) { + $locator->addPath($scheme, '', $config['paths']); + } + if (isset($config['prefixes'])) { + foreach ($config['prefixes'] as $prefix => $paths) { + $locator->addPath($scheme, $prefix, $paths); + } + } + + if (in_array($scheme, $registered)) { + stream_wrapper_unregister($scheme); + } + $type = !empty($config['type']) ? $config['type'] : 'ReadOnlyStream'; + if ($type[0] != '\\') { + $type = '\\Grav\\Component\\Filesystem\\StreamWrapper\\' . $type; + } + + if (!stream_wrapper_register($scheme, $type)) { + throw new \InvalidArgumentException("Stream '{$type}' could not be initialized."); + } + + } + } } diff --git a/system/src/Grav/Common/Themes.php b/system/src/Grav/Common/Themes.php index bfa80fc49..852a34df7 100644 --- a/system/src/Grav/Common/Themes.php +++ b/system/src/Grav/Common/Themes.php @@ -101,13 +101,13 @@ class Themes $className = '\\Grav\\Theme\\' . ucfirst($name); if (class_exists($className)) { - $class = new $className($this->grav, $config); + $class = new $className($this->grav, $config, $name); } } } if (empty($class)) { - $class = new Theme($this->grav, $config); + $class = new Theme($this->grav, $config, $name); } return $class;