From e8eccc281d79b0186b1d2ad3298f8ccf100989fc Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 19 Oct 2013 02:01:58 +0400 Subject: [PATCH 1/6] Refactored instanceof in Module into method in Application --- framework/yii/base/Application.php | 14 ++++++++++++++ framework/yii/base/Module.php | 12 ++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index d92b05c..b3803cb 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -172,6 +172,20 @@ abstract class Application extends Module } /** + * Initializes the application. + * This method is called after the application is created and initialized with property values + * given in configuration. + */ + public function init() + { + $this->preloadComponents(); + if ($this->controllerNamespace === null) { + $this->controllerNamespace = 'app\\controllers'; + } + } + + + /** * Loads components that are declared in [[preload]]. * @throws InvalidConfigException if a component or module to be preloaded is unknown */ diff --git a/framework/yii/base/Module.php b/framework/yii/base/Module.php index abf39ec..8e62d0a 100644 --- a/framework/yii/base/Module.php +++ b/framework/yii/base/Module.php @@ -167,20 +167,16 @@ abstract class Module extends Component /** * Initializes the module. * This method is called after the module is created and initialized with property values - * given in configuration. The default implement will create a path alias using the module [[id]] + * given in configuration. The default implementation will create a path alias using the module [[id]] * and then call [[preloadComponents()]] to load components that are declared in [[preload]]. */ public function init() { $this->preloadComponents(); if ($this->controllerNamespace === null) { - if ($this instanceof Application) { - $this->controllerNamespace = 'app\\controllers'; - } else { - $class = get_class($this); - if (($pos = strrpos($class, '\\')) !== false) { - $this->controllerNamespace = substr($class, 0, $pos) . '\\controllers'; - } + $class = get_class($this); + if (($pos = strrpos($class, '\\')) !== false) { + $this->controllerNamespace = substr($class, 0, $pos) . '\\controllers'; } } } From 3e2bcfdf52c5dd96f349a993ded466d7967bac5e Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 19 Oct 2013 02:19:27 +0400 Subject: [PATCH 2/6] Refactored Module::setBasePath to remove instanceof condition --- framework/yii/base/Application.php | 12 ++++++++++++ framework/yii/base/Module.php | 3 --- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index b3803cb..55258ee 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -222,6 +222,18 @@ abstract class Application extends Module } /** + * Sets the root directory of the applicaition and the @app alias. + * This method can only be invoked at the beginning of the constructor. + * @param string $path the root directory of the application. + * @throws InvalidParamException if the directory does not exist. + */ + public function setBasePath($path) + { + parent::setBasePath($path); + Yii::setAlias('@app', $this->getBasePath()); + } + + /** * Runs the application. * This is the main entrance of an application. * @return integer the exit status (0 means normal, non-zero values mean abnormal) diff --git a/framework/yii/base/Module.php b/framework/yii/base/Module.php index 8e62d0a..a03fd83 100644 --- a/framework/yii/base/Module.php +++ b/framework/yii/base/Module.php @@ -217,9 +217,6 @@ abstract class Module extends Component $p = realpath($path); if ($p !== false && is_dir($p)) { $this->_basePath = $p; - if ($this instanceof Application) { - Yii::setAlias('@app', $p); - } } else { throw new InvalidParamException("The directory does not exist: $path"); } From 54e1abaa7b28924c01c75e7ee5859246696cb179 Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 19 Oct 2013 02:22:05 +0400 Subject: [PATCH 3/6] Better Module::init refactoring --- framework/yii/base/Application.php | 19 +++++-------------- framework/yii/base/Module.php | 12 ++++++------ 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index 55258ee..11abfd6 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -140,6 +140,11 @@ abstract class Application extends Module $this->registerCoreComponents(); Component::__construct($config); + + $this->preloadComponents(); + if ($this->controllerNamespace === null) { + $this->controllerNamespace = 'app\\controllers'; + } } /** @@ -172,20 +177,6 @@ abstract class Application extends Module } /** - * Initializes the application. - * This method is called after the application is created and initialized with property values - * given in configuration. - */ - public function init() - { - $this->preloadComponents(); - if ($this->controllerNamespace === null) { - $this->controllerNamespace = 'app\\controllers'; - } - } - - - /** * Loads components that are declared in [[preload]]. * @throws InvalidConfigException if a component or module to be preloaded is unknown */ diff --git a/framework/yii/base/Module.php b/framework/yii/base/Module.php index a03fd83..a7aa387 100644 --- a/framework/yii/base/Module.php +++ b/framework/yii/base/Module.php @@ -130,6 +130,12 @@ abstract class Module extends Component $this->id = $id; $this->module = $parent; parent::__construct($config); + if ($this->controllerNamespace === null) { + $class = get_class($this); + if (($pos = strrpos($class, '\\')) !== false) { + $this->controllerNamespace = substr($class, 0, $pos) . '\\controllers'; + } + } } /** @@ -173,12 +179,6 @@ abstract class Module extends Component public function init() { $this->preloadComponents(); - if ($this->controllerNamespace === null) { - $class = get_class($this); - if (($pos = strrpos($class, '\\')) !== false) { - $this->controllerNamespace = substr($class, 0, $pos) . '\\controllers'; - } - } } /** From 8583de701df813f72ece5ee4d52eb3edcf845c2d Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 19 Oct 2013 02:23:05 +0400 Subject: [PATCH 4/6] Removed unnecessary call --- framework/yii/base/Application.php | 1 - 1 file changed, 1 deletion(-) diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index 11abfd6..5de7969 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -141,7 +141,6 @@ abstract class Application extends Module Component::__construct($config); - $this->preloadComponents(); if ($this->controllerNamespace === null) { $this->controllerNamespace = 'app\\controllers'; } From 9434cc8f40b6815ce39c6e51b91fd701663bdc6f Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 19 Oct 2013 13:30:31 +0400 Subject: [PATCH 5/6] Moved Application::$controllerNamespace init into variable declaration, reordered Module::__construct. --- framework/yii/base/Application.php | 11 +++++++---- framework/yii/base/Module.php | 3 +-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/framework/yii/base/Application.php b/framework/yii/base/Application.php index 5de7969..c871548 100644 --- a/framework/yii/base/Application.php +++ b/framework/yii/base/Application.php @@ -54,6 +54,13 @@ abstract class Application extends Module * @event ActionEvent an event raised after executing a controller action. */ const EVENT_AFTER_ACTION = 'afterAction'; + + /** + * @var string the namespace that controller classes are in. If not set, + * it will use the "app\controllers" namespace. + */ + public $controllerNamespace = 'app\\controllers'; + /** * @var string the application name. */ @@ -140,10 +147,6 @@ abstract class Application extends Module $this->registerCoreComponents(); Component::__construct($config); - - if ($this->controllerNamespace === null) { - $this->controllerNamespace = 'app\\controllers'; - } } /** diff --git a/framework/yii/base/Module.php b/framework/yii/base/Module.php index 0e5c592..996d0f2 100644 --- a/framework/yii/base/Module.php +++ b/framework/yii/base/Module.php @@ -83,7 +83,6 @@ abstract class Module extends Component * it will use the "controllers" sub-namespace under the namespace of this module. * For example, if the namespace of this module is "foo\bar", then the default * controller namespace would be "foo\bar\controllers". - * If the module is an application, it will default to "app\controllers". */ public $controllerNamespace; /** @@ -127,9 +126,9 @@ abstract class Module extends Component */ public function __construct($id, $parent = null, $config = []) { + parent::__construct($config); $this->id = $id; $this->module = $parent; - parent::__construct($config); if ($this->controllerNamespace === null) { $class = get_class($this); if (($pos = strrpos($class, '\\')) !== false) { From 8e1199b0f6ee5371c1dbc39037b088890731ca8b Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Sat, 19 Oct 2013 23:24:51 +0400 Subject: [PATCH 6/6] Moved setting controllerNamespace back to init --- framework/yii/base/Module.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/framework/yii/base/Module.php b/framework/yii/base/Module.php index 996d0f2..1fdf5ee 100644 --- a/framework/yii/base/Module.php +++ b/framework/yii/base/Module.php @@ -126,15 +126,9 @@ abstract class Module extends Component */ public function __construct($id, $parent = null, $config = []) { - parent::__construct($config); $this->id = $id; $this->module = $parent; - if ($this->controllerNamespace === null) { - $class = get_class($this); - if (($pos = strrpos($class, '\\')) !== false) { - $this->controllerNamespace = substr($class, 0, $pos) . '\\controllers'; - } - } + parent::__construct($config); } /** @@ -178,6 +172,12 @@ abstract class Module extends Component public function init() { $this->preloadComponents(); + if ($this->controllerNamespace === null) { + $class = get_class($this); + if (($pos = strrpos($class, '\\')) !== false) { + $this->controllerNamespace = substr($class, 0, $pos) . '\\controllers'; + } + } } /**