Skip to content

Commit

Permalink
migrate routing util
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Sep 19, 2022
1 parent 8f343d6 commit f1c46e0
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/Util/Routing/RoutingUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* Copyright (c) 2022 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\UtilsBundle\Util\Routing;

use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

class RoutingUtil implements ServiceSubscriberInterface
{
/**
* @var RouterInterface
*/
private $router;
/**
* @var ContainerInterface
*/
private $container;
/**
* @var string
*/
private $csrfTokenName;
/**
* @var RequestStack
*/
private $requestStack;

public function __construct(ContainerInterface $container, RouterInterface $router, string $csrfTokenName, RequestStack $requestStack)
{
$this->router = $router;
$this->container = $container;
$this->csrfTokenName = $csrfTokenName;
$this->requestStack = $requestStack;
}

/**
* Generate a backend route with token and referer.
*
* @param array $params Url-Parameters
*
* @return string The backend route url
*/
public function generateBackendRoute(array $params = [], bool $addToken = true, bool $addReferer = true, string $route = 'contao_backend')
{
if ($addToken) {
// >= contao 4.6.8 uses contao.csrf.token_manager service to validate token
if ($this->container->has('contao.csrf.token_manager')) {
$params['rt'] = $this->container->get('contao.csrf.token_manager')->getToken($this->csrfTokenName)->getValue();
} else {
$params['rt'] = $this->container->get('security.csrf.token_manager')->getToken($this->csrfTokenName)->getValue();
}
}

if ($addReferer && ($request = $this->requestStack->getCurrentRequest())) {
$params['ref'] = $request->get('_contao_referer_id');
}

return $this->router->generate($route, $params);
}

public static function getSubscribedServices()
{
return [
'?contao.csrf.token_manager',
'?security.csrf.token_manager',
];
}
}

0 comments on commit f1c46e0

Please sign in to comment.