• yii2框架随笔21


    今天来看一下BaseYii.php

    <?php
    /**
     * @link http://www.yiiframework.com/
     * @copyright Copyright (c) 2008 Yii Software LLC
     * @license http://www.yiiframework.com/license/
     */
    namespace yii;
    use yiibaseInvalidConfigException;
    use yiibaseInvalidParamException;
    use yiibaseUnknownClassException;
    use yiilogLogger;
    use yiidiContainer;
    /**
     * 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 <qiang.xue@gmail.com>
     * @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 yiiconsoleApplication|yiiwebApplication 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;
            }
        }
  • 相关阅读:
    springboot整合shiro进行权限管理
    多线程基础(一)
    面试杂谈(一)
    Spring循环依赖问题
    记一次电话面试的题目
    Jvm垃圾回收器(终结篇)
    Jvm垃圾回收器(算法篇)
    Jvm垃圾回收器(基础篇)
    Java内存模型探秘
    Jvm类的加载机制
  • 原文地址:https://www.cnblogs.com/taokai/p/5452548.html
Copyright © 2020-2023  润新知