Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 173 lines (155 sloc) 3.584 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2011, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/

namespace lithium\console;

/**
* The `Request` class reprents a console request and holds information about it's
* environment as well as passed arguments.
*
* @see lithium\console\Dispatcher
*/
class Request extends \lithium\core\Object {

/**
* The raw data passed from the command line
*
* @var array
*/
public $argv = array();

/**
* Parameters parsed from arguments.
*
* @see lithium\console\Router
* @var array
*/
public $params = array(
'command' => null, 'action' => 'run', 'args' => array()
);

/**
* Input (STDIN).
*
* @var resource
*/
public $input;

/**
* Enviroment variables.
*
* @var array
*/
protected $_env = array();

/**
* Auto configuration
*
* @var array
*/
protected $_autoConfig = array('env' => 'merge');

/**
* Class Constructor
*
* @param array $config
*/
public function __construct($config = array()) {
$defaults = array('args' => array(), 'input' => null);
$config += $defaults;
parent::__construct($config);
}

/**
* Initialize request object
*
* @return void
*/
protected function _init() {
$this->_env += (array) $_SERVER + (array) $_ENV;
$this->_env['working'] = getcwd() ?: null;
$argv = (array) $this->env('argv');
$this->_env['script'] = array_shift($argv);
$this->argv += $argv + (array) $this->_config['args'];
$this->input = $this->_config['input'];

if (!is_resource($this->_config['input'])) {
$this->input = fopen('php://stdin', 'r');
}
parent::_init();
}

/**
* Allows request parameters to be accessed as object properties, i.e. `$this->request->action`
* instead of `$this->request->params['action']`.
*
* @see lithium\action\Request::$params
* @param string $name The property name/parameter key to return.
* @return mixed Returns the value of `$params[$name]` if it is set, otherwise returns null.
*/
public function __get($name) {
if (isset($this->params[$name])) {
return $this->params[$name];
}
}

public function __isSet($name) {
return isset($this->params[$name]);
}

/**
* Get the value of a command line argument at a given key
*
* @param integer $key
* @return mixed returns null if key does not exist or the value of the key in the args array
*/
public function args($key = 0) {
if (!empty($this->args[$key])) {
return $this->args[$key];
}
return null;
}

/**
* Get environment variables.
*
* @param string $key
* @return string|void
*/
public function env($key = null) {
if (!empty($this->_env[$key])) {
return $this->_env[$key];
}
if ($key === null) {
return $this->_env;
}
return null;
}

/**
* Moves params up a level. Sets command to action, action to passed[0], and so on.
*
* @param integer $num how many times to shift
* @return self
*/
public function shift($num = 1) {
for ($i = $num; $i > 1; $i--) {
$this->shift(--$i);
}
$this->params['command'] = $this->params['action'];
if (isset($this->params['args'][0])) {
$this->params['action'] = array_shift($this->params['args']);
}
return $this;
}

/**
* Reads a line from input.
*
* @return string
*/
public function input() {
return fgets($this->input);
}

/**
* Return input
* Destructor. Closes input.
*
* @return void
*/
public function __destruct() {
if ($this->input) {
fclose($this->input);
}
}
}

?>
Something went wrong with that request. Please try again.