Browse Source

m

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
a5b3932a36
  1. 22
      framework/YiiBase.php
  2. 7
      framework/base/Event.php
  3. 101
      framework/base/Module.php

22
framework/YiiBase.php

@ -189,10 +189,12 @@ class YiiBase
/** /**
* Translates a path alias into an actual path. * Translates a path alias into an actual path.
*
* The path alias can be either a root alias registered via [[setAlias]] or an * 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`). * 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 * In the latter case, the root alias will be replaced by the corresponding registered path
* and the remaining part will be appended to it. * and the remaining part will be appended to it.
*
* Note, this method does not ensure the existence of the resulting path. * Note, this method does not ensure the existence of the resulting path.
* @param string $alias alias * @param string $alias alias
* @return mixed path corresponding to the alias, false if the root alias is not previously registered. * @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. * Registers a path alias.
*
* A path alias is a short name representing a path (a file path, a URL, etc.) * 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'). * A path alias must start with '@' (e.g. '@yii').
*
* Note that this method neither checks the existence of the path nor normalizes the path. * 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 $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 * @param string $path the path corresponding to the alias. This can be
* path alias will be removed. The path can be a file path (e.g. `/tmp`) or a URL (e.g. `http://www.yiiframework.com`). *
* - 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 * @see getAlias
*/ */
public static function setAlias($alias, $path) public static function setAlias($alias, $path)
@ -227,9 +237,15 @@ class YiiBase
if ($path === null) { if ($path === null) {
unset(self::$aliases[$alias]); unset(self::$aliases[$alias]);
} }
else { elseif ($path[0] !== '@') {
self::$aliases[$alias] = rtrim($path, '\\/'); self::$aliases[$alias] = rtrim($path, '\\/');
} }
elseif (($p = self::getAlias($path)) !== false) {
self::$aliases[$alias] = $p;
}
else {
throw new \yii\base\Exception('Invalid path: ' . $path);
}
} }
/** /**

7
framework/base/Event.php

@ -16,7 +16,8 @@ namespace yii\base;
* The [[sender]] property describes who raises the event. * The [[sender]] property describes who raises the event.
* And the [[handled]] property indicates if the event is handled. * And the [[handled]] property indicates if the event is handled.
* If an event handler sets [[handled]] to be true, the rest of the * 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> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @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. * When a handler sets this to be true, the rest of the uninvoked event handlers will be canceled.
*/ */
public $handled = false; public $handled = false;
/**
* @var mixed extra parameters associated with the event.
*/
public $params;
/** /**
* Constructor. * Constructor.

101
framework/base/Module.php

@ -21,17 +21,24 @@ namespace yii\base;
abstract class Module extends Component 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. * @var array the IDs of the application components that should be preloaded.
*/ */
public $preload = array(); public $preload = array();
/** /**
* @var array the behaviors that should be attached to the module. * @var array the behaviors that should be attached to the module.
* The behaviors will be attached to the module when {@link init} is called. * The behaviors will be attached to the module when [[init]] is called.
* Please refer to {@link CModel::behaviors} on how to specify the value of this property. * Please refer to [[Model::behaviors]] on how to specify the value of this property.
*/ */
public $behaviors = array(); public $behaviors = array();
private $_id;
private $_parentModule; private $_parentModule;
private $_basePath; private $_basePath;
private $_modulePath; 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. * Returns the root directory of the module.
* @return string the root directory of the module. Defaults to the directory containing the module class. * @return string the root directory of the module. Defaults to the directory containing the module class.
*/ */
public function getBasePath() public function getBasePath()
{ {
if ($this->_basePath === null) if ($this->_basePath === null) {
{ $class = new ReflectionClass($this);
$class = new ReflectionClass(get_class($this));
$this->_basePath = dirname($class->getFileName()); $this->_basePath = dirname($class->getFileName());
} }
return $this->_basePath; return $this->_basePath;
@ -139,74 +127,51 @@ abstract class Module extends Component
* Sets the root directory of the module. * Sets the root directory of the module.
* This method can only be invoked at the beginning of the constructor. * This method can only be invoked at the beginning of the constructor.
* @param string $path the root directory of the module. * @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) public function setBasePath($path)
{ {
if (($this->_basePath = realpath($path)) === false || !is_dir($this->_basePath)) if (($this->_basePath = realpath($path)) === false || !is_dir($this->_basePath)) {
throw new CException(Yii::t('yii', 'Base path "{path}" is not a valid directory.', throw new Exception('Invalid base path: ' . $path);
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;
} }
} }
/** /**
* Sets user-defined parameters. * Returns the directory that contains child modules.
* @param array $value user-defined parameters. This should be in name-value pairs. * @return string the directory that contains child modules. Defaults to the `modules` subdirectory under [[basePath]].
*/
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}.
*/ */
public function getModulePath() public function getModulePath()
{ {
if ($this->_modulePath !== null) if ($this->_modulePath !== null) {
return $this->_modulePath; return $this->_modulePath;
else }
else {
return $this->_modulePath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'modules'; return $this->_modulePath = $this->getBasePath() . DIRECTORY_SEPARATOR . 'modules';
}
} }
/** /**
* Sets the directory that contains the application modules. * Sets the directory that contains child modules.
* @param string $value the directory that contains the application modules. * @param string $value the directory that contains child modules.
* @throws CException if the directory is invalid * @throws Exception if the directory is invalid
*/ */
public function setModulePath($value) public function setModulePath($value)
{ {
if (($this->_modulePath = realpath($value)) === false || !is_dir($this->_modulePath)) 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.', throw new Exception('Invalid module path: ' . $value);
array('{path}' => $value))); }
} }
/** /**
* Sets the aliases that are used in the module. * Imports the specified path aliases.
* @param array $aliases list of aliases to be imported * 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) public function setImport($aliases)
{ {
foreach ($aliases as $alias) foreach ($aliases as $alias) {
Yii::import($alias); \Yii::import($alias);
}
} }
/** /**

Loading…
Cancel
Save