Expose the active theme metadata and configuration through the grav.theme object (#720)

This commit is contained in:
Matias Griese
2016-03-07 20:27:22 +02:00
parent e9c46e7ace
commit 10a89cf4e7
2 changed files with 100 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ use Grav\Common\Config\Config;
use RocketTheme\Toolbox\Event\EventDispatcher;
use RocketTheme\Toolbox\Event\EventSubscriberInterface;
use RocketTheme\Toolbox\File\YamlFile;
use Symfony\Component\Console\Exception\LogicException;
/**
* The Plugin object just holds the id and path to a plugin.
@@ -14,7 +15,7 @@ use RocketTheme\Toolbox\File\YamlFile;
* @author RocketTheme
* @license MIT
*/
class Plugin implements EventSubscriberInterface
class Plugin implements EventSubscriberInterface, \ArrayAccess
{
/**
* @var string
@@ -37,6 +38,7 @@ class Plugin implements EventSubscriberInterface
protected $config;
protected $active = true;
protected $blueprint;
/**
* By default assign all methods as listeners using the default priority.
@@ -84,6 +86,16 @@ class Plugin implements EventSubscriberInterface
return $this;
}
/**
* Get configuration of the plugin.
*
* @return Config
*/
public function config()
{
return $this->config["plugins.{$this->name}"];
}
/**
* @return bool
*/
@@ -138,6 +150,59 @@ class Plugin implements EventSubscriberInterface
}
}
/**
* Whether or not an offset exists.
*
* @param mixed $offset An offset to check for.
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function offsetExists($offset)
{
$this->loadBlueprint();
if ($offset === 'title') {
$offset = 'name';
}
return isset($this->blueprint[$offset]);
}
/**
* Returns the value at specified offset.
*
* @param mixed $offset The offset to retrieve.
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
$this->loadBlueprint();
if ($offset === 'title') {
$offset = 'name';
}
return isset($this->blueprint[$offset]) ? $this->blueprint[$offset] : null;
}
/**
* Assigns a value to the specified offset.
*
* @param mixed $offset The offset to assign the value to.
* @param mixed $value The value to set.
*/
public function offsetSet($offset, $value)
{
throw new LogicException(__CLASS__ . ' blueprints cannot be modified.');
}
/**
* Unsets an offset.
*
* @param mixed $offset The offset to unset.
*/
public function offsetUnset($offset)
{
throw new LogicException(__CLASS__ . ' blueprints cannot be modified.');
}
/**
* This function will search a string for markdown links in a specific format. The link value can be
* optionally compared against via the $internal_regex and operated on by the callback $function
@@ -232,4 +297,16 @@ class Plugin implements EventSubscriberInterface
return true;
}
/**
* Load blueprints.
*/
protected function loadBlueprint()
{
if (!$this->blueprint) {
$grav = Grav::instance();
$plugins = $grav['plugins'];
$this->blueprint = $plugins->get($this->name)->blueprints();
}
}
}

View File

@@ -22,6 +22,16 @@ class Theme extends Plugin
parent::__construct($name, $grav, $config);
}
/**
* Get configuration of the plugin.
*
* @return Config
*/
public function config()
{
return $this->config["themes.{$this->name}"];
}
/**
* Persists to disk the theme parameters currently stored in the Grav Config object
*
@@ -45,4 +55,16 @@ class Theme extends Plugin
return true;
}
/**
* Load blueprints.
*/
protected function loadBlueprint()
{
if (!$this->blueprint) {
$grav = Grav::instance();
$themes = $grav['themes'];
$this->blueprint = $themes->get($this->name)->blueprints();
}
}
}