Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 55 lines (49 sloc) 1.75 kb
e7f3c31 @gwoo going lithium
gwoo authored
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
14de7bf @gwoo Happy 2012!
gwoo authored
5 * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
e7f3c31 @gwoo going lithium
gwoo authored
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
7 */
f65a402 @gwoo updating headers
gwoo authored
8
e7f3c31 @gwoo going lithium
gwoo authored
9 namespace lithium\console;
10
11 /**
8a1491d @nateabele Adding class-level docblocks for various classes.
nateabele authored
12 * The `Router` class uses an instance of `lithium\console\Request`, which represents an incoming
a0930f4 @phishy fixed spelling errors
phishy authored
13 * command-line invocation, to parse the correct command, and sub-command(s) and parameters, which
8a1491d @nateabele Adding class-level docblocks for various classes.
nateabele authored
14 * are used by `lithium\console\Dispatcher` to load and execute the proper `Command` class.
15 */
e7f3c31 @gwoo going lithium
gwoo authored
16 class Router extends \lithium\core\Object {
17
18 /**
884af54 @davidpersson Documenting console option parsing behavior.
davidpersson authored
19 * Parse incoming request from console. Short and long (GNU-style) options
20 * in the form of `-f`, `--foo`, `--foo-bar` and `--foo=bar` are parsed.
21 * XF68-style long options (i.e. `-foo`) are not supported but support
22 * can be added by extending this class.
e7f3c31 @gwoo going lithium
gwoo authored
23 *
690f4c3 @nateabele Fixing formatting in console classes. Adding test coverage for `Extra…
nateabele authored
24 * @param object $request lithium\console\Request
7567c72 @gwoo modifying params in \console\Request.
gwoo authored
25 * @return array $params
c690b8b @davidpersson QA: Fixing docblock closings `**/` -> `*/`.
davidpersson authored
26 */
e7f3c31 @gwoo going lithium
gwoo authored
27 public static function parse($request = null) {
3102ff4 @gwoo allowing flag params to be the first in the sequence of command line …
gwoo authored
28 $defaults = array('command' => null, 'action' => 'run', 'args' => array());
29 $params = $request ? (array) $request->params + $defaults : $defaults;
77e5457 @gwoo more work on new \command\Create interface.
gwoo authored
30
31 if (!empty($request->argv)) {
32 $args = $request->argv;
3102ff4 @gwoo allowing flag params to be the first in the sequence of command line …
gwoo authored
33
e7f3c31 @gwoo going lithium
gwoo authored
34 while ($arg = array_shift($args)) {
cece130 @davidpersson Removing support for parsing XF86-style long options, standarizing on…
davidpersson authored
35 if (preg_match('/^-(?P<key>[a-zA-Z0-9])$/i', $arg, $match)) {
7567c72 @gwoo modifying params in \console\Request.
gwoo authored
36 $params[$match['key']] = true;
e7f3c31 @gwoo going lithium
gwoo authored
37 continue;
38 }
923c9f5 @nateabele Fixing regex parameter match, now properly matches flags with upperca…
nateabele authored
39 if (preg_match('/^--(?P<key>[a-z0-9-]+)(?:=(?P<val>.+))?$/i', $arg, $match)) {
7567c72 @gwoo modifying params in \console\Request.
gwoo authored
40 $params[$match['key']] = !isset($match['val']) ? true : $match['val'];
7b617fe @davidpersson Updating argument parsing in console router.
davidpersson authored
41 continue;
e7f3c31 @gwoo going lithium
gwoo authored
42 }
7567c72 @gwoo modifying params in \console\Request.
gwoo authored
43 $params['args'][] = $arg;
e7f3c31 @gwoo going lithium
gwoo authored
44 }
45 }
3102ff4 @gwoo allowing flag params to be the first in the sequence of command line …
gwoo authored
46 foreach (array('command', 'action') as $param) {
47 if (!empty($params['args'])) {
48 $params[$param] = array_shift($params['args']);
49 }
e7f3c31 @gwoo going lithium
gwoo authored
50 }
51 return $params;
52 }
8eed07a @nateabele Adding PHP close tags to classes, removing old docblock info.
nateabele authored
53 }
54
55 ?>
Something went wrong with that request. Please try again.