Request.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. /**
  9. * Request represents a request that is handled by an [[Application]].
  10. *
  11. * @property boolean $isConsoleRequest The value indicating whether the current request is made via console.
  12. * @property string $scriptFile Entry script file path (processed w/ realpath()).
  13. *
  14. * @author Qiang Xue <[email protected]>
  15. * @since 2.0
  16. */
  17. abstract class Request extends Component
  18. {
  19. private $_scriptFile;
  20. private $_isConsoleRequest;
  21. /**
  22. * Resolves the current request into a route and the associated parameters.
  23. * @return array the first element is the route, and the second is the associated parameters.
  24. */
  25. abstract public function resolve();
  26. /**
  27. * Returns a value indicating whether the current request is made via command line
  28. * @return boolean the value indicating whether the current request is made via console
  29. */
  30. public function getIsConsoleRequest()
  31. {
  32. return $this->_isConsoleRequest !== null ? $this->_isConsoleRequest : PHP_SAPI === 'cli';
  33. }
  34. /**
  35. * Sets the value indicating whether the current request is made via command line
  36. * @param boolean $value the value indicating whether the current request is made via command line
  37. */
  38. public function setIsConsoleRequest($value)
  39. {
  40. $this->_isConsoleRequest = $value;
  41. }
  42. /**
  43. * Returns entry script file path.
  44. * @return string entry script file path (processed w/ realpath())
  45. * @throws InvalidConfigException if the entry script file path cannot be determined automatically.
  46. */
  47. public function getScriptFile()
  48. {
  49. if ($this->_scriptFile === null) {
  50. if (isset($_SERVER['SCRIPT_FILENAME'])) {
  51. $this->setScriptFile($_SERVER['SCRIPT_FILENAME']);
  52. } else {
  53. throw new InvalidConfigException('Unable to determine the entry script file path.');
  54. }
  55. }
  56. return $this->_scriptFile;
  57. }
  58. /**
  59. * Sets the entry script file path.
  60. * The entry script file path can normally be determined based on the `SCRIPT_FILENAME` SERVER variable.
  61. * However, for some server configurations, this may not be correct or feasible.
  62. * This setter is provided so that the entry script file path can be manually specified.
  63. * @param string $value the entry script file path. This can be either a file path or a path alias.
  64. * @throws InvalidConfigException if the provided entry script file path is invalid.
  65. */
  66. public function setScriptFile($value)
  67. {
  68. $scriptFile = realpath(\Yii::getAlias($value));
  69. if ($scriptFile !== false && is_file($scriptFile)) {
  70. $this->_scriptFile = $scriptFile;
  71. } else {
  72. throw new InvalidConfigException('Unable to determine the entry script file path.');
  73. }
  74. }
  75. }