Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

87 Zeilen
2.9KB

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