Commit bbea1ab1 by Qiang Xue

Fixes #2629: `Module::controllerPath` is now read only, and all controller…

Fixes #2629: `Module::controllerPath` is now read only, and all controller classes must be namespaced under `Module::controllerNamespace`
parent d4869eab
...@@ -9,7 +9,6 @@ return [ ...@@ -9,7 +9,6 @@ return [
'id' => 'basic-console', 'id' => 'basic-console',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'preload' => ['log'], 'preload' => ['log'],
'controllerPath' => dirname(__DIR__) . '/commands',
'controllerNamespace' => 'app\commands', 'controllerNamespace' => 'app\commands',
'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'), 'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'),
'components' => [ 'components' => [
......
...@@ -45,7 +45,6 @@ $application = new yii\console\Application([ ...@@ -45,7 +45,6 @@ $application = new yii\console\Application([
'basePath' => __DIR__, 'basePath' => __DIR__,
'enableCoreCommands' => false, 'enableCoreCommands' => false,
'controllerNamespace' => 'yii\\apidoc\\commands', 'controllerNamespace' => 'yii\\apidoc\\commands',
'controllerPath' => '@yii/apidoc/commands',
]); ]);
if ($vendorPath !== null) { if ($vendorPath !== null) {
$application->setVendorPath($vendorPath); $application->setVendorPath($vendorPath);
......
...@@ -189,6 +189,7 @@ Yii Framework 2 Change Log ...@@ -189,6 +189,7 @@ Yii Framework 2 Change Log
- Chg #2426: Changed URL creation method signatures to be consistent (samdark) - Chg #2426: Changed URL creation method signatures to be consistent (samdark)
- Chg #2544: Changed `DetailView`'s `name:format:label` to `attribute:format:label` to match `GridView` (samdark) - Chg #2544: Changed `DetailView`'s `name:format:label` to `attribute:format:label` to match `GridView` (samdark)
- Chg #2603: `yii\base\ErrorException` now extends `\ErrorException` (samdark) - Chg #2603: `yii\base\ErrorException` now extends `\ErrorException` (samdark)
- Chg #2629: `Module::controllerPath` is now read only, and all controller classes must be namespaced under `Module::controllerNamespace`. (qiangxue)
- Chg: Renamed `yii\jui\Widget::clientEventsMap` to `clientEventMap` (qiangxue) - Chg: Renamed `yii\jui\Widget::clientEventsMap` to `clientEventMap` (qiangxue)
- Chg: Renamed `ActiveRecord::getPopulatedRelations()` to `getRelatedRecords()` (qiangxue) - Chg: Renamed `ActiveRecord::getPopulatedRelations()` to `getRelatedRecords()` (qiangxue)
- Chg: Renamed `attributeName` and `className` to `targetAttribute` and `targetClass` for `UniqueValidator` and `ExistValidator` (qiangxue) - Chg: Renamed `attributeName` and `className` to `targetAttribute` and `targetClass` for `UniqueValidator` and `ExistValidator` (qiangxue)
......
...@@ -25,7 +25,8 @@ use Yii; ...@@ -25,7 +25,8 @@ use Yii;
* This property is write-only. * This property is write-only.
* @property string $basePath The root directory of the module. * @property string $basePath The root directory of the module.
* @property array $components The components (indexed by their IDs). * @property array $components The components (indexed by their IDs).
* @property string $controllerPath The directory that contains the controller classes. * @property string $controllerPath The directory that contains the controller classes according to [[controllerNamespace]].
* This property is read-only.
* @property string $layoutPath The root directory of layout files. Defaults to "[[viewPath]]/layouts". * @property string $layoutPath The root directory of layout files. Defaults to "[[viewPath]]/layouts".
* @property array $modules The modules (indexed by their IDs). * @property array $modules The modules (indexed by their IDs).
* @property string $uniqueId The unique ID of the module. This property is read-only. * @property string $uniqueId The unique ID of the module. This property is read-only.
...@@ -106,10 +107,6 @@ class Module extends Component ...@@ -106,10 +107,6 @@ class Module extends Component
*/ */
private $_layoutPath; private $_layoutPath;
/** /**
* @var string the directory containing controller classes in the module.
*/
private $_controllerPath;
/**
* @var array child modules of this module * @var array child modules of this module
*/ */
private $_modules = []; private $_modules = [];
...@@ -224,28 +221,12 @@ class Module extends Component ...@@ -224,28 +221,12 @@ class Module extends Component
} }
/** /**
* Returns the directory that contains the controller classes. * Returns the directory that contains the controller classes according to [[controllerNamespace]].
* Defaults to "[[basePath]]/controllers".
* @return string the directory that contains the controller classes. * @return string the directory that contains the controller classes.
*/ */
public function getControllerPath() public function getControllerPath()
{ {
if ($this->_controllerPath !== null) { return Yii::getAlias('@' . str_replace('\\', '/', $this->controllerNamespace));
return $this->_controllerPath;
} else {
return $this->_controllerPath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'controllers';
}
}
/**
* Sets the directory that contains the controller classes.
* @param string $path the directory that contains the controller classes.
* This can be either a directory name or a path alias.
* @throws InvalidParamException if the directory is invalid
*/
public function setControllerPath($path)
{
$this->_controllerPath = Yii::getAlias($path);
} }
/** /**
...@@ -664,7 +645,7 @@ class Module extends Component ...@@ -664,7 +645,7 @@ class Module extends Component
* Creates a controller based on the given controller ID. * Creates a controller based on the given controller ID.
* *
* The controller ID is relative to this module. The controller class * The controller ID is relative to this module. The controller class
* should be located under [[controllerPath]] and namespaced under [[controllerNamespace]]. * should be namespaced under [[controllerNamespace]].
* *
* Note that this method does not check [[modules]] or [[controllerMap]]. * Note that this method does not check [[modules]] or [[controllerMap]].
* *
...@@ -689,7 +670,7 @@ class Module extends Component ...@@ -689,7 +670,7 @@ class Module extends Component
} }
$className = str_replace(' ', '', ucwords(str_replace('-', ' ', $className))) . 'Controller'; $className = str_replace(' ', '', ucwords(str_replace('-', ' ', $className))) . 'Controller';
$classFile = $this->controllerPath . '/' . $prefix . $className . '.php'; $classFile = $this->getControllerPath() . '/' . $prefix . $className . '.php';
$className = ltrim($this->controllerNamespace . '\\' . str_replace('/', '\\', $prefix) . $className, '\\'); $className = ltrim($this->controllerNamespace . '\\' . str_replace('/', '\\', $prefix) . $className, '\\');
if (strpos($className, '-') !== false || !is_file($classFile)) { if (strpos($className, '-') !== false || !is_file($classFile)) {
return null; return null;
......
...@@ -24,7 +24,7 @@ use yii\base\InvalidRouteException; ...@@ -24,7 +24,7 @@ use yii\base\InvalidRouteException;
* - User specifies which command to run on the command line; * - User specifies which command to run on the command line;
* - The command processes the user request with the specified parameters. * - The command processes the user request with the specified parameters.
* *
* The command classes reside in the directory specified by [[controllerPath]]. * The command classes should be under the namespace specified by [[controllerNamespace]].
* Their naming should follow the same naming convention as controllers. For example, the `help` command * Their naming should follow the same naming convention as controllers. For example, the `help` command
* is implemented using the `HelpController` class. * is implemented using the `HelpController` class.
* *
......
...@@ -19,7 +19,6 @@ require(__DIR__ . '/Yii.php'); ...@@ -19,7 +19,6 @@ require(__DIR__ . '/Yii.php');
$application = new yii\console\Application([ $application = new yii\console\Application([
'id' => 'yii-console', 'id' => 'yii-console',
'basePath' => __DIR__ . '/console', 'basePath' => __DIR__ . '/console',
'controllerPath' => '@yii/console/controllers',
]); ]);
$exitCode = $application->run(); $exitCode = $application->run();
exit($exitCode); exit($exitCode);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment