init
This commit is contained in:
54 files changed
+4604
No files matched your search
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Eitaa\Client;
|
||||
use App\Http\Controllers\DemoController;
|
||||
use App\Http\Request;
|
||||
use App\Http\Response;
|
||||
use App\Security\RateLimiter;
|
||||
use App\Security\Session;
|
||||
use function App\Support\storage_path;
|
||||
use function App\Support\view;
|
||||
|
||||
/** Composition root: dependencies are constructed once and passed explicitly. */
|
||||
final class Application
|
||||
{
|
||||
private function __construct(
|
||||
private readonly Config $config,
|
||||
private readonly Request $request,
|
||||
private readonly Session $session,
|
||||
private readonly DemoController $controller,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function boot(): self
|
||||
{
|
||||
ini_set('display_errors', '0');
|
||||
$request = Request::capture();
|
||||
Response::securityHeaders($request->isHttps());
|
||||
$config = Config::load();
|
||||
$session = new Session(storage_path('sessions'), $config->get('session_lifetime'));
|
||||
$session->start($request->isHttps());
|
||||
$controller = new DemoController(
|
||||
$config,
|
||||
$session,
|
||||
new RateLimiter(storage_path('cache/limits.json')),
|
||||
new Client($config->get('token')),
|
||||
);
|
||||
return new self($config, $request, $session, $controller);
|
||||
}
|
||||
|
||||
public function render(): void
|
||||
{
|
||||
$csrf = $this->session->csrf();
|
||||
$this->session->close();
|
||||
$publicUrl = rtrim($this->config->get('public_url'), '/');
|
||||
view('app', [
|
||||
'csrf' => $csrf,
|
||||
'shareImage' => $publicUrl === '' ? '' : $publicUrl . '/assets/brand/social.png',
|
||||
]);
|
||||
}
|
||||
|
||||
public function api(): void
|
||||
{
|
||||
try {
|
||||
$data = $this->controller->dispatch($this->request);
|
||||
} finally {
|
||||
$this->session->close();
|
||||
}
|
||||
Response::json($data);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user