Commit a5b3932a by Qiang Xue

m

parent 07c86e58
......@@ -189,10 +189,12 @@ class YiiBase
/**
* Translates a path alias into an actual path.
*
* The path alias can be either a root alias registered via [[setAlias]] or an
* alias starting with a root alias (e.g. `@yii/base/Component.php`).
* In the latter case, the root alias will be replaced by the corresponding registered path
* and the remaining part will be appended to it.
*
* Note, this method does not ensure the existence of the resulting path.
* @param string $alias alias
* @return mixed path corresponding to the alias, false if the root alias is not previously registered.
......@@ -214,12 +216,20 @@ class YiiBase
/**
* Registers a path alias.
*
* A path alias is a short name representing a path (a file path, a URL, etc.)
* A path alias must start with '@' (e.g. '@yii').
*
* Note that this method neither checks the existence of the path nor normalizes the path.
* Any trailing '/' and '\' characters in the path will be trimmed.
*
* @param string $alias alias to the path. The alias must start with '@'.
* @param string $path the path corresponding to the alias. If this is null, the corresponding
* path alias will be removed. The path can be a file path (e.g. `/tmp`) or a URL (e.g. `http://www.yiiframework.com`).
* @param string $path the path corresponding to the alias. This can be
*
* - a directory or a file path (e.g. `/tmp`, `/tmp/main.txt`)
* - a URL (e.g. `http://www.yiiframework.com`)
* - a path alias (e.g. `@yii/base`). In this case, the path alias will be converted into the
* actual path first by calling [[getAlias]].
* @see getAlias
*/
public static function setAlias($alias, $path)
......@@ -227,9 +237,15 @@ class YiiBase
if ($path === null) {
unset(self::$aliases[$alias]);
}
else {
elseif ($path[0] !== '@') {
self::$aliases[$alias] = rtrim($path, '\\/');
}
elseif (($p = self::getAlias($path)) !== false) {
self::$aliases[$alias] = $p;
}
else {
throw new \yii\base\Exception('Invalid path: ' . $path);
}
}
/**
......
......@@ -16,7 +16,8 @@ namespace yii\base;
* The [[sender]] property describes who raises the event.
* And the [[handled]] property indicates if the event is handled.
* If an event handler sets [[handled]] to be true, the rest of the
* uninvoked handlers will be canceled.
* uninvoked handlers will no longer be called to handle the event.
* Additionally, an event may specify extra parameters via the [[params]] property.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
......@@ -32,6 +33,10 @@ class Event extends Component
* When a handler sets this to be true, the rest of the uninvoked event handlers will be canceled.
*/
public $handled = false;
/**
* @var mixed extra parameters associated with the event.
*/
public $params;
/**
* Constructor.
......
......@@ -21,17 +21,24 @@ namespace yii\base;
abstract class Module extends Component
{
/**
* @var string the ID of this module. This should follow the same rule as naming PHP variables.
*/
public $id;
/**
* @var array custom module parameters (name => value).
*/
public $params = array();
/**
* @var array the IDs of the application components that should be preloaded.
*/
public $preload = array();
/**
* @var array the behaviors that should be attached to the module.
* The behaviors will be attached to the module when {@link init} is called.
* Please refer to {@link CModel::behaviors} on how to specify the value of this property.
* The behaviors will be attached to the module when [[init]] is called.
* Please refer to [[Model::behaviors]] on how to specify the value of this property.
*/
public $behaviors = array();
private $_id;
private $_parentModule;
private $_basePath;
private $_modulePath;
......@@ -104,32 +111,13 @@ abstract class Module extends Component
}
/**
* Returns the module ID.
* @return string the module ID.
*/
public function getId()
{
return $this->_id;
}
/**
* Sets the module ID.
* @param string $id the module ID
*/
public function setId($id)
{
$this->_id = $id;
}
/**
* Returns the root directory of the module.
* @return string the root directory of the module. Defaults to the directory containing the module class.
*/
public function getBasePath()
{
if ($this->_basePath === null)
{
$class = new ReflectionClass(get_class($this));
if ($this->_basePath === null) {
$class = new ReflectionClass($this);
$this->_basePath = dirname($class->getFileName());
}
return $this->_basePath;
......@@ -139,74 +127,51 @@ abstract class Module extends Component
* Sets the root directory of the module.
* This method can only be invoked at the beginning of the constructor.
* @param string $path the root directory of the module.
* @throws CException if the directory does not exist.
* @throws Exception if the directory does not exist.
*/
public function setBasePath($path)
{
if (($this->_basePath = realpath($path)) === false || !is_dir($this->_basePath))
throw new CException(Yii::t('yii', 'Base path "{path}" is not a valid directory.',
array('{path}' => $path)));
}
/**
* Returns user-defined parameters.
* @return CAttributeCollection the list of user-defined parameters
*/
public function getParams()
{
if ($this->_params !== null)
return $this->_params;
else
{
$this->_params = new CAttributeCollection;
$this->_params->caseSensitive = true;
return $this->_params;
if (($this->_basePath = realpath($path)) === false || !is_dir($this->_basePath)) {
throw new Exception('Invalid base path: ' . $path);
}
}
/**
* Sets user-defined parameters.
* @param array $value user-defined parameters. This should be in name-value pairs.
*/
public function setParams($value)
{
$params = $this->getParams();
foreach ($value as $k => $v)
$params->add($k, $v);
}
/**
* Returns the directory that contains the application modules.
* @return string the directory that contains the application modules. Defaults to the 'modules' subdirectory of {@link basePath}.
* Returns the directory that contains child modules.
* @return string the directory that contains child modules. Defaults to the `modules` subdirectory under [[basePath]].
*/
public function getModulePath()
{
if ($this->_modulePath !== null)
if ($this->_modulePath !== null) {
return $this->_modulePath;
else
}
else {
return $this->_modulePath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'modules';
}
}
/**
* Sets the directory that contains the application modules.
* @param string $value the directory that contains the application modules.
* @throws CException if the directory is invalid
* Sets the directory that contains child modules.
* @param string $value the directory that contains child modules.
* @throws Exception if the directory is invalid
*/
public function setModulePath($value)
{
if (($this->_modulePath = realpath($value)) === false || !is_dir($this->_modulePath))
throw new CException(Yii::t('yii', 'The module path "{path}" is not a valid directory.',
array('{path}' => $value)));
if (($this->_modulePath = realpath($value)) === false || !is_dir($this->_modulePath)) {
throw new Exception('Invalid module path: ' . $value);
}
}
/**
* Sets the aliases that are used in the module.
* @param array $aliases list of aliases to be imported
* Imports the specified path aliases.
* This method is provided so that you can import a set of path aliases by module configuration.
* @param array $aliases list of path aliases to be imported
*/
public function setImport($aliases)
{
foreach ($aliases as $alias)
Yii::import($alias);
foreach ($aliases as $alias) {
\Yii::import($alias);
}
}
/**
......
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