• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 15706350668

17 Jun 2025 11:46AM UTC coverage: 26.167% (+0.03%) from 26.142%
15706350668

push

github

koertho
fix routing utils parameters

7 of 8 new or added lines in 1 file covered. (87.5%)

1570 of 6000 relevant lines covered (26.17%)

1.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.12
/src/Util/Routing/RoutingUtil.php
1
<?php
2

3
/*
4
 * Copyright (c) 2023 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\Util\Routing;
10

11
use Contao\CoreBundle\Csrf\ContaoCsrfTokenManager;
12
use HeimrichHannot\UtilsBundle\Util\AbstractServiceSubscriber;
13
use Psr\Container\ContainerExceptionInterface;
14
use Psr\Container\ContainerInterface;
15
use Psr\Container\NotFoundExceptionInterface;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18
use Symfony\Component\Routing\RouterInterface;
19
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
20

21
class RoutingUtil extends AbstractServiceSubscriber
22
{
23
    /**
24
     * @var RouterInterface
25
     */
26
    private $router;
27
    /**
28
     * @var ContainerInterface
29
     */
30
    private $container;
31
    /**
32
     * @var string
33
     */
34
    private $csrfTokenName;
35
    /**
36
     * @var RequestStack
37
     */
38
    private $requestStack;
39

40
    public function __construct(ContainerInterface $container, RouterInterface $router, string $csrfTokenName, RequestStack $requestStack)
41
    {
42
        $this->router = $router;
1✔
43
        $this->container = $container;
1✔
44
        $this->csrfTokenName = $csrfTokenName;
1✔
45
        $this->requestStack = $requestStack;
1✔
46
    }
47

48
    /**
49
     * Generate a backend route with token and referer.
50
     *
51
     * Options:
52
     * - absoluteUrl (bool): Return absolute url (default: false)
53
     * - route (string): Override the default backend route (default: contao_backend)
54
     *
55
     * @param array $params Url-Parameters
56
     * @param bool $addToken
57
     * @param bool $addReferer
58
     * @param array{
59
     *     route: string,
60
     *     absoluteUrl: bool,
61
     * }|string $options
62
     * @return string The backend route url
63
     * @throws ContainerExceptionInterface
64
     * @throws NotFoundExceptionInterface
65
     */
66
    public function generateBackendRoute(array $params = [], bool $addToken = true, bool $addReferer = true, $options = [], $route = []): string
67
    {
68
        if (is_string($options)) {
1✔
69
            trigger_deprecation(
1✔
70
                'heimrichhannot/contao-utils-bundle',
1✔
71
                '2.244.0',
1✔
72
                'Passing a string as fourth parameter is deprecated. Use an array with the key "route" instead.'
1✔
73
            );
1✔
74
            $options = ['route' => $options];
1✔
75
        } elseif (!is_array($options)) {
1✔
76
            throw new \InvalidArgumentException('Fourth parameter must be a string or an array.');
×
77
        }
78

79
        if (!empty($route)) {
1✔
80
            trigger_deprecation(
1✔
81
                'heimrichhannot/contao-utils-bundle',
1✔
82
                '2.244.0',
1✔
83
                'Passing more than four parameters or the route parameter is deprecated. Use an array as fourth parameter \'options\' with the key "route" instead.'
1✔
84
            );
1✔
85
            if (is_string($route)) {
1✔
86
                if (!isset($options['route'])) {
1✔
87
                    $options['route'] = $route;
1✔
88
                }
89
            } elseif (is_array($route)) {
1✔
90
                $options = array_merge($route, $options);
1✔
91
            } else {
NEW
92
                throw new \InvalidArgumentException('Parameter route must be a string or an array.');
×
93
            }
94
        }
95

96
        $options = array_merge(
1✔
97
            ['absoluteUrl' => false],
1✔
98
            $options
1✔
99
        );
1✔
100

101
        if ($addToken) {
1✔
102
            // >= contao 4.6.8 uses contao.csrf.token_manager service to validate token
103
            if ($this->container->has(ContaoCsrfTokenManager::class)) {
1✔
104
                $params['rt'] = $this->container->get(ContaoCsrfTokenManager::class)->getToken($this->csrfTokenName)->getValue();
1✔
105
            } elseif ($this->container->has(CsrfTokenManagerInterface::class)) {
1✔
106
                $params['rt'] = $this->container->get(CsrfTokenManagerInterface::class)->getToken($this->csrfTokenName)->getValue();
1✔
107
            }
108
        }
109

110
        if ($addReferer && ($request = $this->requestStack->getCurrentRequest())) {
1✔
111
            $params['ref'] = $request->get('_contao_referer_id');
1✔
112
        }
113

114
        return $this->router->generate(
1✔
115
            $options['route'] ?? 'contao_backend',
1✔
116
            $params,
1✔
117
            $options['absoluteUrl'] ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH
1✔
118
        );
1✔
119
    }
120

121
    /**
122
     * @codeCoverageIgnore
123
     */
124
    public static function getSubscribedServices(): array
125
    {
126
        return [
127
            '?'.ContaoCsrfTokenManager::class,
128
            '?'.CsrfTokenManagerInterface::class,
129
        ];
130
    }
131
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc