1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Controller; |
6
|
|
|
|
7
|
|
|
use App\Dictionary\LanguageCode; |
8
|
|
|
use App\Dictionary\TranslationDomain; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Sunrise\Http\Message\Response\HtmlResponse; |
13
|
|
|
use Sunrise\Http\Router\Annotation\GetRoute; |
14
|
|
|
use Sunrise\Http\Router\Helper\TemplateRenderer; |
15
|
|
|
use Sunrise\Http\Router\ServerRequest; |
16
|
|
|
use Sunrise\Translator\TranslatorManagerInterface; |
17
|
|
|
|
18
|
|
|
#[GetRoute('welcome', '/')] |
19
|
|
|
final class WelcomeController implements RequestHandlerInterface |
20
|
|
|
{ |
21
|
|
|
public const LANGUAGE_CODE_VAR_NAME = 'language_code'; |
22
|
|
|
public const GREETING_MESSAGE_VAR_NAME = 'greeting_message'; |
23
|
|
|
|
24
|
|
|
private const TEMPLATE_FILENAME = __DIR__ . '/../../resources/templates/welcome.phtml'; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
private readonly TranslatorManagerInterface $translatorManager, |
28
|
|
|
) { |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
32
|
|
|
{ |
33
|
|
|
$clientPreferredLanguage = ServerRequest::create($request) |
34
|
|
|
->getClientPreferredLanguage(...LanguageCode::cases()) |
35
|
|
|
?? LanguageCode::English; |
36
|
|
|
|
37
|
|
|
$translatedGreetingMessage = $this->translatorManager->translate( |
38
|
|
|
TranslationDomain::APP, |
39
|
|
|
$clientPreferredLanguage->getCode(), |
40
|
|
|
'Welcome!', |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$template = TemplateRenderer::renderTemplate(self::TEMPLATE_FILENAME, [ |
44
|
|
|
self::LANGUAGE_CODE_VAR_NAME => $clientPreferredLanguage->getCode(), |
45
|
|
|
self::GREETING_MESSAGE_VAR_NAME => $translatedGreetingMessage, |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
return new HtmlResponse(200, $template); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|