Request.php 2.7 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7 8
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;
9

10
use Yii;
Qiang Xue committed
11 12

/**
13
 * Request represents a request that is handled by an [[Application]].
14 15 16 17
 *
 * @property boolean $isConsoleRequest The value indicating whether the current request is made via console.
 * @property string $scriptFile Entry script file path (processed w/ realpath()).
 *
Qiang Xue committed
18 19 20
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
21
abstract class Request extends Component
Qiang Xue committed
22 23
{
	private $_scriptFile;
Qiang Xue committed
24
	private $_isConsoleRequest;
Qiang Xue committed
25

Qiang Xue committed
26 27 28 29 30 31
	/**
	 * Resolves the current request into a route and the associated parameters.
	 * @return array the first element is the route, and the second is the associated parameters.
	 */
	abstract public function resolve();

Qiang Xue committed
32
	/**
Qiang Xue committed
33 34
	 * Returns a value indicating whether the current request is made via command line
	 * @return boolean the value indicating whether the current request is made via console
Qiang Xue committed
35
	 */
Qiang Xue committed
36
	public function getIsConsoleRequest()
Qiang Xue committed
37
	{
Qiang Xue committed
38
		return $this->_isConsoleRequest !== null ? $this->_isConsoleRequest : PHP_SAPI === 'cli';
Qiang Xue committed
39 40 41
	}

	/**
Qiang Xue committed
42 43
	 * Sets the value indicating whether the current request is made via command line
	 * @param boolean $value the value indicating whether the current request is made via command line
Qiang Xue committed
44
	 */
Qiang Xue committed
45
	public function setIsConsoleRequest($value)
Qiang Xue committed
46
	{
Qiang Xue committed
47
		$this->_isConsoleRequest = $value;
Qiang Xue committed
48 49 50
	}

	/**
Qiang Xue committed
51 52
	 * Returns entry script file path.
	 * @return string entry script file path (processed w/ realpath())
Qiang Xue committed
53
	 * @throws InvalidConfigException if the entry script file path cannot be determined automatically.
Qiang Xue committed
54
	 */
Qiang Xue committed
55
	public function getScriptFile()
Qiang Xue committed
56
	{
Qiang Xue committed
57
		if ($this->_scriptFile === null) {
Qiang Xue committed
58 59 60 61 62
			if (isset($_SERVER['SCRIPT_FILENAME'])) {
				$this->setScriptFile($_SERVER['SCRIPT_FILENAME']);
			} else {
				throw new InvalidConfigException('Unable to determine the entry script file path.');
			}
Qiang Xue committed
63 64
		}
		return $this->_scriptFile;
Qiang Xue committed
65 66 67
	}

	/**
Qiang Xue committed
68
	 * Sets the entry script file path.
Qiang Xue committed
69 70 71 72 73
	 * The entry script file path can normally be determined based on the `SCRIPT_FILENAME` SERVER variable.
	 * However, for some server configurations, this may not be correct or feasible.
	 * This setter is provided so that the entry script file path can be manually specified.
	 * @param string $value the entry script file path. This can be either a file path or a path alias.
	 * @throws InvalidConfigException if the provided entry script file path is invalid.
Qiang Xue committed
74
	 */
Qiang Xue committed
75
	public function setScriptFile($value)
Qiang Xue committed
76
	{
77
		$scriptFile = realpath(Yii::getAlias($value));
Qiang Xue committed
78 79 80 81 82
		if ($scriptFile !== false && is_file($scriptFile)) {
			$this->_scriptFile = $scriptFile;
		} else {
			throw new InvalidConfigException('Unable to determine the entry script file path.');
		}
Qiang Xue committed
83 84
	}
}