Browse Source

Fixes #744: simplified Yii autoloader.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
f576add9e9
  1. 33
      framework/yii/YiiBase.php
  2. 25
      framework/yii/base/UnknownClassException.php
  3. 1
      framework/yii/classes.php

33
framework/yii/YiiBase.php

@ -9,7 +9,6 @@ namespace yii;
use yii\base\Exception;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\base\UnknownClassException;
use yii\log\Logger;
/**
@ -69,11 +68,6 @@ class YiiBase
*/
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
*/
public static $app;
@ -338,12 +332,8 @@ class YiiBase
* 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
* (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 \
* @return boolean whether the class has been loaded successfully
* @throws UnknownClassException if the class does not exist in the class file
* @param string $className the fully qualified class name without a leading backslash "\"
*/
public static function autoload($className)
{
@ -352,6 +342,7 @@ class YiiBase
if ($classFile[0] === '@') {
$classFile = static::getAlias($classFile);
}
include($classFile);
} else {
// follow PSR-0 to determine the class file
if (($pos = strrpos($className, '\\')) !== false) {
@ -367,27 +358,9 @@ class YiiBase
$fullPath = static::getAlias('@' . $path, false);
if ($fullPath !== false && is_file($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");
}
}

25
framework/yii/base/UnknownClassException.php

@ -1,25 +0,0 @@
<?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');
}
}

1
framework/yii/classes.php

@ -37,7 +37,6 @@ return array(
'yii\base\Request' => YII_PATH . '/base/Request.php',
'yii\base\Response' => YII_PATH . '/base/Response.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\UnknownPropertyException' => YII_PATH . '/base/UnknownPropertyException.php',
'yii\base\UserException' => YII_PATH . '/base/UserException.php',

Loading…
Cancel
Save