Commit ee261d38 by Qiang Xue

Merge pull request #1020 from yiisoft/module-refactoring

Module & Application refactoring
parents 07c65385 8e1199b0
......@@ -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.
*/
......@@ -221,6 +228,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)
......
......@@ -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;
/**
......@@ -167,23 +166,19 @@ 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';
}
}
}
}
/**
* Returns an ID that uniquely identifies this module among all modules within the current application.
......@@ -221,9 +216,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");
}
......
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