【Laravel3.0.0源码阅读分析】uri类uri.php

2021/5/30 14:49:59

本文主要是介绍【Laravel3.0.0源码阅读分析】uri类uri.php,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

<?php namespace Laravel;

class URI {

	/**
	 * The URI for the current request.
	 * 当前请求的URI。
	 * @var string
	 */
	public static $uri;

	/**
	 * The URI segments for the current request.
	 * 当前请求的 URI 段。
	 * @var array
	 */
	public static $segments = array();

	/**
	 * The server variables to check for the URI.
	 * The server variables to check for the URI.
	 * @var array
	 */
	protected static $attempt = array(
		'PATH_INFO', 'REQUEST_URI',
		'PHP_SELF', 'REDIRECT_URL'
	);

	/**
	 * Get the full URI including the query string.
	 * 获取包含查询字符串的完整 URI。
	 * @return string
	 */
	public static function full()
	{
		return static::current().static::query();
	}

	/**
	 * Get the URI for the current request.
	 * 获取当前请求的 URI。
	 * @return string
	 */
	public static function current()
	{
		if ( ! is_null(static::$uri)) return static::$uri;

		// To get the URI, we'll first call the detect method which will spin
		// through each of the server variables that we check for the URI in
		// and use the first one we encounter for the URI.
        // 要获取 URI,我们将首先调用检测方法,该方法将遍历我们检查 URI 的每个服务器变量,并使用我们遇到的第一个作为 URI。
		static::$uri = static::detect();

		// If you ever encounter this error, please inform the nerdy Laravel
		// dev team with information about your server. We want to support
		// Laravel an as many servers as we possibly can!
        // 如果您遇到此错误,请告知 Laravel 开发团队有关您的服务器的信息。 我们希望支持 Laravel 尽可能多的服务器!
		if (is_null(static::$uri))
		{
			throw new \Exception("Could not detect request URI.");
		}

		static::segments(static::$uri);

		return static::$uri;
	}

	/**
	 * Detect the URI from the server variables.
	 * 从服务器变量中检测 URI。
	 * @return string|null
	 */
	protected static function detect()
	{
		foreach (static::$attempt as $variable)
		{
			// Each variable we search for the URI has its own parser function
			// which is responsible for doing any formatting before the value
			// is fed into the main formatting function.
            // 我们搜索 URI 的每个变量都有自己的解析器函数,该函数负责在将值输入主格式化函数之前进行任何格式化。
			$method = "parse_{$variable}";

			if (isset($_SERVER[$variable]))
			{
				$uri = static::$method($_SERVER[$variable]);

				return static::format($uri);
			}
		}		
	}

	/**
	 * Format a given URI.
	 * 格式化给定的 URI。
	 * @param  string  $uri
	 * @return string
	 */
	protected static function format($uri)
	{
		// First we want to remove the application's base URL from the URI if it is
		// in the string. It is possible for some of the parsed server variables to
		// include the entire document root in the string.
        // 首先,我们要从 URI 中删除应用程序的基本 URL(如果它在字符串中)。 一些解析的服务器变量可能在字符串中包含整个文档根。
		$uri = static::remove_base($uri);

		$index = '/'.Config::get('application.index');

		// Next we'll remove the index file from the URI if it is there and then
		// finally trim down the URI. If the URI is left with spaces, we'll use
		// a single slash for the root URI.
        // 接下来,我们将从 URI 中删除索引文件(如果它在那里),然后最后修剪 URI。
        // 如果 URI 留有空格,我们将对根 URI 使用一个斜杠
		if ($index !== '/')
		{
			$uri = static::remove($uri, $index);
		}

		return trim($uri, '/') ?: '/';
	}

	/**
	 * Determine if the current URI matches a given pattern.
	 * 确定当前 URI 是否与给定模式匹配。
	 * @param  string  $pattern
	 * @return bool
	 */
	public static function is($pattern)
	{
		// Asterisks are translated into zero-or-more regular expression wildcards
		// to make it convenient to check if the URI starts with a given pattern
		// such as "library/*". This is only done when not root.
        // 星号被翻译成零个或多个正则表达式通配符,以便于检查 URI 是否以给定的模式(例如“library/*”)开头。 这仅在非 root 时完成。
		if ($pattern !== '/')
		{
			$pattern = str_replace('*', '(.*)', $pattern).'\z';
		}
		else
		{
			$pattern = '^/$';
		}

		return preg_match('#'.$pattern.'#', static::current());
	}

	/**
	 * Parse the PATH_INFO server variable.
	 * 解析 PATH_INFO 服务器变量。
	 * @param  string  $value
	 * @return string
	 */
	protected static function parse_path_info($value)
	{
		return $value;
	}

	/**
	 * Parse the REQUEST_URI server variable.
	 * 解析 REQUEST_URI 服务器变量。
	 * @param  string  $value
	 * @return string
	 */
	protected static function parse_request_uri($value)
	{
		return parse_url($value, PHP_URL_PATH);
	}

	/**
	 * Parse the PHP_SELF server variable.
	 * 解析 PHP_SELF 服务器变量。
	 * @param  string  $value
	 * @return string
	 */
	protected static function parse_php_self($value)
	{
		return $value;
	}

	/**
	 * Parse the REDIRECT_URL server variable.
	 * 解析 REDIRECT_URL 服务器变量。
	 * @param  string  $value
	 * @return string
	 */
	protected static function parse_redirect_url($value)
	{
		return $value;
	}

	/**
	 * Remove the base URL off of the request URI.
	 * 从请求 URI 中删除基本 URL。
	 * @param  string  $uri
	 * @return string
	 */
	protected static function remove_base($uri)
	{
		return static::remove($uri, parse_url(URL::base(), PHP_URL_PATH));
	}

	/**
	 * Get a specific segment of the request URI via an one-based index.
	 * 通过基于的索引获取请求 URI 的特定段。
	 * <code>
	 *		// Get the first segment of the request URI
	 *		$segment = URI::segment(1);
	 *
	 *		// Get the second segment of the URI, or return a default value
	 *		$segment = URI::segment(2, 'Taylor');
	 * </code>
	 *
	 * @param  int     $index
	 * @param  mixed   $default
	 * @return string
	 */
	public static function segment($index, $default = null)
	{
		static::current();

		return array_get(static::$segments, $index - 1, $default);
	}

	/**
	 * Set the URI segments for the request.
	 * 设置请求的 URI 段。
	 * @param  string  $uri
	 * @return void
	 */
	protected static function segments($uri)
	{
		$segments = explode('/', trim($uri, '/'));
        // array_diff-计算数组的差集
		static::$segments = array_diff($segments, array(''));
	}

	/**
	 * Remove a given value from the URI.
	 * 从 URI 中删除给定值。
	 * @param  string  $uri
	 * @param  string  $value
	 * @return string
	 */
	protected static function remove($uri, $value)
	{
		return (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
	}

	/**
	 * Get the query string for the current request.
	 * 获取当前请求的查询字符串。
	 * @return string
	 */
	protected static function query()
	{
	    // http_build_query-生成 URL-encode 之后的请求字符串
		return (count((array) $_GET) > 0) ? '?'.http_build_query($_GET) : '';
	}

}

 

github地址: https://github.com/liu-shilong/laravel3-scr    



这篇关于【Laravel3.0.0源码阅读分析】uri类uri.php的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程