Unified module bundled theme ability, moved theme helpers method to ThemeHelper

This commit is contained in:
Lucas Bartholemy 2016-05-05 14:39:57 +02:00
parent da4bcccf06
commit d8f858bf7c
5 changed files with 199 additions and 127 deletions

View File

@ -10,6 +10,7 @@ namespace humhub\components;
use Yii;
use yii\helpers\FileHelper;
use humhub\libs\ThemeHelper;
use humhub\models\Setting;
/**
@ -30,6 +31,11 @@ class Theme extends \yii\base\Theme
*/
private $_baseUrl = null;
/**
* Indicates that resources should be published via assetManager
*/
public $publishResources = false;
/**
* @inheritdoc
*/
@ -46,26 +52,36 @@ class Theme extends \yii\base\Theme
}
/**
* @return string the base URL (without ending slash) for this theme. All resources of this theme are considered
* to be under this base URL.
* @inheritdoc
*/
public function getBaseUrl()
{
if ($this->_baseUrl !== null) {
return $this->_baseUrl;
}
$this->_baseUrl = rtrim(Yii::getAlias('@web/themes/' . $this->name), '/');
$this->_baseUrl = ($this->publishResources) ? $this->publishResources() : $this->theme->getBaseUrl();
return $this->_baseUrl;
}
/**
* This method will be called before when this theme is written to the
* dynamic configuration file.
*/
public function beforeActivate()
{
// Force republish theme files
$this->publishResources(true);
// Store color variables to configuration
$this->storeColorsToConfig();
}
/**
* @inheritdoc
*/
public function applyTo($path)
{
$autoPath = $this->autoFindModuleView($path);
if ($autoPath !== null && file_exists($autoPath)) {
return $autoPath;
@ -84,6 +100,25 @@ class Theme extends \yii\base\Theme
return parent::applyTo($path);
}
/**
* Publishs theme assets (e.g. images or css)
*
* @param boolean|null $force
* @return string url of published resources
*/
public function publishResources($force = null)
{
if ($force === null) {
$force = (YII_DEBUG);
}
$published = Yii::$app->assetManager->publish(
$this->getBasePath(), ['forceCopy' => $force, 'except' => ['views/']]
);
return $published[1];
}
/**
* Tries to automatically maps the view file of a module to a themed one.
*
@ -122,113 +157,13 @@ class Theme extends \yii\base\Theme
}
/**
* Returns an array of all installed themes.
*
* @return Array Theme instances
* Stores color informations to configuration for use in modules.
*/
public static function getThemes()
public function storeColorsToConfig()
{
$themes = array();
$themePaths = [];
$themePaths[] = \Yii::getAlias('@webroot/themes');
// Collect enabled module theme paths
foreach (Yii::$app->getModules() as $module) {
$basePath = "";
if (is_array($module)) {
if (isset($module['class'])) {
$reflector = new \ReflectionClass($module['class']);
$basePath = dirname($reflector->getFileName());
}
} else {
$basePath = $module->getBasePath();
}
if (is_dir($basePath . DIRECTORY_SEPARATOR . 'themes')) {
$themePaths[] = $basePath . DIRECTORY_SEPARATOR . 'themes';
}
}
foreach ($themePaths as $themePath) {
foreach (scandir($themePath) as $file) {
if ($file == "." || $file == ".." || !is_dir($themePath . DIRECTORY_SEPARATOR . $file)) {
continue;
}
$themes[] = Yii::createObject([
'class' => 'humhub\components\Theme',
'basePath' => $themePath . DIRECTORY_SEPARATOR . $file,
'name' => $file
]);
}
}
return $themes;
}
/**
* Returns a Theme by given name
*
* @param string $name of the theme
* @return Theme
*/
public static function getThemeByName($name)
{
foreach (self::getThemes() as $theme) {
if ($theme->name === $name) {
return $theme;
}
}
return null;
}
/**
* Returns configuration array of given theme
*
* @param Theme|string $theme name or theme instance
* @return array Configuration
*/
public static function getThemeConfig($theme)
{
if (is_string($theme)) {
$theme = self::getThemeByName($theme);
}
if ($theme === null) {
return [];
}
return [
'components' => [
'view' => [
'theme' => [
'name' => $theme->name,
'basePath' => $theme->getBasePath()
],
],
'mailer' => [
'view' => [
'theme' => [
'name' => $theme->name,
'basePath' => $theme->getBasePath()
]
]
]
]
];
}
public static function setColorVariables($themeName)
{
$theme = self::getThemeByName($themeName);
if ($theme === null) {
return;
}
$lessFileName = Yii::getAlias($theme->getBasePath() . '/css/theme.less');
$lessFileName = $this->getBasePath() . '/css/theme.less';
if (file_exists($lessFileName)) {
$file = fopen($lessFileName, "r") or die("Unable to open file!");
$less = fread($file, filesize($lessFileName));
fclose($file);
$less = file_get_contents($lessFileName);
$startDefault = strpos($less, '@default') + 10;
$startPrimary = strpos($less, '@primary') + 10;
@ -247,4 +182,19 @@ class Theme extends \yii\base\Theme
}
}
/**
* Store colors to configuration.
*
* @deprecated since version 1.1
* @param type $themeName
*/
public static function setColorVariables($themeName)
{
$theme = ThemeHelper::getThemeByName($themeName);
if ($theme !== null) {
$theme->storeColorsToConfig();
}
}
}

View File

@ -10,7 +10,7 @@ namespace humhub\libs;
use Yii;
use yii\helpers\ArrayHelper;
use humhub\components\Theme;
use humhub\libs\ThemeHelper;
use humhub\models\Setting;
/**
@ -160,13 +160,6 @@ class DynamicConfig extends \yii\base\Object
'class' => $cacheClass,
'keyPrefix' => Yii::$app->id
];
// Prefix APC Cache Keys
//if ($cacheClass == 'yii\caching\ApcCache') {
// $config['components']['cache'] = [
// 'keyPrefix' => Yii::$app->id
// ];
//}
}
// Add User settings
$config['components']['user'] = array();
@ -207,7 +200,7 @@ class DynamicConfig extends \yii\base\Object
$mail['useFileTransport'] = true;
}
$config['components']['mailer'] = $mail;
$config = ArrayHelper::merge($config, Theme::getThemeConfig(Setting::Get('theme')));
$config = ArrayHelper::merge($config, ThemeHelper::getThemeConfig(Setting::Get('theme')));
$config['params']['config_created_at'] = time();
foreach (Setting::find()->all() as $setting) {

View File

@ -0,0 +1,132 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2016 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\libs;
use Yii;
use humhub\components\Theme;
use yii\helpers\ArrayHelper;
/**
* ThemeHelper
*
* @since 1.1
* @author Luke
*/
class ThemeHelper
{
/**
* Returns an array of all available themes.
*
* @return Array Theme instances
*/
public static function getThemes()
{
$themes = self::getThemesByPath(Yii::getAlias('@webroot/themes'));
// Collect themes provided by modules
foreach (Yii::$app->getModules() as $id => $module) {
if (is_array($module)) {
$module = Yii::$app->getModule($id);
}
$moduleThemePath = $module->getBasePath() . DIRECTORY_SEPARATOR . 'themes';
if (is_dir($moduleThemePath)) {
$themes = ArrayHelper::merge($themes, self::getThemesByPath($moduleThemePath, ['publishResources' => true]));
}
}
return $themes;
}
/**
* Returns an array of Theme instances of a given directory
*
* @param string $path the theme directory
* @param type $additionalOptions options for Theme instance
* @return Theme[]
*/
public static function getThemesByPath($path, $additionalOptions = [])
{
$themes = [];
if (is_dir($path)) {
foreach (scandir($path) as $file) {
// Skip dots and non directories
if ($file == "." || $file == ".." || !is_dir($path . DIRECTORY_SEPARATOR . $file)) {
continue;
}
$themes[] = Yii::createObject(ArrayHelper::merge([
'class' => 'humhub\components\Theme',
'basePath' => $path . DIRECTORY_SEPARATOR . $file,
'name' => $file
], $additionalOptions));
}
}
return $themes;
}
/**
* Returns a Theme by given name
*
* @param string $name of the theme
* @return Theme
*/
public static function getThemeByName($name)
{
foreach (self::getThemes() as $theme) {
if ($theme->name === $name) {
return $theme;
}
}
return null;
}
/**
* Returns configuration array of given theme
*
* @param Theme|string $theme name or theme instance
* @return array Configuration
*/
public static function getThemeConfig($theme)
{
if (is_string($theme)) {
$theme = self::getThemeByName($theme);
}
if ($theme === null) {
return [];
}
$theme->beforeActivate();
return [
'components' => [
'view' => [
'theme' => [
'name' => $theme->name,
'basePath' => $theme->getBasePath(),
'publishResources' => $theme->publishResources,
],
],
'mailer' => [
'view' => [
'theme' => [
'name' => $theme->name,
'basePath' => $theme->getBasePath(),
'publishResources' => $theme->publishResources,
]
]
]
]
];
}
}

View File

@ -9,8 +9,8 @@
namespace humhub\modules\admin\controllers;
use Yii;
use yii\helpers\Url;
use humhub\libs\DynamicConfig;
use humhub\libs\ThemeHelper;
use humhub\models\Setting;
use humhub\models\UrlOembed;
use humhub\modules\admin\components\Controller;
@ -162,7 +162,7 @@ class SettingController extends Controller
$groups = array();
$groups[''] = Yii::t('AdminModule.controllers_SettingController', 'None - shows dropdown in user registration.');
foreach (\humhub\modules\user\models\Group::find()->all() as $group) {
if(!$group->is_admin_group) {
if (!$group->is_admin_group) {
$groups[$group->id] = $group->name;
}
}
@ -242,7 +242,7 @@ class SettingController extends Controller
Ldap::getInstance()->refreshUsers();
return $this->redirect(['/admin/setting/authentication-ldap']);
}
/**
* Caching Options
*/
@ -390,9 +390,6 @@ class SettingController extends Controller
$logoImage->setNew($form->logo);
}
// read and save colors from current theme
\humhub\components\Theme::setColorVariables($form->theme);
DynamicConfig::rewrite();
Yii::$app->getSession()->setFlash('data-saved', Yii::t('AdminModule.controllers_SettingController', 'Saved'));
@ -401,7 +398,7 @@ class SettingController extends Controller
}
$themes = [];
foreach (\humhub\components\Theme::getThemes() as $theme) {
foreach (ThemeHelper::getThemes() as $theme) {
$themes[$theme->name] = $theme->name;
}

View File

@ -9,9 +9,9 @@
namespace humhub\modules\admin\models\forms;
use Yii;
use humhub\libs\ThemeHelper;
/**
* @package humhub.modules_core.admin.forms
* @since 0.5
*/
class DesignSettingsForm extends \yii\base\Model
@ -30,7 +30,7 @@ class DesignSettingsForm extends \yii\base\Model
public function rules()
{
$themes = [];
foreach (\humhub\components\Theme::getThemes() as $theme) {
foreach (ThemeHelper::getThemes() as $theme) {
$themes[] = $theme->name;
}