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

heimrichhannot / contao-utils-bundle / 15180859911

22 May 2025 07:36AM UTC coverage: 26.142% (+0.2%) from 25.941%
15180859911

Pull #104

github

koertho
make RoutingUtil::generateBackendRoute signature compatible with v2
Pull Request #104: Make RoutingUtil::generateBackendRoute() signature compatible with v3

18 of 20 new or added lines in 1 file covered. (90.0%)

1568 of 5998 relevant lines covered (26.14%)

1.55 hits per line

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

94.87
/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 = []): 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✔
NEW
76
            throw new \InvalidArgumentException('Fourth parameter must be a string or an array.');
×
77
        }
78

79
        // support legacy method signature
80
        if (func_num_args() > 4) {
1✔
81
            trigger_deprecation(
1✔
82
                'heimrichhannot/contao-utils-bundle',
1✔
83
                '2.244.0',
1✔
84
                'Passing more than four parameters is deprecated. Use an array as fourth parameter with the key "route" instead.'
1✔
85
            );
1✔
86
            $oldOptions = func_get_arg(4);
1✔
87
            if (is_array($oldOptions)) {
1✔
88
                $options = array_merge($options, $oldOptions);
1✔
89
            } else {
NEW
90
                throw new \InvalidArgumentException('Fifth parameter must be an array.');
×
91
            }
92
        }
93

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

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

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

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

119
    /**
120
     * @codeCoverageIgnore
121
     */
122
    public static function getSubscribedServices(): array
123
    {
124
        return [
125
            '?'.ContaoCsrfTokenManager::class,
126
            '?'.CsrfTokenManagerInterface::class,
127
        ];
128
    }
129
}
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

© 2025 Coveralls, Inc