Introduce Resources component for testing

This should be considered experimental at this stage, however this component is used in every October website we've built thus far, it makes sense for it to be included in the core. Once tested we will look at documenting it with an accompanying screencast video.
This commit is contained in:
Samuel Georges 2017-03-20 17:31:35 +11:00
parent 1ad5a58ae8
commit 31ba5e29bd
3 changed files with 165 additions and 1 deletions

View File

@ -64,6 +64,7 @@ class ServiceProvider extends ModuleServiceProvider
{
ComponentManager::instance()->registerComponents(function ($manager) {
$manager->registerComponent('Cms\Components\ViewBag', 'viewBag');
$manager->registerComponent('Cms\Components\Resources', 'resources');
});
}

View File

@ -0,0 +1,163 @@
<?php namespace Cms\Components;
use File;
use Config;
use Cms\Classes\ComponentBase;
use System\Classes\CombineAssets;
/**
* Resources component
*/
class Resources extends ComponentBase
{
/**
* @var string The default JavaScript directory
*/
public $jsDir = 'js';
/**
* @var string The default CSS directory
*/
public $cssDir = 'css';
/**
* @var string The default LESS directory
*/
public $lessDir = 'less';
/**
* @return array
*/
public function componentDetails()
{
return [
'name' => 'Resources',
'description' => 'Easily reference theme assets for inclusion on a page.',
];
}
/**
* @return array
*/
public function defineProperties()
{
return [
'js' => [
'title' => 'JavaScript',
'description' => 'JavaScript file(s) in the assets/js folder',
'type' => 'stringList',
'showExternalParam' => false
],
'less' => [
'title' => 'LESS',
'description' => 'LESS file(s) in the assets/less folder',
'type' => 'stringList',
'showExternalParam' => false
],
'css' => [
'title' => 'CSS',
'description' => 'Stylesheet file(s) in the assets/css folder',
'type' => 'stringList',
'showExternalParam' => false
],
'vars' => [
'title' => 'Variables',
'description' => 'Page variables name(s) and value(s)',
'type' => 'dictionary',
'showExternalParam' => false
]
];
}
public function init()
{
$this->assetPath = $this->guessAssetPath();
$this->jsDir = $this->guessAssetDirectory(['js', 'javascript'], $this->jsDir);
}
public function onRun()
{
/*
* JavaScript
*/
$js = [];
if ($assets = $this->property('js')) {
$js += array_map([$this, 'prefixJs'], (array) $assets);
}
/*
* LESS
*/
$less = [];
if ($assets = $this->property('less')) {
$less += array_map([$this, 'prefixLess'], (array) $assets);
}
/*
* CSS
*/
$css = [];
if ($assets = $this->property('css')) {
$css += array_map([$this, 'prefixCss'], (array) $assets);
}
if (count($js)) {
$this->addJs(CombineAssets::combine($js, $this->assetPath));
}
if (count($less)) {
$this->addCss(CombineAssets::combine($less, $this->assetPath));
}
if (count($css)) {
$this->addCss(CombineAssets::combine($css, $this->assetPath));
}
/*
* Variables
*/
if ($vars = $this->property('vars')) {
foreach ((array) $vars as $key => $value) {
$this->page[$key] = $value;
}
}
}
protected function prefixJs($value)
{
return $this->jsDir.'/'.trim($value);
}
protected function prefixCss($value)
{
return $this->cssDir.'/'.trim($value);
}
protected function prefixLess($value)
{
return $this->lessDir.'/'.trim($value);
}
protected function guessAssetDirectory(array $possible, $default = null)
{
foreach ($possible as $option) {
if (File::isDirectory($this->assetPath.'/'.$option)) {
return $option;
}
}
return $default;
}
protected function guessAssetPath()
{
$baseTheme = themes_path().'/'.$this->getTheme()->getDirName();
if (File::isDirectory($baseTheme.'/assets')) {
return $baseTheme.'/assets';
}
else {
return $baseTheme.'/resources';
}
}
}

View File

@ -1,7 +1,7 @@
<?php namespace Cms\Components;
use Cms\Classes\ComponentBase;
use Cms\Classes\CodeBase;
use Cms\Classes\ComponentBase;
class UnknownComponent extends ComponentBase
{