lithium\console\Router::parse PHP Method

parse() public static method

XF68-style long options (i.e. -foo) are not supported but support can be added by extending this class.
public static parse ( Request $request = null ) : array
$request 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 ($args) {
                $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;
    }

Usage Example

Beispiel #1
0
 public function testParseWithParam()
 {
     $expected = array('command' => 'test', 'action' => 'action', 'args' => array(), 'i' => true);
     $result = Router::parse(new Request(array('args' => array('test', 'action', '-i'))));
     $this->assertEqual($expected, $result);
     $expected = array('command' => 'test', 'action' => 'action', 'args' => array('something'), 'i' => true);
     $result = Router::parse(new Request(array('args' => array('test', 'action', '-i', 'something'))));
     $this->assertEqual($expected, $result);
 }
All Usage Examples Of lithium\console\Router::parse
Router