PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/console/Request.php

https://bitbucket.org/d1rk/lithium
PHP | 203 lines | 78 code | 20 blank | 105 comment | 9 complexity | a0f31e92eb40d0cdc8163e712f2c9628 MD5 | raw file
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\console;
  9. /**
  10. * The `Request` class represents a console request and holds information about it's
  11. * environment as well as passed arguments.
  12. *
  13. * @see lithium\console\Dispatcher
  14. */
  15. class Request extends \lithium\core\Object {
  16. /**
  17. * The raw data passed from the command line
  18. *
  19. * @var array
  20. */
  21. public $argv = array();
  22. /**
  23. * Parameters parsed from arguments.
  24. *
  25. * @see lithium\console\Router
  26. * @var array
  27. */
  28. public $params = array(
  29. 'command' => null, 'action' => 'run', 'args' => array()
  30. );
  31. /**
  32. * Input (STDIN).
  33. *
  34. * @var resource
  35. */
  36. public $input;
  37. /**
  38. * Enviroment variables.
  39. *
  40. * @var array
  41. */
  42. protected $_env = array();
  43. /**
  44. * Holds the value of the current locale, set through the `locale()` method.
  45. *
  46. * @var string
  47. */
  48. protected $_locale = null;
  49. /**
  50. * Auto configuration
  51. *
  52. * @var array
  53. */
  54. protected $_autoConfig = array('env' => 'merge');
  55. /**
  56. * Class Constructor
  57. *
  58. * @param array $config
  59. */
  60. public function __construct($config = array()) {
  61. $defaults = array('args' => array(), 'input' => null);
  62. $config += $defaults;
  63. parent::__construct($config);
  64. }
  65. /**
  66. * Initialize request object, pulling request data from superglobals.
  67. *
  68. * Defines an artificial `'PLATFORM'` environment variable as `'CLI'` to
  69. * allow checking for the SAPI in a normalized way. This is also for
  70. * establishing consistency with this class' sister classes.
  71. *
  72. * @see lithium\action\Request::_init()
  73. * @return void
  74. */
  75. protected function _init() {
  76. $this->_env += (array) $_SERVER + (array) $_ENV;
  77. $this->_env['working'] = getcwd() ?: null;
  78. $argv = (array) $this->env('argv');
  79. $this->_env['script'] = array_shift($argv);
  80. $this->_env['PLATFORM'] = 'CLI';
  81. $this->argv += $argv + (array) $this->_config['args'];
  82. $this->input = $this->_config['input'];
  83. if (!is_resource($this->_config['input'])) {
  84. $this->input = fopen('php://stdin', 'r');
  85. }
  86. parent::_init();
  87. }
  88. /**
  89. * Allows request parameters to be accessed as object properties, i.e. `$this->request->action`
  90. * instead of `$this->request->params['action']`.
  91. *
  92. * @see lithium\action\Request::$params
  93. * @param string $name The property name/parameter key to return.
  94. * @return mixed Returns the value of `$params[$name]` if it is set, otherwise returns null.
  95. */
  96. public function __get($name) {
  97. if (isset($this->params[$name])) {
  98. return $this->params[$name];
  99. }
  100. }
  101. public function __isset($name) {
  102. return isset($this->params[$name]);
  103. }
  104. /**
  105. * Get the value of a command line argument at a given key
  106. *
  107. * @param integer $key
  108. * @return mixed returns null if key does not exist or the value of the key in the args array
  109. */
  110. public function args($key = 0) {
  111. if (!empty($this->args[$key])) {
  112. return $this->args[$key];
  113. }
  114. return null;
  115. }
  116. /**
  117. * Get environment variables.
  118. *
  119. * @param string $key
  120. * @return mixed Returns the environment key related to the `$key` argument. If `$key` is equal
  121. * to null the result will be the entire environment array. If `$key` is set but not
  122. * available, `null` will be returned.
  123. */
  124. public function env($key = null) {
  125. if (!empty($this->_env[$key])) {
  126. return $this->_env[$key];
  127. }
  128. if ($key === null) {
  129. return $this->_env;
  130. }
  131. return null;
  132. }
  133. /**
  134. * Moves params up a level. Sets command to action, action to passed[0], and so on.
  135. *
  136. * @param integer $num how many times to shift
  137. * @return self
  138. */
  139. public function shift($num = 1) {
  140. for ($i = $num; $i > 1; $i--) {
  141. $this->shift(--$i);
  142. }
  143. $this->params['command'] = $this->params['action'];
  144. if (isset($this->params['args'][0])) {
  145. $this->params['action'] = array_shift($this->params['args']);
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Reads a line from input.
  151. *
  152. * @return string
  153. */
  154. public function input() {
  155. return fgets($this->input);
  156. }
  157. /**
  158. * Sets or returns the current locale string. For more information, see
  159. * "[Globalization](http://lithify.me/docs/manual/07_globalization)" in the manual.
  160. *
  161. * @param string $locale An optional locale string like `'en'`, `'en_US'` or `'de_DE'`. If
  162. * specified, will overwrite the existing locale.
  163. * @return Returns the currently set locale string.
  164. */
  165. public function locale($locale = null) {
  166. if ($locale) {
  167. $this->_locale = $locale;
  168. }
  169. return $this->_locale;
  170. }
  171. /**
  172. * Return input
  173. * Destructor. Closes input.
  174. *
  175. * @return void
  176. */
  177. public function __destruct() {
  178. if ($this->input) {
  179. fclose($this->input);
  180. }
  181. }
  182. }
  183. ?>