Skip to content

Commit

Permalink
MFH
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed May 18, 2024
2 parents 0d676d8 + 14a49f7 commit cf7f6f5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/main/php/web/Application.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace web;

use Closure, Generator;
use Closure, Traversable;
use lang\Value;

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ public function service($request, $response) {

// Handle dispatching
dispatch: $result= $this->routing()->handle($request, $response);
if ($result instanceof Generator) {
if ($result instanceof Traversable) {
foreach ($result as $kind => $argument) {
if ('dispatch' === $kind) {
$seen[$request->uri()->hashCode()]= true;
Expand Down
34 changes: 34 additions & 0 deletions src/main/php/web/Dispatch.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php namespace web;

use IteratorAggregate, Traversable;
use util\URI;

/**
* Dispatches a request; performing an internal redirect.
*
* Return instances of this class from a handler *before* the response
* has been flushed, as follows:
*
* ```php
* function($req, $res) {
* return $req->dispatch('/home');
* }
* ```
*
* @deprecated See https://github.com/xp-forge/web/issues/113#issuecomment-2118673725
* @see xp://web.Request#dispatch
*/
class Dispatch implements IteratorAggregate {
private $uri;

/** @param util.URI|string $uri */
public function __construct($uri) {
$this->uri= $uri instanceof URI ? $uri : new URI($uri);
}

/** @return util.URI */
public function uri() { return $this->uri; }

/** @return iterable */
public function getIterator(): Traversable { yield 'dispatch' => $this->uri; }
}
17 changes: 16 additions & 1 deletion src/test/php/web/unittest/ApplicationTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use test\{Assert, Expect, Test, Values};
use util\Objects;
use web\io\{TestInput, TestOutput};
use web\{Application, Environment, Error, Filter, Filters, Handler, Request, Response, Routes};
use web\{Application, Dispatch, Environment, Error, Filter, Filters, Handler, Request, Response, Routes};

class ApplicationTest {
private $environment;
Expand Down Expand Up @@ -218,6 +218,21 @@ public function dispatch_request_bubbles_up_to_toplevel() {
});
}

/** @deprecated */
#[Test]
public function dispatch_request_via_dispatch_instance() {
$this->assertHandled($handled, function() use(&$handled) {
return [
'/home' => function($request, $response) use(&$handled) {
$handled[]= [$request, $response];
},
'/' => function($request, $response) {
return new Dispatch('/home');
},
];
});
}

#[Test]
public function dispatch_works_with_nesting() {
$this->assertHandled($handled, function() use(&$handled) {
Expand Down

0 comments on commit cf7f6f5

Please sign in to comment.