diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 04560c7..8ec111d 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -29,6 +29,7 @@ Yii Framework 2 Change Log - Bug #18909: Fix bug with binding default action parameters for controllers (bizley) - Bug #18955: Check `yiisoft/yii2-swiftmailer` before using as default mailer in `yii\base\Application` (WinterSilence) - Bug #18988: Fix default value of `yii\console\controllers\MessageController::$translator` (WinterSilence) +- Enh #19005: Add `yii\base\Module::setControllerPath()` (WinterSilence) - Bug #18993: Load defaults by `attributes()` in `yii\db\ActiveRecord::loadDefaultValues()` (WinterSilence) - Bug #19021: Fix return type in PhpDoc `yii\db\Migration` functions `up()`, `down()`, `safeUp()` and `safeDown()` (WinterSilence, rhertogh) - Bug #19031: Fix displaying console help for parameters with declared types (WinterSilence) diff --git a/framework/base/Module.php b/framework/base/Module.php index 2562f0c..184eada 100644 --- a/framework/base/Module.php +++ b/framework/base/Module.php @@ -27,8 +27,7 @@ use yii\di\ServiceLocator; * start with `@`) and the array values are the corresponding paths or aliases. See [[setAliases()]] for an * example. This property is write-only. * @property string $basePath The root directory of the module. - * @property-read string $controllerPath The directory that contains the controller classes. This property is - * read-only. + * @property string $controllerPath The root directory that contains the controller classes. * @property string $layoutPath The root directory of layout files. Defaults to "[[viewPath]]/layouts". * @property array $modules The modules (indexed by their IDs). * @property-read string $uniqueId The unique ID of the module. This property is read-only. @@ -116,6 +115,10 @@ class Module extends ServiceLocator */ private $_basePath; /** + * @var string The root directory that contains the controller classes for this module. + */ + private $_controllerPath; + /** * @var string the root directory that contains view files for this module */ private $_viewPath; @@ -127,6 +130,7 @@ class Module extends ServiceLocator * @var array child modules of this module */ private $_modules = []; + /** * @var string|callable the version of this module. * Version can be specified as a PHP callback, which can accept module instance as an argument and should @@ -254,7 +258,22 @@ class Module extends ServiceLocator */ public function getControllerPath() { - return Yii::getAlias('@' . str_replace('\\', '/', $this->controllerNamespace)); + if ($this->_controllerPath === null) { + $this->_controllerPath = Yii::getAlias('@' . str_replace('\\', '/', $this->controllerNamespace)); + } + + return $this->_controllerPath; + } + + /** + * Sets the directory that contains the controller classes. + * @param string $path the root directory that contains the controller classes. + * @throws InvalidArgumentException if the directory is invalid. + * @since 2.0.44 + */ + public function setControllerPath($path) + { + $this->_controllerPath = Yii::getAlias($path); } /** diff --git a/tests/framework/base/ModuleTest.php b/tests/framework/base/ModuleTest.php index c4b3ae4..2df5cc5 100644 --- a/tests/framework/base/ModuleTest.php +++ b/tests/framework/base/ModuleTest.php @@ -37,11 +37,22 @@ class ModuleTest extends TestCase $this->assertEquals('parent', $child2->module->id); } - public function testControllerPath() + public function testGetControllerPath() { $module = new TestModule('test'); + $controllerPath = __DIR__ . DIRECTORY_SEPARATOR . 'controllers'; + $this->assertEquals('yiiunit\framework\base\controllers', $module->controllerNamespace); - $this->assertEquals(__DIR__ . DIRECTORY_SEPARATOR . 'controllers', str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $module->controllerPath)); + $this->assertEquals($controllerPath, str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $module->getControllerPath())); + } + + public function testSetControllerPath() + { + $module = new TestModule('test'); + $controllerPath = __DIR__ . DIRECTORY_SEPARATOR . 'controllers'; + + $module->setControllerPath($controllerPath); + $this->assertEquals($controllerPath, $module->getControllerPath()); } public function testSetupVersion()