Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theme bootstrap and layout fix #1963

Merged
merged 10 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions core/base/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,6 @@ public function init()
throw new InvalidConfigException(sprintf('The required component "%s" is not registered in the configuration file', $component));
}
}

if (Yii::$app->themeManager->hasActiveTheme) {
$this->layout = Yii::$app->themeManager->activeTheme->layout;
}

static::onLoad();
}
Expand Down
17 changes: 12 additions & 5 deletions core/theme/ThemeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,12 @@ protected function getActiveThemeBasePath()
/**
* Get all theme configs as array list.
*
* @param bool $throwException
*
* @return ThemeConfig[]
* @throws Exception
* @throws InvalidConfigException
* @throws \yii\base\Exception
*/
public function getThemes()
public function getThemes($throwException = true)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function getThemes has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.

{
if ($this->_themes) {
return $this->_themes;
Expand All @@ -141,8 +142,14 @@ public function getThemes()
$themeDefinitions = $this->getThemeDefinitions();

foreach ($themeDefinitions as $themeDefinition) {
$themeConfig = static::loadThemeConfig($themeDefinition);
$this->registerTheme($themeConfig);
try {
$themeConfig = static::loadThemeConfig($themeDefinition);
$this->registerTheme($themeConfig);
} catch (\yii\base\Exception $ex) {
if ($throwException) {
throw $ex;
}
}
}

return $this->_themes;
Expand Down
4 changes: 4 additions & 0 deletions core/web/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public function run($app)
} else {
// Frontend context
$app->themeManager->setup();

if ($app->themeManager->hasActiveTheme) {
$app->layout = $app->themeManager->activeTheme->layout;
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions core/web/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function init()
{
// call parent initializer
parent::init();

if (empty($this->theme) && Yii::$app->themeManager->hasActiveTheme) {
$this->theme = Yii::$app->themeManager->activeTheme;
}

// auto register csrf tags if enabled
if ($this->autoRegisterCsrf && Yii::$app->request->enableCsrfValidation) {
$this->registerCsrfMetaTags();
Expand Down
8 changes: 2 additions & 6 deletions tests/core/base/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ public function testThemeLayoutPath()

/** @var \luya\base\Module $module */
$module = Yii::$app->getModule('unitmodule');
$module->init();
$module->luyaBootstrap(Yii::$app);

$this->assertEquals('theme', $module->layout);

$module->useAppLayoutPath = false;
$this->assertContains('/modules/unitmodule/views/layouts', $module->getLayoutPath());
}
Expand All @@ -77,9 +75,7 @@ public function testThemeUseAppLayoutPath()

/** @var \luya\base\Module $module */
$module = Yii::$app->getModule('unitmodule');
$module->init();

$this->assertEquals('theme', $module->layout);
$module->luyaBootstrap(Yii::$app);

$module->useAppLayoutPath = true;
$this->assertContains('/themes/moduleTest/views/layouts', $module->getLayoutPath());
Expand Down
1 change: 1 addition & 0 deletions tests/core/web/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ public function testThemeManagerSetup()
$boot->bootstrap(Yii::$app);

$this->assertTrue(Yii::$app->themeManager->hasActiveTheme);
$this->assertEquals('theme', Yii::$app->layout);
}
}
17 changes: 17 additions & 0 deletions tests/core/web/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace luyatests\core\web;

use luya\theme\Theme;
use Yii;
use luya\web\View;
use luya\web\Asset;

Expand Down Expand Up @@ -50,4 +52,19 @@ public function testGetPublicHtml()
$url = $view->getPublicHtml();
$this->assertContains('public_html', $url);
}

public function testThemeSetup()
{
Yii::$app->themeManager->activeThemeName = '@app/themes/blank';
Yii::$app->themeManager->setup();

$view = new View();

$this->assertInstanceOf(Theme::class, $view->theme);

$expectedPath = realpath(Yii::getAlias('@luyatests/data/themes/blank'));
$this->assertEquals($expectedPath, $view->theme->basePath, 'Theme base path not correct.');
$this->assertEquals($expectedPath, Yii::getAlias('@activeTheme'), 'Alias path is not correct.');

}
}