Request.php 3.1 KB

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