1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @link https://www.yiiframework.com/ |
||
5 | * @copyright Copyright (c) 2008 Yii Software LLC |
||
6 | * @license https://www.yiiframework.com/license/ |
||
7 | */ |
||
8 | |||
9 | namespace yii\base; |
||
10 | |||
11 | use Yii; |
||
12 | |||
13 | /** |
||
14 | * Request represents a request that is handled by an [[Application]]. |
||
15 | * |
||
16 | * For more details and usage information on Request, see the [guide article on requests](guide:runtime-requests). |
||
17 | * |
||
18 | * @property bool $isConsoleRequest The value indicating whether the current request is made via console. |
||
19 | * @property string $scriptFile Entry script file path (processed w/ realpath()). |
||
20 | * |
||
21 | * @author Qiang Xue <[email protected]> |
||
22 | * @since 2.0 |
||
23 | */ |
||
24 | abstract class Request extends Component |
||
25 | { |
||
26 | private $_scriptFile; |
||
27 | private $_isConsoleRequest; |
||
28 | |||
29 | |||
30 | /** |
||
31 | * Resolves the current request into a route and the associated parameters. |
||
32 | * @return array the first element is the route, and the second is the associated parameters. |
||
33 | */ |
||
34 | abstract public function resolve(); |
||
35 | |||
36 | /** |
||
37 | * Returns a value indicating whether the current request is made via command line. |
||
38 | * @return bool the value indicating whether the current request is made via console |
||
39 | */ |
||
40 | public function getIsConsoleRequest() |
||
41 | { |
||
42 | return $this->_isConsoleRequest !== null ? $this->_isConsoleRequest : PHP_SAPI === 'cli'; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Sets the value indicating whether the current request is made via command line. |
||
47 | * @param bool $value the value indicating whether the current request is made via command line |
||
48 | */ |
||
49 | 448 | public function setIsConsoleRequest($value) |
|
50 | { |
||
51 | 448 | $this->_isConsoleRequest = $value; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * Returns entry script file path. |
||
56 | * @return string entry script file path (processed w/ realpath()) |
||
57 | * @throws InvalidConfigException if the entry script file path cannot be determined automatically. |
||
58 | */ |
||
59 | 6 | public function getScriptFile() |
|
60 | { |
||
61 | 6 | if ($this->_scriptFile === null) { |
|
62 | 6 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
|
63 | 6 | $this->setScriptFile($_SERVER['SCRIPT_FILENAME']); |
|
64 | } else { |
||
65 | throw new InvalidConfigException('Unable to determine the entry script file path.'); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | 6 | return $this->_scriptFile; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Sets the entry script file path. |
||
74 | * The entry script file path can normally be determined based on the `SCRIPT_FILENAME` SERVER variable. |
||
75 | * However, for some server configurations, this may not be correct or feasible. |
||
76 | * This setter is provided so that the entry script file path can be manually specified. |
||
77 | * @param string $value the entry script file path. This can be either a file path or a [path alias](guide:concept-aliases). |
||
78 | * @throws InvalidConfigException if the provided entry script file path is invalid. |
||
79 | */ |
||
80 | 6 | public function setScriptFile($value) |
|
81 | { |
||
82 | 6 | $scriptFile = realpath(Yii::getAlias($value)); |
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
83 | 6 | if ($scriptFile !== false && is_file($scriptFile)) { |
|
84 | 6 | $this->_scriptFile = $scriptFile; |
|
85 | } else { |
||
86 | throw new InvalidConfigException('Unable to determine the entry script file path.'); |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 |