Skip to content

Commit

Permalink
Theme basepath (#2002)
Browse files Browse the repository at this point in the history
* Translation helper controler

* PHPdoc

* Correct parameter order for help

* Documentation for block preview luyadev/luya-module-cms#83

* Revert PHP doc

* Travis mysql connection failed

* config with env scope

* used call_user_func for callback

* Update Config.php

* theme load without exception as default

* theme load without exception as default

* theme load without exception as default

* Update ThemeManager.php

* changelog #1969

* Update CHANGELOG.md

* add php doc

* theme management image for guide #1983

* Update app-themes.md

* close #1970

* Update CHANGELOG.md

* Relative theme path to alias path

* Changelog

* Relative vendor path to alias path

* Tests for Relative vendor path to alias path

Co-authored-by: Basil <git@nadar.io>
  • Loading branch information
boehsermoe and nadar committed Apr 6, 2020
1 parent 263d0bf commit 00eb345
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 25 deletions.
1 change: 1 addition & 0 deletions core/CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE

+ [#2003](https://github.com/luyadev/luya/pull/2003) Add new `StringHelper::isNumeric()` which checks whether a value is nummeric (with regex instead of `is_numeric`) or not.
+ [#2004](https://github.com/luyadev/luya/pull/2004) Make impoter public in order to improve setting up unit tests and option to override in controller map configuration.
+ [#1970](https://github.com/luyadev/luya/issues/1970) Fixed a bug with relative theme paths.
+ [#2007](https://github.com/luyadev/luya/pull/2007) Fix bug in JsonBehavior and added new properties for auto decoding data after find.

## 1.1.0 (12. March 2020)
Expand Down
28 changes: 19 additions & 9 deletions core/theme/ThemeManager.php
Expand Up @@ -50,12 +50,14 @@ public function loadThemeConfig(string $basePath)
{
if (strpos($basePath, '@') === 0) {
$dir = Yii::getAlias($basePath);
} elseif (strpos($basePath, '/') !== 0) {
$dir = $basePath = Yii::$app->basePath . DIRECTORY_SEPARATOR . $basePath;
} else {
} elseif (strpos($basePath, '/') === 0) {
// absolute path
$dir = $basePath;
} else {
// relative path
throw new InvalidConfigException('Theme base path have to be absolute or alias: ' . $basePath);
}

// $basePath is an absolute path = /VENDOR/NAME/theme.json
if (is_file($basePath) && file_exists($basePath)) {
$themeFile = $basePath;
Expand Down Expand Up @@ -144,7 +146,7 @@ public function getThemes($throwException = false)
}

$themeDefinitions = $this->getThemeDefinitions();

foreach ($themeDefinitions as $themeDefinition) {
try {
$themeConfig = $this->loadThemeConfig($themeDefinition);
Expand Down Expand Up @@ -175,23 +177,31 @@ protected function getThemeDefinitions()
}

foreach (Yii::$app->getPackageInstaller()->getConfigs() as $config) {

/** @var PackageConfig $config */
$themeDefinitions = array_merge($themeDefinitions, $config->themes);
foreach ($config->themes as $theme) {
if (strpos($theme, '@') === 0 || strpos($theme, '/') === 0) {
$themeDefinitions[] = $theme;
} else {
$themeDefinitions[] = preg_replace('#^vendor/#', '@vendor/', $theme);
}
}
}

return $themeDefinitions;
}

/**
* @param string $basePath
* @param bool $throwException
*
* @return ThemeConfig
* @throws Exception
* @throws \yii\base\Exception
* @throws InvalidConfigException
*/
public function getThemeByBasePath(string $basePath)
public function getThemeByBasePath(string $basePath, $throwException = false)
{
$themes = $this->getThemes();
$themes = $this->getThemes($throwException);

if (!isset($themes[$basePath])) {
throw new InvalidArgumentException("Theme $basePath could not loaded.");
Expand Down
12 changes: 11 additions & 1 deletion tests/core/theme/ThemeConfigTest.php
@@ -1,8 +1,9 @@
<?php

namespace luya\theme;
namespace luyatests\core\theme;

use luya\helpers\Json;
use luya\theme\ThemeConfig;
use luyatests\LuyaWebTestCase;
use Yii;

Expand All @@ -29,6 +30,15 @@ public function testWithoutParents()
$this->assertCount(0, $themeConfig->getParents());
}

public function testBasePath()
{
$basePath = '@app/themes/blank3';
$config = Json::decode(file_get_contents(Yii::getAlias($basePath . '/theme.json')));

$themeConfig = new ThemeConfig($basePath, $config);
$this->assertEquals('@app/themes/blank3', $themeConfig->getBasePath());
}

public function testGetViewPath()
{
$basePath = '@app/themes/blank3';
Expand Down
58 changes: 43 additions & 15 deletions tests/core/theme/ThemeManagerTest.php
Expand Up @@ -159,27 +159,17 @@ public function testEmptyThemeDir()
}
}

public function testRelativeThemeDefinition()
public function testAliasThemeDefinition()
{
$relativePath = 'otherThemeLocation/foo';
$relativePath = '@app/otherThemeLocation/foo';

Yii::$app->getPackageInstaller()->getConfigs()['luyadev/luya-core']->setValue('themes', [$relativePath]);

$themeManager = new ThemeManager();
$basePath = realpath(__DIR__ . '/../../data') . DIRECTORY_SEPARATOR . $relativePath;
$themeConfig = $themeManager->getThemeByBasePath($basePath);


$themeConfig = $themeManager->getThemeByBasePath($relativePath, true);
$this->assertEquals("fooTheme", $themeConfig->name);
}

public function testDirectThemeFilePath()
{
$path = realpath(__DIR__ . '/../../data/themes/blank/theme.json');
$manager = new ThemeManager();
$config = $manager->loadThemeConfig($path);

$this->assertSame('blank', $config->name);
}

public function testAbsoluteThemeDefinition()
{
Expand All @@ -188,11 +178,49 @@ public function testAbsoluteThemeDefinition()
Yii::$app->getPackageInstaller()->getConfigs()['luyadev/luya-core']->setValue('themes', [$absolutePath]);

$themeManager = new ThemeManager();
$themeConfig = $themeManager->getThemeByBasePath($absolutePath);
$themeConfig = $themeManager->getThemeByBasePath($absolutePath, true);

$this->assertEquals("fooTheme", $themeConfig->name);
}

/**
* @expectedException \yii\base\InvalidConfigException
* @expectedExceptionMessage Theme base path have to be absolute or alias: vendorThemeLocation/foo
*/
public function testRelativeThemeDefinition()
{
$relativePath = 'vendorThemeLocation/foo';

Yii::$app->getPackageInstaller()->getConfigs()['luyadev/luya-core']->setValue('themes', [$relativePath]);

$themeManager = new ThemeManager();

$themeConfig = $themeManager->getThemeByBasePath($relativePath, true);
$this->assertEquals("fooTheme", $themeConfig->name);
}

public function testVendorThemeDefinition()
{
$relativePath = 'vendor/luyadev/luya-core/vendorThemeLocation/foo';

Yii::$app->getPackageInstaller()->getConfigs()['luyadev/luya-core']->setValue('themes', [$relativePath]);

$themeManager = new ThemeManager();

$themeConfig = $themeManager->getThemeByBasePath('@' . $relativePath, true);
$this->assertEquals("fooTheme", $themeConfig->name);
}


public function testDirectThemeFilePath()
{
$path = realpath(__DIR__ . '/../../data/themes/blank/theme.json');
$manager = new ThemeManager();
$config = $manager->loadThemeConfig($path);

$this->assertSame('blank', $config->name);
}

/**
* @expectedException \yii\base\InvalidArgumentException
* @expectedExceptionMessage Theme @app/themes/blank already registered.
Expand Down
@@ -0,0 +1,3 @@
{
"name": "fooTheme"
}

0 comments on commit 00eb345

Please sign in to comment.