Commit f576add9 by Qiang Xue

Fixes #744: simplified Yii autoloader.

parent ce4f37a1
...@@ -9,7 +9,6 @@ namespace yii; ...@@ -9,7 +9,6 @@ namespace yii;
use yii\base\Exception; use yii\base\Exception;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\base\InvalidParamException; use yii\base\InvalidParamException;
use yii\base\UnknownClassException;
use yii\log\Logger; use yii\log\Logger;
/** /**
...@@ -69,11 +68,6 @@ class YiiBase ...@@ -69,11 +68,6 @@ class YiiBase
*/ */
public static $classMap = array(); public static $classMap = array();
/** /**
* @var boolean whether to search PHP include_path when autoloading unknown classes.
* You may want to turn this off if you are also using autoloaders from other libraries.
*/
public static $enableIncludePath = false;
/**
* @var \yii\console\Application|\yii\web\Application the application instance * @var \yii\console\Application|\yii\web\Application the application instance
*/ */
public static $app; public static $app;
...@@ -338,12 +332,8 @@ class YiiBase ...@@ -338,12 +332,8 @@ class YiiBase
* 3. If the class is named in PEAR style (e.g. `PHPUnit_Framework_TestCase`), * 3. If the class is named in PEAR style (e.g. `PHPUnit_Framework_TestCase`),
* it will attempt to include the file associated with the corresponding path alias * it will attempt to include the file associated with the corresponding path alias
* (e.g. `@PHPUnit/Framework/TestCase.php`); * (e.g. `@PHPUnit/Framework/TestCase.php`);
* 4. Search PHP include_path for the actual class file if [[enableIncludePath]] is true;
* 5. Return false so that other autoloaders have chance to include the class file.
* *
* @param string $className the fully qualified class name without leading \ * @param string $className the fully qualified class name without a leading backslash "\"
* @return boolean whether the class has been loaded successfully
* @throws UnknownClassException if the class does not exist in the class file
*/ */
public static function autoload($className) public static function autoload($className)
{ {
...@@ -352,6 +342,7 @@ class YiiBase ...@@ -352,6 +342,7 @@ class YiiBase
if ($classFile[0] === '@') { if ($classFile[0] === '@') {
$classFile = static::getAlias($classFile); $classFile = static::getAlias($classFile);
} }
include($classFile);
} else { } else {
// follow PSR-0 to determine the class file // follow PSR-0 to determine the class file
if (($pos = strrpos($className, '\\')) !== false) { if (($pos = strrpos($className, '\\')) !== false) {
...@@ -367,27 +358,9 @@ class YiiBase ...@@ -367,27 +358,9 @@ class YiiBase
$fullPath = static::getAlias('@' . $path, false); $fullPath = static::getAlias('@' . $path, false);
if ($fullPath !== false && is_file($fullPath)) { if ($fullPath !== false && is_file($fullPath)) {
$classFile = $fullPath; $classFile = $fullPath;
include($classFile);
} }
} }
// search include_path
if (!isset($classFile) && self::$enableIncludePath && ($fullPath = stream_resolve_include_path($path)) !== false) {
$classFile = $fullPath;
}
if (!isset($classFile)) {
// return false to let other autoloaders to try loading the class
return false;
}
}
include($classFile);
if (class_exists($className, false) || interface_exists($className, false) ||
function_exists('trait_exists') && trait_exists($className, false)) {
return true;
} else {
throw new UnknownClassException("Unable to find '$className' in file: $classFile");
} }
} }
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* UnknownClassException represents an exception caused by accessing an unknown class.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class UnknownClassException extends Exception
{
/**
* @return string the user-friendly name of this exception
*/
public function getName()
{
return \Yii::t('yii', 'Unknown Class');
}
}
...@@ -37,7 +37,6 @@ return array( ...@@ -37,7 +37,6 @@ return array(
'yii\base\Request' => YII_PATH . '/base/Request.php', 'yii\base\Request' => YII_PATH . '/base/Request.php',
'yii\base\Response' => YII_PATH . '/base/Response.php', 'yii\base\Response' => YII_PATH . '/base/Response.php',
'yii\base\Theme' => YII_PATH . '/base/Theme.php', 'yii\base\Theme' => YII_PATH . '/base/Theme.php',
'yii\base\UnknownClassException' => YII_PATH . '/base/UnknownClassException.php',
'yii\base\UnknownMethodException' => YII_PATH . '/base/UnknownMethodException.php', 'yii\base\UnknownMethodException' => YII_PATH . '/base/UnknownMethodException.php',
'yii\base\UnknownPropertyException' => YII_PATH . '/base/UnknownPropertyException.php', 'yii\base\UnknownPropertyException' => YII_PATH . '/base/UnknownPropertyException.php',
'yii\base\UserException' => YII_PATH . '/base/UserException.php', 'yii\base\UserException' => YII_PATH . '/base/UserException.php',
......
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