/** * 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 '@rock' as the alias of the path to the Rock framework directory. * * 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. * * * @param string $alias the alias name (e.g. "@rock"). It must start with a '@' character. * It may contain the forward slash '/' which serves as boundary character when performing * alias translation by {@see \rock\base\Alias::getAlias()}. * @param string $path the path corresponding to the alias. 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.site.com`) * - a path alias (e.g. `@rock/base`). In this case, the path alias will be converted into the * actual path first by calling {@see \rock\base\Alias::getAlias()}. * * @param bool $trailingTrim any trailing '/' and '\' characters in the given path/url will be trimmed. * @throws \Exception if $path is an invalid alias. * @see getAlias() */ public static function setAlias($alias, $path, $trailingTrim = true) { if (strncmp($alias, '@', 1)) { $alias = '@' . $alias; } $delimiter = ObjectHelper::isNamespace($alias) ? '\\' : '/'; $pos = strpos($alias, $delimiter); $root = $pos === false ? $alias : substr($alias, 0, $pos); if ($path !== null) { if (!strncmp($path, '@', 1)) { $path = static::getAlias($path); } elseif ($trailingTrim) { $path = rtrim($path, '\\/'); } if (!isset(static::$aliases[$root])) { if ($pos === false) { static::$aliases[$root] = $path; } else { static::$aliases[$root] = [$alias => $path]; } } elseif (is_string(static::$aliases[$root])) { if ($pos === false) { static::$aliases[$root] = $path; } else { static::$aliases[$root] = [$alias => $path, $root => static::$aliases[$root]]; } } else { static::$aliases[$root][$alias] = $path; krsort(static::$aliases[$root]); } } elseif (isset(static::$aliases[$root])) { if (is_array(static::$aliases[$root])) { unset(static::$aliases[$root][$alias]); } elseif ($pos === false) { unset(static::$aliases[$root]); } } }