码迷,mamicode.com
首页 > 其他好文 > 详细

yii2框架随笔22

时间:2016-05-04 01:08:18      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

今天继续来看BaseYii.php

<?php
  /**
     * Returns the root alias part of a given alias.
     * 返回根别名的一部分,一个给定的别名。
     * A root alias is an alias that has been registered via [[setAlias()]] previously.
     * 根别名是已经注册别名通过[[setAlias()]]。
     * If a given alias matches multiple root aliases, the longest one will be returned.
     * 如果一个给定的别名匹配多个根别名,将返回最长的一个。
     * @param string $alias the alias
     * @return string|boolean the root alias, or false if no root alias is found
     */
    public static function getRootAlias($alias)
    {
        // 获取 / 在 $alias 中首次出现的位置
        $pos = strpos($alias, /);
        // 如果 / 不存在,$root 就是整个 $alias,否则就是 $alias 中 / 前的内容
        $root = $pos === false ? $alias : substr($alias, 0, $pos);
        if (isset(static::$aliases[$root])) {
            // 如果 $root 对应的别名存在
            if (is_string(static::$aliases[$root])) {
                // 如果 $root 对应的别名是一个字符串,之直接返回 $root
                return $root;
            } else {
                // 否则,要遍历整个 $aliases[$root] 数组,找到 $name 与 $alias 相同的值,返回 $name
                foreach (static::$aliases[$root] as $name => $path) {
                    if (strpos($alias . /, $name . /) === 0) {
                        return $name;
                    }
                }
            }
        }
        return false;
    }
    /**
     * Registers a path alias.
     *
     * 用一个真实的路径注册一个别名
     *
     * A path alias is a short name representing a long path (a file path, a URL, etc.)
     * 路径别名是一个短名称代表长路径(文件路径、网址等)。
     * For example, we use ‘@yii‘ as the alias of the path to the Yii framework directory.
     * 例如,我们使用“@yii”作为Yii框架目录的路径别名。
     *
     * A path alias must start with the character ‘@‘ so that it can be easily differentiated
     * from non-alias paths.
     * 路径别名必须伴随字符‘@’,所以这样是很容易区分的。
     * Note that this method does not check if the given path exists or not. All it does is
     * to associate the alias with the path.
     * 注意,这个方法不检查是否存在给定的路径。它所做的是把别名和路径联系起来。
     * Any trailing ‘/‘ and ‘\‘ characters in the given path will be trimmed.
     * 任何落后于‘ / ‘和‘ \ ‘字符在给定的路径将会被去掉。
     * @param string $alias the alias name (e.g. "@yii"). It must start with a ‘@‘ character.
     * It may contain the forward slash ‘/‘ which serves as boundary character when performing
     * alias translation by [[getAlias()]].
     * @param string $path the path corresponding to the alias. If this is null, the alias will
     * be removed. Trailing ‘/‘ and ‘\‘ characters will be trimmed. This can be
     *
     * - a directory or a file path (e.g. `/tmp`, `/tmp/main.txt`)
     * - a URL (e.g. `http://www.yiiframework.com`)
     * - a path alias (e.g. `@yii/base`). In this case, the path alias will be converted into the
     *   actual path first by calling [[getAlias()]].
     *
     * @throws InvalidParamException if $path is an invalid alias.
     * @see getAlias()
     */
    public static function setAlias($alias, $path)
    {
        if (strncmp($alias, @, 1)) {
            // 如果不是以 @ 开头,就将 @ 拼到开头
            $alias = @ . $alias;
        }
        // 获取 / 在 $alias 中首次出现的位置
        $pos = strpos($alias, /);
        // 如果 / 不存在,$root 就是整个 $alias,否则就是 $alias 中 / 前的内容
        $root = $pos === false ? $alias : substr($alias, 0, $pos);
        if ($path !== null) {
            // 如果 $path 以 @ 开头,使用 getAlias 去获取路径,否则,就去除掉最右边的 /
            $path = strncmp($path, @, 1) ? rtrim($path, \\/) : static::getAlias($path);
            if (!isset(static::$aliases[$root])) {
                // 如果不存在这个 $root 的别名
                if ($pos === false) {
                    // 没有 /,就将 $path 直接赋值以为 $root 别名对应的路径
                    static::$aliases[$root] = $path;
                } else {
                    // 否则,就将 $path 直接赋值为 $root 下的 $alias 的路径
                    static::$aliases[$root] = [$alias => $path];
                }
            } elseif (is_string(static::$aliases[$root])) {
                // 如果存在,而且是个string类型
                if ($pos === false) {
                    // 没有 /,意味着 $alias 就是 $root,直接覆盖即可
                    static::$aliases[$root] = $path;
                } else {
                    // 否则,就合并到一起
                    static::$aliases[$root] = [
                        $alias => $path,
                        $root => static::$aliases[$root],
                    ];
                }
            } else {
                // 这种,正常是个 array 类型
                // 直接添加进去即可
                static::$aliases[$root][$alias] = $path;
                // krsort — 对数组按照键名逆向排序
                // 可以做到优先匹配长的别名
                krsort(static::$aliases[$root]);
            }
        } elseif (isset(static::$aliases[$root])) {
            // $path 为空且对应的别名有值存在,就是要移除相应的别名
            if (is_array(static::$aliases[$root])) {
                // 如果 $root 的别名对应一个 array,就只移除掉对应的别名即可
                unset(static::$aliases[$root][$alias]);
            } elseif ($pos === false) {
                // 如果 $root 的别名对应不是一个 array 而且 $root 就是 $alias,就移除这个 $root 的别名
                unset(static::$aliases[$root]);
            }
        }
    }

 

yii2框架随笔22

标签:

原文地址:http://www.cnblogs.com/taokai/p/5456985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!