標籤:
今天來看一下BaseYii.php
<?php/** * @link http://www.yiiframework.com/ * @copyright Copyright (c) 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */namespace yii;use yii\base\InvalidConfigException;use yii\base\InvalidParamException;use yii\base\UnknownClassException;use yii\log\Logger;use yii\di\Container;/** * Gets the application start timestamp. * 定義項目開始的時間 */defined(‘YII_BEGIN_TIME‘) or define(‘YII_BEGIN_TIME‘, microtime(true));/** * This constant defines the framework installation directory. * 定義 Yii2 項目的檔案地址 */defined(‘YII2_PATH‘) or define(‘YII2_PATH‘, __DIR__);/** * This constant defines whether the application should be in debug mode or not. Defaults to false. * 定義是否開啟 Yii 的 Debug */defined(‘YII_DEBUG‘) or define(‘YII_DEBUG‘, false);/** * This constant defines in which environment the application is running. Defaults to ‘prod‘, meaning production environment. * You may define this constant in the bootstrap script. The value could be ‘prod‘ (production), ‘dev‘ (development), ‘test‘, ‘staging‘, etc. * 定義 Yii 的環境, 其值可以是 ‘prod‘ (production), ‘dev‘ (development), ‘test‘, ‘staging‘ 等等 */defined(‘YII_ENV‘) or define(‘YII_ENV‘, ‘prod‘);/** * Whether the the application is running in production environment * 項目是否運行在 production 環境上 */defined(‘YII_ENV_PROD‘) or define(‘YII_ENV_PROD‘, YII_ENV === ‘prod‘);/** * Whether the the application is running in development environment * 項目是否運行在 development 環境上 */defined(‘YII_ENV_DEV‘) or define(‘YII_ENV_DEV‘, YII_ENV === ‘dev‘);/** * Whether the the application is running in testing environment * 項目是否運行在 testing 環境上 */defined(‘YII_ENV_TEST‘) or define(‘YII_ENV_TEST‘, YII_ENV === ‘test‘);/** * This constant defines whether error handling should be enabled. Defaults to true. * 定義是否開啟 error handler */defined(‘YII_ENABLE_ERROR_HANDLER‘) or define(‘YII_ENABLE_ERROR_HANDLER‘, true);/** * BaseYii is the core helper class for the Yii framework. * * Do not use BaseYii directly. Instead, use its child class [[\Yii]] which you can replace to * customize methods of BaseYii. * * @author Qiang Xue <[email protected]> * @since 2.0 */ class BaseYii{ /** * @var array class map used by the Yii autoloading mechanism. * The array keys are the class names (without leading backslashes), and the array values * are the corresponding class file paths (or path aliases). This property mainly affects * how [[autoload()]] works. * @see autoload() */ public static $classMap = []; /** * @var \yii\console\Application|\yii\web\Application the application instance * Yii 的 application 的執行個體, Yii 的 components 的使用都是通過這個執行個體使用的 */ public static $app; /** * @var array registered path aliases * @see getAlias() * @see setAlias() * Yii 的路徑別名的 Map, 預設 @yii 指向目前的目錄 */ public static $aliases = [‘@yii‘ => __DIR__]; /** * Returns a string representing the current version of the Yii framework. * 返回一個字串代表的目前的版本Yii架構。 * @return string the version of Yii framework */ public static function getVersion() { return ‘2.0.3‘; } /** * Translates a path alias into an actual path. * 將別名轉化為真實的路徑 * */ public static function getAlias($alias, $throwException = true) { /** * strncmp — 二進位安全比較字串開頭的若干個字元 * int strncmp ( string $str1 , string $str2 , int $len ) * 如果 $alias 不是以 ‘@‘ 開頭的,就不是一個 Yii 的別名 */ if (strncmp($alias, ‘@‘, 1)) { // not an alias return $alias; } // 擷取 / 在 $alias 中首次出現的位置 $pos = strpos($alias, ‘/‘); // 如果 / 不存在,$root 就是整個 $alias,否則就是 $alias 中 / 前的內容 $root = $pos === false ? $alias : substr($alias, 0, $pos); // 如果存在 $root 的別名 if (isset(static::$aliases[$root])) { if (is_string(static::$aliases[$root])) { // 如果 $root 對應的別名是一個字串,之直接返回 $aliases[$root] 或者 $aliases[$root] . substr($alias, $pos) // 當 $root 就是 $alias 返回 $aliases[$root], 否則就在拼接上 $alias 除去 $root 後,剩下的字串 return $pos === false ? static::$aliases[$root] : static::$aliases[$root] . substr($alias, $pos); } else { // 否則,要遍曆整個 $aliases[$root] 數組,找到 $name 與 $alias 相同的值,返回 $path . substr($alias, strlen($name)) // 其實是返回了 $path 拼接上 $alias 除去 $root 後,剩下的字串 foreach (static::$aliases[$root] as $name => $path) { if (strpos($alias . ‘/‘, $name . ‘/‘) === 0) { return $path . substr($alias, strlen($name)); } } } } if ($throwException) { throw new InvalidParamException("Invalid path alias: $alias");//拋出提示 } else { return false; } }
yii2架構隨筆21