.*?) = (? .*) /x'; protected $_flagpat = '/ ^-+ (? [a-z]+) /xi'; protected $_flags = []; public function __construct () { global $argv; $script = array_shift($argv); $request = []; $path_arr = []; $argc = 1; foreach ($argv as $arg) { $matches = []; if (preg_match($this->_keyvalpat, $arg, $matches)) { // handle pulling key/values into the "request" $request[ $matches['key'] ] = $matches['val']; } else if (preg_match($this->_flagpat, $arg, $matches)) { // set some bits for --flag style flags $this->_flags[ $matches['flag'] ] = true; } else if (count($path_arr) >= 2) { $request['arg' . ($argc++)] = $arg; } else { // tack other strings on to path $path_arr[] = $arg; } } // we need a leading slash and a slash-separated path // for all the routing crap: $this->_path = implode('/', $path_arr); // if we have a path, but it doesn't start with a leading slash, // add one: if (strlen($this->_path)) { if ($this->_path[0] !== '/') { $this->_path = '/' . $this->_path; } } // set up a Request object switch ($this->method()) { case 'GET' : $this->_req = new Get($request); break; case 'POST' : $this->_req = new Post($request); break; case 'DELETE' : $this->_req = new Delete($request); break; case 'HEAD' : $this->_req = new Head($request); break; default : $this->_req = new Get($request); } } public function method () { // silly, liable to break: if (isset($this->_flags['get'])) return 'GET'; if (isset($this->_flags['post'])) return 'POST'; if (isset($this->_flags['delete'])) return 'DELETE'; if (isset($this->_flags['head'])) return 'HEAD'; // silly default: return 'GET'; } /** * For now, just fake a session. */ public function startSession () { $GLOBALS['_SESSION'] = []; } /** * End the current session. */ public function endSession () { $GLOBALS['_SESSION'] = array(); } public function header ($header) { return; } }