Commit bcc83320 by Qiang Xue

simplified Yii::import().

parent 6ae715ff
...@@ -107,73 +107,36 @@ class YiiBase ...@@ -107,73 +107,36 @@ class YiiBase
} }
/** /**
* Imports a class or a directory. * Imports a class by its alias.
* *
* Importing a class is like including the corresponding class file. * This method is provided to support autoloading of non-namespaced classes.
* The main difference is that importing a class is much lighter because it only * Such a class can be specified in terms of an alias. For example, the alias `@old/code/Sample`
* includes the class file when the class is referenced in the code the first time. * may represent the `Sample` class under the directory `@old/code` (a path alias).
* *
* Importing a directory will add the directory to the front of the [[classPath]] array. * By importing a class, the class is put in an internal storage such that when
* When [[autoload()]] is loading an unknown class, it will search in the directories * the class is used for the first time, the class autoloader will be able to
* specified in [[classPath]] to find the corresponding class file to include. * find the corresponding class file and include it. For this reason, this method
* For this reason, if multiple directories are imported, the directories imported later * is much lighter than `include()`.
* will take precedence in class file searching.
* *
* The same class or directory can be imported multiple times. Only the first importing * You may import the same class multiple times. Only the first importing will count.
* will count. Importing a directory does not import any of its subdirectories.
* *
* To import a class or a directory, one can use either path alias or class name (can be namespaced): * @param string $alias the class to be imported. This may be either a class alias or a fully-qualified class name.
* * If the latter, it will be returned back without change.
* - `@app/components/GoogleMap`: importing the `GoogleMap` class with a path alias; * @return string the actual class name that `$alias` refers to
* - `@app/components/*`: importing the whole `components` directory with a path alias; * @throws Exception if the alias is invalid
* - `GoogleMap`: importing the `GoogleMap` class with a class name. [[autoload()]] will be used
* when this class is used for the first time.
*
* @param string $alias path alias or a simple class name to be imported
* @param boolean $forceInclude whether to include the class file immediately. If false, the class file
* will be included only when the class is being used. This parameter is used only when
* the path alias refers to a class.
* @return string the class name or the directory that this alias refers to
* @throws Exception if the path alias is invalid
*/ */
public static function import($alias, $forceInclude = false) public static function import($alias)
{ {
if (isset(self::$_imported[$alias])) { if (strncmp($alias, '@', 1)) {
return self::$_imported[$alias];
}
if ($alias[0] !== '@') {
// a simple class name
if (class_exists($alias, false) || interface_exists($alias, false)) {
return self::$_imported[$alias] = $alias;
}
if ($forceInclude && static::autoload($alias)) {
self::$_imported[$alias] = $alias;
}
return $alias; return $alias;
} } else {
$alias = static::getAlias($alias);
$className = basename($alias); if (!isset(self::$_imported[$alias])) {
$isClass = $className !== '*'; $className = basename($alias);
if ($isClass && (class_exists($className, false) || interface_exists($className, false))) {
return self::$_imported[$alias] = $className;
}
$path = static::getAlias(dirname($alias));
if ($isClass) {
if ($forceInclude) {
require($path . "/$className.php");
self::$_imported[$alias] = $className; self::$_imported[$alias] = $className;
} else { self::$classMap[$className] = $alias . '.php';
self::$classMap[$className] = $path . DIRECTORY_SEPARATOR . "$className.php";
} }
return $className; return self::$_imported[$alias];
} else {
// a directory
array_unshift(self::$classPath, $path);
return self::$_imported[$alias] = $path;
} }
} }
...@@ -357,7 +320,7 @@ class YiiBase ...@@ -357,7 +320,7 @@ class YiiBase
} }
if (!class_exists($class, false)) { if (!class_exists($class, false)) {
$class = static::import($class, true); $class = static::import($class);
} }
$class = ltrim($class, '\\'); $class = ltrim($class, '\\');
......
...@@ -286,19 +286,6 @@ abstract class Module extends Component ...@@ -286,19 +286,6 @@ abstract class Module extends Component
} }
/** /**
* Imports the specified path aliases.
* This method is provided so that you can import a set of path aliases when configuring a module.
* The path aliases will be imported by calling [[Yii::import()]].
* @param array $aliases list of path aliases to be imported
*/
public function setImport($aliases)
{
foreach ($aliases as $alias) {
Yii::import($alias);
}
}
/**
* Defines path aliases. * Defines path aliases.
* This method calls [[Yii::setAlias()]] to register the path aliases. * This method calls [[Yii::setAlias()]] to register the path aliases.
* This method is provided so that you can define path aliases when configuring a module. * This method is provided so that you can define path aliases when configuring a module.
......
...@@ -60,7 +60,7 @@ class UniqueValidator extends Validator ...@@ -60,7 +60,7 @@ class UniqueValidator extends Validator
} }
/** @var $className \yii\db\ActiveRecord */ /** @var $className \yii\db\ActiveRecord */
$className = $this->className === null ? get_class($object) : \Yii::import($this->className); $className = $this->className === null ? get_class($object) : Yii::import($this->className);
$attributeName = $this->attributeName === null ? $attribute : $this->attributeName; $attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
$table = $className::getTableSchema(); $table = $className::getTableSchema();
......
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