From bce3afcca207c5289dbd286e4e06a00a0a8a370b Mon Sep 17 00:00:00 2001
From: Luciano Baraglia
Date: Wed, 16 Oct 2013 18:14:18 -0300
Subject: [PATCH 1/2] Module::getModule and Module::hasModule support for
sub-modules - see #983
---
framework/yii/base/Module.php | 37 ++++++++++++++++++++++++++++++-------
1 file changed, 30 insertions(+), 7 deletions(-)
diff --git a/framework/yii/base/Module.php b/framework/yii/base/Module.php
index 7df87a5..a85e249 100644
--- a/framework/yii/base/Module.php
+++ b/framework/yii/base/Module.php
@@ -333,7 +333,20 @@ abstract class Module extends Component
*/
public function hasModule($id)
{
- return isset($this->_modules[$id]);
+ if (strpos($id, '/') === false) {
+ return isset($this->_modules[$id]);
+ } else {
+ // it's a sub-module
+ $ids = explode('/', $id);
+ $module = $this;
+ foreach ($ids as $id) {
+ if (!isset($module->_modules[$id])) {
+ return false;
+ }
+ $module = $module->getModule($id);
+ }
+ return true;
+ }
}
/**
@@ -345,13 +358,23 @@ abstract class Module extends Component
*/
public function getModule($id, $load = true)
{
- if (isset($this->_modules[$id])) {
- if ($this->_modules[$id] instanceof Module) {
- return $this->_modules[$id];
- } elseif ($load) {
- Yii::trace("Loading module: $id", __METHOD__);
- return $this->_modules[$id] = Yii::createObject($this->_modules[$id], $id, $this);
+ if (strpos($id, '/') === false) {
+ if (isset($this->_modules[$id])) {
+ if ($this->_modules[$id] instanceof Module) {
+ return $this->_modules[$id];
+ } elseif ($load) {
+ Yii::trace("Loading module: $id", __METHOD__);
+ return $this->_modules[$id] = Yii::createObject($this->_modules[$id], $id, $this);
+ }
+ }
+ } else {
+ // it's a sub-module
+ $ids = explode('/', $id);
+ $module = $this;
+ foreach ($ids as $id) {
+ $module = $module->getModule($id);
}
+ return $module;
}
return null;
}
From c2457f95e91d238c9494d25047bbcc6d65f29eab Mon Sep 17 00:00:00 2001
From: Luciano Baraglia
Date: Wed, 16 Oct 2013 18:16:03 -0300
Subject: [PATCH 2/2] GII sub-modules support - see #871
---
framework/yii/gii/generators/controller/Generator.php | 2 +-
framework/yii/gii/generators/module/Generator.php | 9 ++++-----
2 files changed, 5 insertions(+), 6 deletions(-)
diff --git a/framework/yii/gii/generators/controller/Generator.php b/framework/yii/gii/generators/controller/Generator.php
index 9de9c17..b7c4095 100644
--- a/framework/yii/gii/generators/controller/Generator.php
+++ b/framework/yii/gii/generators/controller/Generator.php
@@ -197,7 +197,7 @@ class Generator extends \yii\gii\Generator
*/
public function getModule()
{
- if (($pos = strpos($this->controller, '/')) !== false) {
+ if (($pos = strrpos($this->controller, '/')) !== false) {
$id = substr($this->controller, 0, $pos);
if (($module = Yii::$app->getModule($id)) !== null) {
return $module;
diff --git a/framework/yii/gii/generators/module/Generator.php b/framework/yii/gii/generators/module/Generator.php
index 1b35db8..39c68f0 100644
--- a/framework/yii/gii/generators/module/Generator.php
+++ b/framework/yii/gii/generators/module/Generator.php
@@ -87,18 +87,17 @@ class Generator extends \yii\gii\Generator
$output = <<The module has been generated successfully.
-To access the module, you need to modify the application configuration as follows:
+To access the module, you need to add this to your application configuration:
EOD;
$code = <<array(
+ ......
+ 'modules' => array(
'{$this->moduleID}' => array(
'class' => '{$this->moduleClass}',
),
),
- ......
-);
+ ......
EOD;
return $output . '' . highlight_string($code, true) . '
';