private function givenIHaveAddedARouterWhere_RespondsWith($string, $return) { $router = new DynamicRouter(); $router->addPath($string, CallbackTarget::factory(function () use($return) { return $return; })); $this->router->add($router); }
private function given_IsRoutedToARespondingClass_ThatRespondsWith($path, $className, $methodBody) { eval('class ' . $className . ' implements \\watoki\\deli\\Responding { public function respond(\\watoki\\deli\\Request $request) { ' . $methodBody . ' } }'); $this->router->addPath($path, RespondingTarget::factory($this->factory, new $className())); }
private function whenIRouteTheRequest() { $this->target = $this->router->route($this->request->request); $this->response = $this->target->respond(); }
function testQuickStart() { /** * The first thing you need in order to use *curir* is to forward all HTTP requests * to a single file (e.g. `index.php`) with the target path in the query as `$_REQUEST['-']`. For apache * it would look like * <a href="javascript:" onclick="$('#htaccess').toggle();">this</a> * <pre id="htaccess" style="display: none;"> * <code> * # content of .htaccess * RewriteEngine On * RewriteBase / * RewriteRule ^(.*)$ index.php?-=$1 [L,QSA] * </code> * </pre> */ /** * ### Responding Objects * * You can then easily route all requests to a class implementing `Responding`, e.g. like * <a href="javascript:" onclick="$('#myResource').toggle();">this</a> * <div id="myResource" style="display: none;"> */ eval(' use \\watoki\\deli\\Responding; use \\watoki\\deli\\Request; use \\watoki\\curir\\delivery\\WebResponse; class MyResource implements Responding { public function respond(Request $request) { return "Hello World"; } } '); // </div> /** * with this line in your `index.php` */ WebDelivery::quickResponse('MyResource'); $this->thenTheResponseShouldBe('Hello World'); /** * ### DynamicRouter * * Or if you think creating a whole file to return "Hello World" is a little over-engineered, you can * use a `DynamicRouter` to map incoming URLs to anything. */ $router = new DynamicRouter(); $router->addPath('hello', CallbackTarget::factory(function () { return "Hello World"; })); /** * You can also use placeholders which will the set as request arguments */ $router->addPath('hi/{name}', CallbackTarget::factory(function (WebRequest $request) { return "Hi " . $request->getArguments()->get('name'); })); /** * And you can route to objects as well */ $respondingClass = 'MyResource'; $router->addPath('my', RespondingTarget::factory($this->factory, new $respondingClass())); /** * To get the whole routing and delivering going just call */ WebDelivery::quickRoute($router); /** * Let's give it a spin */ $this->givenTheTargetIs('hello'); WebDelivery::quickRoute($router); $this->thenTheResponseShouldBe('Hello World'); // <hr/> $this->givenTheTargetIs('hi/Joe'); WebDelivery::quickRoute($router); $this->thenTheResponseShouldBe('Hi Joe'); // <hr/> $this->givenTheTargetIs('my/anything'); WebDelivery::quickRoute($router); $this->thenTheResponseShouldBe('Hello World'); }