2018-11-14 16:52:46 -06:00
|
|
|
<?php namespace Cms\Classes;
|
|
|
|
|
2018-11-22 16:54:35 -06:00
|
|
|
use Yaml;
|
2018-11-14 16:52:46 -06:00
|
|
|
|
|
|
|
/**
|
2020-05-05 13:12:45 -04:00
|
|
|
* The CMS meta file class, used for interacting with YAML files within the Halcyon datasources
|
2018-11-14 16:52:46 -06:00
|
|
|
*
|
2021-03-10 15:02:53 -06:00
|
|
|
* @package winter\wn-cms-module
|
2018-11-14 16:52:46 -06:00
|
|
|
* @author Luke Towers
|
|
|
|
*/
|
|
|
|
class Meta extends CmsObject
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string The container name associated with the model, eg: pages.
|
|
|
|
*/
|
|
|
|
protected $dirName = 'meta';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Cache store used by parseContent method.
|
|
|
|
*/
|
|
|
|
protected $contentDataCache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Allowable file extensions.
|
|
|
|
*/
|
|
|
|
protected $allowedExtensions = ['yaml'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Default file extension.
|
|
|
|
*/
|
|
|
|
protected $defaultExtension = 'yaml';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {inheritDoc}
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct(...func_get_args());
|
|
|
|
|
|
|
|
// Bind data processing to model events
|
|
|
|
$this->bindEvent('model.beforeSave', function () {
|
|
|
|
$this->content = $this->renderContent();
|
|
|
|
});
|
|
|
|
$this->bindEvent('model.afterFetch', function () {
|
2019-06-06 15:05:38 +02:00
|
|
|
$this->attributes = array_merge($this->attributes, $this->parseContent());
|
2018-11-14 16:52:46 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes the content attribute to an array of menu data.
|
|
|
|
* @return array|null
|
|
|
|
*/
|
|
|
|
protected function parseContent()
|
|
|
|
{
|
|
|
|
if ($this->contentDataCache !== null) {
|
|
|
|
return $this->contentDataCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsedData = Yaml::parse($this->content);
|
|
|
|
|
|
|
|
if (!is_array($parsedData)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->contentDataCache = $parsedData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the meta data as a content string in YAML format.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function renderContent()
|
|
|
|
{
|
|
|
|
return Yaml::render($this->settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compile the content for this CMS object, used by the theme logger.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function toCompiled()
|
|
|
|
{
|
|
|
|
return $this->renderContent();
|
|
|
|
}
|
2019-06-06 15:05:38 +02:00
|
|
|
}
|