|
1 | 1 | <?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
2 | 9 | namespace Ibexa\Bundle\AdminUi\Templating\Twig; |
3 | 10 |
|
4 | | -use Ibexa\User\UserSetting\UserSettingService; |
5 | | -use Symfony\Component\Asset\Packages; |
6 | | -use Symfony\Component\HttpFoundation\RequestStack; |
| 11 | +use Ibexa\Contracts\AdminUi\Rtl\RtlModeResolverInterface; |
7 | 12 | use Twig\Extension\AbstractExtension; |
8 | 13 | use Twig\Extension\GlobalsInterface; |
9 | | -use Twig\TwigFunction; |
10 | 14 |
|
11 | | -class RtlExtension extends AbstractExtension implements GlobalsInterface |
| 15 | +final class RtlExtension extends AbstractExtension implements GlobalsInterface |
12 | 16 | { |
13 | | - private Packages $assets; |
14 | | - private RequestStack $requestStack; |
15 | | - private UserSettingService $userSettingService; |
16 | | - private array $rtlLanguages; |
17 | | - |
18 | | - public function __construct(Packages $assets, RequestStack $requestStack, UserSettingService $userSettingService, array $rtlLanguages = []) |
19 | | - { |
20 | | - $this->assets = $assets; |
21 | | - $this->requestStack = $requestStack; |
22 | | - $this->userSettingService = $userSettingService; |
23 | | - $this->rtlLanguages = $rtlLanguages; |
| 17 | + public function __construct( |
| 18 | + private readonly RtlModeResolverInterface $rtlModeResolver, |
| 19 | + ) { |
24 | 20 | } |
25 | 21 |
|
26 | 22 | public function getGlobals(): array |
27 | 23 | { |
28 | 24 | return [ |
29 | | - 'ibexa_is_rtl' => $this->requestStack->getCurrentRequest() !== null && $this->isRtl(), |
30 | | - ]; |
31 | | - } |
32 | | - |
33 | | - public function isRtl(): bool |
34 | | - { |
35 | | - $userLanguage = $this->userSettingService->getUserSetting('language')->value; |
36 | | - |
37 | | - return in_array($userLanguage, $this->rtlLanguages, true); |
38 | | - } |
39 | | - |
40 | | - public function getFunctions() |
41 | | - { |
42 | | - return [ |
43 | | - new TwigFunction('ibexa_encore_entry_link_tags', [$this, 'renderRtlEncoreEntryLinkTags'], ['is_safe' => ['html']]), |
| 25 | + 'ibexa_is_rtl' => $this->rtlModeResolver->isRtl(), |
44 | 26 | ]; |
45 | 27 | } |
46 | | - |
47 | | - public function renderRtlEncoreEntryLinkTags(string $entryName, ?string $packageName = null, ?string $entrypointName = null, array $extraRtlEntries = []) |
48 | | - { |
49 | | - $fileSuffix = $this->isRtl() ? '-rtl.css' : '.css'; |
50 | | - $url = $this->assets->getUrl("assets/ibexa/build/{$entryName}{$fileSuffix}", $packageName); |
51 | | - $tags = sprintf('<link rel="stylesheet" href="%s" />', htmlspecialchars($url, ENT_QUOTES)); |
52 | | - |
53 | | - if ($this->isRtl()) { |
54 | | - foreach ($extraRtlEntries as $extra) { |
55 | | - $extraEntry = is_array($extra) ? $extra['entry'] : $extra; |
56 | | - $extraPackage = is_array($extra) ? ($extra['package'] ?? null) : null; |
57 | | - $extraUrl = $this->assets->getUrl("assets/ibexa/build/{$extraEntry}.css", $extraPackage); |
58 | | - $tags .= "\n" . sprintf('<link rel="stylesheet" href="%s" />', htmlspecialchars($extraUrl, ENT_QUOTES)); |
59 | | - } |
60 | | - } |
61 | | - |
62 | | - return $tags; |
63 | | - } |
64 | 28 | } |
0 commit comments