public function run(Request $request) { // Save the app's URL parser state (e.g. the current callback map) $currentCallbacks = $this->currentCallbacks; $this->currentCallbacks = $this->rootCallbacks; try { // Remove empty path elements $uri = $request->url(); $parts = []; foreach (explode('/', $uri) as $part) { if ($part != '') { $parts[] = $part; } } // TODO: detect extension // TODO: run before filter // Walk through the URI and execute path callbacks foreach ($parts as $part) { // Try to find a callback array for the current URI part if (!array_key_exists('path', $this->currentCallbacks) || !array_key_exists($part, $this->currentCallbacks['path'])) { // TODO: generate an exeception, and catch that. return new Response(null, 404); } $c = $this->currentCallbacks['path'][$part]; // Reset the current callback array, so the path callback $this->currentCallbacks = []; // Execute path callback $rsp = $this->executeCallback($c, $request); // If there's already a response, return it and finish parsing the URL if ($rsp instanceof Response) { return $rsp; } } $method = $request->method(); // The URI has been processed. Call the appropriate method callback if (!array_key_exists($method, $this->currentCallbacks)) { // Nope, we can't serve this URI, 405 Not Allowed return new Response(null, 405); } // There indeed is a method callback, so let's call it! $rsp = $this->executeCallback($this->currentCallbacks[$method], $request); // If there's a response, we can return it if ($rsp instanceof Response) { return $rsp; } // TODO: formats? //return new Response(406); // Not acceptable format return new Response(null, 501); // Got no error, but got no response either. This is "Not Implemented". } finally { $this->currentCallbacks =& $currentCallbacks; } }
function testUrl() { $r = new Bullet\Request('DELETE', '/foo/bar/'); $this->assertEquals('/foo/bar/', $r->url()); }