From 855a97b35e56f1bf7a4a3efe071f06617ad769ca Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 12 Mar 2016 12:14:19 +1100 Subject: [PATCH] Set the default theme as part of the boot process Themes can also register themselves gracefully with Halycon after loading --- modules/cms/ServiceProvider.php | 18 ++++++++ modules/cms/classes/Theme.php | 80 ++++++++++++++++++++++++--------- 2 files changed, 77 insertions(+), 21 deletions(-) diff --git a/modules/cms/ServiceProvider.php b/modules/cms/ServiceProvider.php index 2b399081a..2a5da37a7 100644 --- a/modules/cms/ServiceProvider.php +++ b/modules/cms/ServiceProvider.php @@ -12,6 +12,7 @@ use System\Classes\SettingsManager; use System\Classes\CombineAssets; use Cms\Classes\ComponentManager; use Cms\Classes\Page as CmsPage; +use Cms\Classes\Theme as CmsTheme; class ServiceProvider extends ModuleServiceProvider { @@ -47,6 +48,7 @@ class ServiceProvider extends ModuleServiceProvider { parent::boot('cms'); + $this->bootDefaultTheme(); $this->bootMenuItemEvents(); $this->bootRichEditorEvents(); } @@ -284,4 +286,20 @@ class ServiceProvider extends ModuleServiceProvider }); } + /** + * Boot the default theme. + */ + protected function bootDefaultTheme() + { + $resolver = App::make('halcyon'); + if ($resolver->getDefaultTheme()) { + return; + } + + $defaultTheme = App::runningInBackend() + ? CmsTheme::getEditThemeCode() + : CmsTheme::getActiveThemeCode(); + + $resolver->setDefaultTheme($defaultTheme); + } } diff --git a/modules/cms/classes/Theme.php b/modules/cms/classes/Theme.php index 2efd3f242..a7a5b63a8 100644 --- a/modules/cms/classes/Theme.php +++ b/modules/cms/classes/Theme.php @@ -1,6 +1,7 @@ setDirName($dirName); + $theme->registerHalyconTheme(); return $theme; } @@ -134,18 +137,14 @@ class Theme } /** - * Returns the active theme. + * Returns the active theme code. * By default the active theme is loaded from the cms.activeTheme parameter, * but this behavior can be overridden by the cms.activeTheme event listeners. - * @return \Cms\Classes\Theme Returns the loaded theme object. + * @return string * If the theme doesn't exist, returns null. */ - public static function getActiveTheme() + public static function getActiveThemeCode() { - if (self::$activeThemeCache !== false) { - return self::$activeThemeCache; - } - $activeTheme = Config::get('cms.activeTheme'); if (DbDongle::hasDatabase()) { @@ -167,7 +166,24 @@ class Theme throw new SystemException(Lang::get('cms::lang.theme.active.not_set')); } - $theme = static::load($activeTheme); + return $activeTheme; + } + + + /** + * Returns the active theme. + * By default the active theme is loaded from the cms.activeTheme parameter, + * but this behavior can be overridden by the cms.activeTheme event listeners. + * @return \Cms\Classes\Theme Returns the loaded theme object. + * If the theme doesn't exist, returns null. + */ + public static function getActiveTheme() + { + if (self::$activeThemeCache !== false) { + return self::$activeThemeCache; + } + + $theme = static::load(static::getActiveThemeCode()); if (!File::isDirectory($theme->getPath())) { return self::$activeThemeCache = null; @@ -188,23 +204,18 @@ class Theme } /** - * Returns the edit theme. + * Returns the edit theme code. * By default the edit theme is loaded from the cms.editTheme parameter, * but this behavior can be overridden by the cms.editTheme event listeners. * If the edit theme is not defined in the configuration file, the active theme * is returned. - * @return \Cms\Classes\Theme Returns the loaded theme object. - * If the theme doesn't exist, returns null. + * @return string */ - public static function getEditTheme() + public static function getEditThemeCode() { - if (self::$editThemeCache !== false) { - return self::$editThemeCache; - } - $editTheme = Config::get('cms.editTheme'); if (!$editTheme) { - $editTheme = static::getActiveTheme()->getDirName(); + $editTheme = static::getActiveThemeCode(); } $apiResult = Event::fire('cms.editTheme', [], true); @@ -216,7 +227,20 @@ class Theme throw new SystemException(Lang::get('cms::lang.theme.edit.not_set')); } - $theme = static::load($editTheme); + return $editTheme; + } + + /** + * Returns the edit theme. + * @return \Cms\Classes\Theme Returns the loaded theme object. + */ + public static function getEditTheme() + { + if (self::$editThemeCache !== false) { + return self::$editThemeCache; + } + + $theme = static::load(static::getEditThemeCode()); if (!File::isDirectory($theme->getPath())) { return self::$editThemeCache = null; @@ -309,10 +333,10 @@ class Theme { $previewPath = '/assets/images/theme-preview.png'; if (File::exists($this->getPath().$previewPath)) { - return URL::asset('themes/'.$this->getDirName().$previewPath); + return Url::asset('themes/'.$this->getDirName().$previewPath); } - return URL::asset('modules/cms/assets/images/default-theme-preview.png'); + return Url::asset('modules/cms/assets/images/default-theme-preview.png'); } /** @@ -336,6 +360,20 @@ class Theme return $this->getConfigValue('form', false); } + /** + * Ensures this theme is registered as a Halcyon them datasource. + * @return void + */ + public function registerHalyconTheme() + { + $resolver = App::make('halcyon'); + + if (!$resolver->hasTheme($this->dirName)) { + $themeDatasource = new HalcyonFileTheme($this->getPath(), App::make('files')); + $resolver->addTheme($this->dirName, $themeDatasource); + } + } + /** * Implements the getter functionality. * @param string $name