From e123428c948f5860ef9b66063b5adc5031d0d2fd Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 8 Jan 2014 09:11:26 -0500 Subject: [PATCH] Changed Yii autoloader to support loading PSR-4 classes only (i.e. PEAR-styled classes not supported anymore) --- framework/CHANGELOG.md | 1 + framework/yii/BaseYii.php | 29 +++++++---------------------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 1eab4f3..6c66691 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -71,6 +71,7 @@ Yii Framework 2 Change Log - Chg: Added `yii\widgets\InputWidget::options` (qiangxue) - Chg: Changed the signature of `urlCreator` and button creators for `yii\gridview\ActionColumn` (qiangxue) - Chg: Updated HTMLPurified dependency to `4.6.*`. +- Chg: Changed Yii autoloader to support loading PSR-4 classes only (i.e. PEAR-styled classes not supported anymore) (qiangxue) - New #66: [Auth client library](https://github.com/yiisoft/yii2-authclient) OpenId, OAuth1, OAuth2 clients (klimov-paul) - New #1393: [Codeception testing framework integration](https://github.com/yiisoft/yii2-codeception) (Ragazzo) - New #1438: [MongoDB integration](https://github.com/yiisoft/yii2-mongodb) ActiveRecord and Query (klimov-paul) diff --git a/framework/yii/BaseYii.php b/framework/yii/BaseYii.php index b8af607..3a4e645 100644 --- a/framework/yii/BaseYii.php +++ b/framework/yii/BaseYii.php @@ -266,12 +266,9 @@ class BaseYii * 2. If the class is namespaced (e.g. `yii\base\Component`), it will attempt * to include the file associated with the corresponding path alias * (e.g. `@yii/base/Component.php`); - * 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`); * - * 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. + * This autoloader allows loading classes that follow the [PSR-4 standard](http://www.php-fig.org/psr/psr-4/) + * and have its top-level namespace defined as path aliases. * * @param string $className the fully qualified class name without a leading backslash "\" * @throws UnknownClassException if the class does not exist in the class file @@ -283,25 +280,13 @@ class BaseYii if ($classFile[0] === '@') { $classFile = static::getAlias($classFile); } - } else { - // follow PSR-0 to determine the class file - if (($pos = strrpos($className, '\\')) !== false) { - // namespaced class, e.g. yii\base\Component - $path = str_replace('\\', '/', substr($className, 0, $pos + 1)) - . str_replace('_', '/', substr($className, $pos + 1)) . '.php'; - } else { - $path = str_replace('_', '/', $className) . '.php'; - } - - // try loading via path alias - if (strpos($path, '/') === false) { + } elseif (strpos($className, '\\') !== false) { + $classFile = static::getAlias('@' . str_replace('\\', '/', $className) . '.php', false); + if ($classFile === false || !is_file($classFile)) { return; - } else { - $classFile = static::getAlias('@' . $path, false); - if ($classFile === false || !is_file($classFile)) { - return; - } } + } else { + return; } include($classFile);