private function givenIHaveAddedARouterWhere_RespondsWith($string, $return) { $router = new DynamicRouter(); $router->addPath($string, CallbackTarget::factory(function () use($return) { return $return; })); $this->router->add($router); }
public static function quickRoot($rootDirectory, $defaultPath = 'index', $namespace = '', Factory $factory = null) { $factory = $factory ?: self::init(); $router = new WebRouter($factory, $rootDirectory, $namespace); $router->setDefaultTarget(CallbackTarget::factory(function (WebRequest $request) use($router, $defaultPath) { return $router->route($request->withTarget(Path::fromString($defaultPath)))->respond(); })); self::quickRoute($router, $factory); }
<?php use spec\watoki\deli\fixtures\TestDelivererStub; use watoki\deli\Delivery; use watoki\deli\Path; use watoki\deli\Request; use watoki\deli\router\NoneRouter; use watoki\deli\target\CallbackTarget; require_once __DIR__ . '/../../vendor/autoload.php'; error_reporting(0); $router = new NoneRouter(CallbackTarget::factory(function () { /** @noinspection PhpUndefinedFunctionInspection */ causeFatalError(); })); $test = new TestDelivererStub(new Request(new Path(), Path::fromString('some/target'))); $test->onDeliver(function ($response) { echo $response; }); $delivery = new Delivery($router, $test, $test); $delivery->run();
public function givenTheTargetRespondsWith($callback) { $this->router = new NoneRouter(CallbackTarget::factory($callback)); }
private function givenISetATargetForThePath_Responding($path, $return) { $this->router->addPath($path, CallbackTarget::factory(function () use($return) { return $return; })); }
private function given_IsRoutedToTheCallback($path, $callback) { $this->router->addPath($path, CallbackTarget::factory($callback)); }
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'); }