Add basic theme configuration support

This commit is contained in:
Matias Griese
2014-08-20 20:51:49 +03:00
parent 9d0e917e60
commit fae64f5b7a
4 changed files with 58 additions and 3 deletions

View File

@@ -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

View File

@@ -61,6 +61,7 @@ class Plugins extends Iterator
}
$instance = $this->grav['themes']->load();
$instance->configure();
if ($instance instanceof EventSubscriberInterface) {
$events->addSubscriber($instance);
}

View File

@@ -1,6 +1,61 @@
<?php
namespace Grav\Common;
use Grav\Common\Filesystem\File\Yaml;
use Grav\Component\Filesystem\ResourceLocator;
class Theme extends Plugin
{
public $name;
/**
* Constructor.
*
* @param Grav $grav
* @param Config $config
* @param string $name
*/
public function __construct(Grav $grav, Config $config, $name)
{
$this->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.");
}
}
}
}

View File

@@ -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;