Skip to content

Commit 2bda515

Browse files
committed
Add front controller example
1 parent ca29ebb commit 2bda515

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

index.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5+
use App\FrontController;
56
use Boson\Application;
67
use Boson\ApplicationCreateInfo;
7-
use Boson\Component\Http\Response;
88
use Boson\Component\Http\Static\FilesystemStaticProvider;
99
use Boson\WebView\Api\Schemes\Event\SchemeRequestReceived;
1010
use Boson\WebView\Api\WebComponents\WebComponentsExtension;
@@ -122,19 +122,10 @@
122122
*
123123
*/
124124

125-
$app->on(static function (SchemeRequestReceived $e) use ($static): void {
126-
//
127-
// Install file (if exists) as response using static adapter...
128-
//
129-
if ($e->response = $static->findFileByRequest($e->request)) {
130-
$e->stopPropagation();
125+
$controller = new FrontController($static);
131126

132-
return;
133-
}
134-
135-
$response = file_get_contents(__DIR__ . '/assets/view/layout/main.html');
136-
137-
$e->response = new Response($response);
127+
$app->on(static function (SchemeRequestReceived $e) use ($controller): void {
128+
$e->response = $controller($e->request);
138129
});
139130

140131

src/FrontController.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
use Boson\Component\Http\Component\StatusCode;
8+
use Boson\Component\Http\Response;
9+
use Boson\Component\Http\Static\StaticProviderInterface;
10+
use Boson\Contracts\Http\RequestInterface;
11+
use Boson\Contracts\Http\ResponseInterface;
12+
13+
final readonly class FrontController
14+
{
15+
public function __construct(
16+
private StaticProviderInterface $static,
17+
) {}
18+
19+
public function __invoke(RequestInterface $request): ?ResponseInterface
20+
{
21+
$response = $this->static->findFileByRequest($request);
22+
23+
if ($response !== null) {
24+
return $response;
25+
}
26+
27+
return new Response(
28+
body: file_get_contents(__DIR__ . '/../assets/view/layout/main.html'),
29+
status: StatusCode::Ok,
30+
);
31+
}
32+
}

0 commit comments

Comments
 (0)