Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 53 lines (47 sloc) 1.53 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
<?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 `Router` class uses an instance of `lithium\console\Request`, which represents an incoming
* command-line invocation, to parse the correct command, and sub-command(s) and parameters, which
* are used by `lithium\console\Dispatcher` to load and execute the proper `Command` class.
*/
class Router extends \lithium\core\Object {

/**
* Parse incoming request from console
*
* @param object $request lithium\console\Request
* @return array $params
*
*/
public static function parse($request = null) {
$defaults = array('command' => null, 'action' => 'run', 'args' => array());
$params = $request ? (array) $request->params + $defaults : $defaults;

if (!empty($request->argv)) {
$args = $request->argv;

while ($arg = array_shift($args)) {
if (preg_match('/^-(?P<key>[a-zA-Z0-9])$/i', $arg, $match)) {
$params[$match['key']] = true;
continue;
}
if (preg_match('/^--(?P<key>[a-z0-9-]+)(?:=(?P<val>.+))?$/i', $arg, $match)) {
$params[$match['key']] = !isset($match['val']) ? true : $match['val'];
continue;
}
$params['args'][] = $arg;
}
}
foreach (array('command', 'action') as $param) {
if (!empty($params['args'])) {
$params[$param] = array_shift($params['args']);
}
}
return $params;
}
}

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