Passed
Push — main ( b15a24...935ae5 )
by Axel
04:15
created

LocaleRuntime::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ThemeBundle\Twig\Runtime;
15
16
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
17
use Symfony\Component\Form\FormFactoryInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21
use Symfony\Component\Routing\RouterInterface;
22
use Twig\Environment;
23
use Twig\Extension\RuntimeExtensionInterface;
24
use Zikula\Bundle\CoreBundle\Api\ApiInterface\LocaleApiInterface;
25
26
class LocaleRuntime implements RuntimeExtensionInterface
27
{
28
    public function __construct(
29
        private readonly RouterInterface $router,
30
        private readonly RequestStack $requestStack,
31
        private readonly Environment $twig,
32
        private readonly FormFactoryInterface $formFactory,
33
        private readonly LocaleApiInterface $localeApi
34
    ) {
35
    }
36
37
    public function renderLocaleSwitcher(): string
38
    {
39
        $locales = $this->localeApi->getSupportedLocaleNames();
40
        $localeLinks = [];
41
        /** @var Request $request */
42
        $request = $this->requestStack->getMainRequest();
43
        try {
44
            $routeInfo = $this->router->match($request->getPathInfo());
45
        } catch (\Exception) {
46
            return '';
47
        }
48
        $locale = $request->getLocale();
49
        $selectedRoute = false;
50
        foreach ($locales as $displayName => $code) {
51
            if ($locale === $code) {
52
                $url = $request->getPathInfo();
53
                $selectedRoute = $url;
54
            } else {
55
                $url = $this->router->generate($routeInfo['_route'], $this->filterRouteInfo($routeInfo, $code), UrlGeneratorInterface::ABSOLUTE_URL);
56
            }
57
            $localeLinks[$displayName] = $url;
58
        }
59
        if (2 > count($localeLinks)) {
60
            return '';
61
        }
62
63
        $form = $this->formFactory->create()->add('locale', ChoiceType::class, [
64
            'choices' => $localeLinks,
65
            'data' => $selectedRoute,
66
            'attr' => ['class' => 'locale-switcher-block']
67
        ]);
68
69
        return $this->twig->render('@ZikulaTheme/Locale/localeSwitcher.html.twig', [
70
            'form' => $form->createView(),
71
            'multilingual' => $this->localeApi->multilingual(),
72
        ]);
73
    }
74
75
    private function filterRouteInfo(array $routeInfo, string $locale): array
76
    {
77
        $params = [];
78
        foreach ($routeInfo as $param => $value) {
79
            if (0 !== mb_strpos($param, '_')) {
80
                $params[$param] = $value;
81
            }
82
        }
83
        $params['_locale'] = $locale;
84
85
        return $params;
86
    }
87
}
88