|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FH\Bundle\MultiSiteBundle\Router; |
|
6
|
|
|
|
|
7
|
|
|
use FH\Bundle\MultiSiteBundle\Site\IdentifiedSiteInterface; |
|
8
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
9
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Joris van de Sande <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
final class PathIdentifiedUrlGenerator implements UrlGeneratorInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private $requestContext; |
|
17
|
|
|
private $routeParameter; |
|
18
|
|
|
private $defaultIdentifier; |
|
19
|
|
|
private $urlGenerator; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(UrlGeneratorInterface $urlGenerator, string $routeParameter = 'site', ?string $defaultIdentifier = null) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->routeParameter = $routeParameter; |
|
24
|
|
|
$this->defaultIdentifier = $defaultIdentifier; |
|
25
|
|
|
$this->urlGenerator = $urlGenerator; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function setContext(RequestContext $context): void |
|
29
|
|
|
{ |
|
30
|
|
|
$this->requestContext = $context; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getContext(): RequestContext |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->requestContext ?? new RequestContext(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH): string |
|
39
|
|
|
{ |
|
40
|
|
|
if (isset($parameters['site'])) { |
|
41
|
|
|
if ($parameters['site'] instanceof IdentifiedSiteInterface) { |
|
42
|
|
|
/** @var IdentifiedSiteInterface $site */ |
|
43
|
|
|
$site = $parameters['site']; |
|
44
|
|
|
$identifier = $site->getIdentifier(); |
|
45
|
|
|
} elseif (\is_string($parameters['site'])) { |
|
46
|
|
|
$identifier = $parameters['site']; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (isset($identifier)) { |
|
51
|
|
|
unset($parameters['site']); |
|
52
|
|
|
$parameters[$this->routeParameter] = $identifier === $this->defaultIdentifier ? null : $identifier; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->urlGenerator->generate($name, $parameters, $referenceType); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|