From bb2274eae22c590f5294bf613f0eaa17b0d2d2c2 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sun, 18 Aug 2013 19:33:33 +0200 Subject: [PATCH] re-added Exception about wrong class name to autoloader helps reducing weird error messages. --- framework/yii/YiiBase.php | 21 +++++++++++++++++---- framework/yii/base/UnknownClassException.php | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 framework/yii/base/UnknownClassException.php diff --git a/framework/yii/YiiBase.php b/framework/yii/YiiBase.php index 2834d7e..a5a3833 100644 --- a/framework/yii/YiiBase.php +++ b/framework/yii/YiiBase.php @@ -9,6 +9,7 @@ namespace yii; use yii\base\Exception; use yii\base\InvalidConfigException; use yii\base\InvalidParamException; +use yii\base\UnknownClassException; use yii\log\Logger; /** @@ -333,7 +334,11 @@ class YiiBase * it will attempt to include the file associated with the corresponding path alias * (e.g. `@PHPUnit/Framework/TestCase.php`); * + * This autoloader allows loading classes that follow the [PSR-0 standard](http://www.php-fig.org/psr/0/). + * Therefor a path alias has to be defined for each top-level namespace. + * * @param string $className the fully qualified class name without a leading backslash "\" + * @throws UnknownClassException if the class does not exist in the class file */ public static function autoload($className) { @@ -342,7 +347,6 @@ 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) { @@ -354,13 +358,22 @@ class YiiBase } // try loading via path alias - if (strpos($path, '/') !== false) { + if (strpos($path, '/') === false) { + return; + } else { $classFile = static::getAlias('@' . $path, false); - if ($classFile !== false && is_file($classFile)) { - include($classFile); + if ($classFile === false || !is_file($classFile)) { + return; } } } + + include($classFile); + + if (!class_exists($className, false) && !interface_exists($className, false) && + (!function_exists('trait_exists') || !trait_exists($className, false))) { + throw new UnknownClassException("Unable to find '$className' in file: $classFile"); + } } /** diff --git a/framework/yii/base/UnknownClassException.php b/framework/yii/base/UnknownClassException.php new file mode 100644 index 0000000..8ccd09c --- /dev/null +++ b/framework/yii/base/UnknownClassException.php @@ -0,0 +1,25 @@ + + * @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'); + } +}