Skip to content

Commit

Permalink
Merge branch 'master' into translation-controller
Browse files Browse the repository at this point in the history
  • Loading branch information
boehsermoe authored Sep 2, 2018
2 parents 5f902ee + b327e02 commit 1019876
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. This projec
## 1.0.11 (in progress)

+ [#1840](https://github.com/luyadev/luya/issues/1840) Convert mail message into alt body automatically.
+ [#1816](https://github.com/luyadev/luya/issues/1816) View mapping to change the view folder of actions inside modules.
+ [#1844](https://github.com/luyadev/luya/pull/1844) Added translation command to add easier a new record to the translation files. This command is used in the luya-env-dev project in order to develop on the LUYA modules or create your own extensions/modules.

## 1.0.10 (18. July 2018)
Expand Down
21 changes: 20 additions & 1 deletion core/base/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,26 @@ public function getUrlRules()
*
*/
public $useAppViewPath = false;


/**
* @var array mapping from action ID to view configurations.
* Each name-value pair specifies the configuration of a specifed oder wildcard action to a single view folder.
* The first match
* For example,
*
* ```php
* [
* 'default/index' => '@app/views/mymodule/default',
* 'login/info' => '@app/views/mymodule/login',
* 'default/*' => '@app/views/mymodule/default',
* '*' => '@app/views/mymodule',
* ]
* ```
*
* @since 1.0.11
*/
public $viewMap = [];

/**
* @var string if this/the module is included via another module (parent module), the parent module will write its
* name inside the child modules $context variable. For example the cms includes the news module, the context variable
Expand Down
21 changes: 18 additions & 3 deletions core/web/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,25 @@ public function registerAsset($className)
*/
public function getViewPath()
{
if ($this->module instanceof Module && $this->module->useAppViewPath) {
return '@app/views/'.$this->module->id.'/'.$this->id;
if ($this->module instanceof Module) {

if ($this->module->useAppViewPath) {
return '@app/views/' . $this->module->id . '/' . $this->id;
} elseif (is_array($this->module->viewMap)) {

$currentAction = $this->id . '/' . ($this->action ? $this->action->id : $this->defaultAction);
foreach ($this->module->viewMap as $action => $viewPath) {

// Special case for map all views of controller
if ($action === '*') {
return $viewPath . '/' . $this->id;
} elseif (fnmatch($action, $currentAction)) {
return $viewPath;
}
}
}
}

return parent::getViewPath();
}

Expand Down
14 changes: 14 additions & 0 deletions tests/core/ModuleApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ public function testApp()
$this->assertEquals('baz', Yii::$app->runAction('unitmodule/other/baz', []));
$this->assertEquals('baz', Yii::$app->runAction('unitmodule/other/baz/', []));
}

public function testViewMap()
{
// Works only with disabled $useAppViewPath
Yii::$app->getModule('unitmodule')->useAppViewPath = false;

// Test ViewmapController
$this->assertEquals('viewmap1', Yii::$app->runAction('unitmodule/viewmap/viewmap1'));
$this->assertEquals('viewmap2', Yii::$app->runAction('unitmodule/viewmap/viewmap2'));
$this->assertEquals('viewmap3', Yii::$app->runAction('unitmodule/viewmap/viewmap3'));

// Test ViewmapAllController
$this->assertEquals('viewmapAll', Yii::$app->runAction('unitmodule/viewmap-all'));
}
}
4 changes: 4 additions & 0 deletions tests/data/configs/webmoduleapp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
'remoteToken' => 'testtoken',
'basePath' => dirname(__DIR__),
'defaultRoute' => 'unitmodule',
'aliases' => [
'@runtime' => dirname(__DIR__) . '/runtime',
'@luyatests' => dirname(__DIR__) . '/../',
],
'modules' => [
'unitmodule' => [
'class' => 'luyatests\data\modules\unitmodule\Module',
Expand Down
7 changes: 7 additions & 0 deletions tests/data/modules/unitmodule/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
class Module extends \luya\base\Module implements CoreModuleInterface
{
public $useAppViewPath = true;

// Map all viewmap actions to viewmodule
public $viewMap = [
'viewmap/viewmap3' => '@luyatests/data/modules/viewmodule/views/stub',
'viewmap/*' => '@luyatests/data/modules/viewmodule/views/viewmap',
'*' => '@luyatests/data/modules/viewmodule/views',
];

public $defaultRoute = 'test';

Expand Down
11 changes: 11 additions & 0 deletions tests/data/modules/unitmodule/controllers/ViewmapAllController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace luyatests\data\modules\unitmodule\controllers;

class ViewmapAllController extends \luya\web\Controller
{
public function actionIndex()
{
return $this->render('viewmapAll');
}
}
21 changes: 21 additions & 0 deletions tests/data/modules/unitmodule/controllers/ViewmapController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace luyatests\data\modules\unitmodule\controllers;

class ViewmapController extends \luya\web\Controller
{
public function actionViewmap1()
{
return $this->render('viewmap1');
}

public function actionViewmap2()
{
return $this->render('viewmap2');
}

public function actionViewmap3()
{
return $this->render('viewmap3');
}
}
1 change: 1 addition & 0 deletions tests/data/modules/viewmodule/views/stub/viewmap3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
viewmap3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
viewmapAll
1 change: 1 addition & 0 deletions tests/data/modules/viewmodule/views/viewmap/viewmap1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
viewmap1
1 change: 1 addition & 0 deletions tests/data/modules/viewmodule/views/viewmap/viewmap2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
viewmap2

0 comments on commit 1019876

Please sign in to comment.