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 FH\Bundle\MultiSiteBundle\Site\IdentifierMappingInterface; |
||
9 | use Symfony\Component\HttpFoundation\Request; |
||
10 | use Symfony\Component\HttpFoundation\RequestStack; |
||
11 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
12 | use Symfony\Component\Routing\RequestContext; |
||
13 | |||
14 | /** |
||
15 | * @author Joris van de Sande <[email protected]> |
||
16 | */ |
||
17 | final class HostnameIdentifiedUrlGenerator implements UrlGeneratorInterface |
||
18 | { |
||
19 | private $urlGenerator; |
||
20 | private $requestContext; |
||
21 | private $identifierMapping; |
||
22 | private $requestStack; |
||
23 | |||
24 | public function __construct(UrlGeneratorInterface $urlGenerator, IdentifierMappingInterface $identifierMapping, RequestStack $requestStack) |
||
25 | { |
||
26 | $this->urlGenerator = $urlGenerator; |
||
27 | $this->identifierMapping = $identifierMapping; |
||
28 | $this->requestStack = $requestStack; |
||
29 | } |
||
30 | |||
31 | public function setContext(RequestContext $context): void |
||
32 | { |
||
33 | $this->requestContext = $context; |
||
34 | } |
||
35 | |||
36 | public function getContext(): RequestContext |
||
37 | { |
||
38 | return $this->requestContext ?? new RequestContext(); |
||
39 | } |
||
40 | |||
41 | public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH): string |
||
42 | { |
||
43 | $identifier = $this->resolveIdentifier($parameters); |
||
44 | |||
45 | if (isset($identifier)) { |
||
46 | $hostnames = $this->identifierMapping->findHostnamesByIdentifier($identifier); |
||
47 | |||
48 | if (\count($hostnames) > 0) { |
||
49 | $parameters['site_hostname'] = $hostnames[0]; |
||
50 | } |
||
51 | |||
52 | unset($parameters['site']); |
||
53 | } |
||
54 | |||
55 | return $this->urlGenerator->generate($name, $parameters, $referenceType); |
||
56 | } |
||
57 | |||
58 | private function resolveIdentifier(array $parameters): ?string |
||
59 | { |
||
60 | if (isset($parameters['site'])) { |
||
61 | if ($parameters['site'] instanceof IdentifiedSiteInterface) { |
||
62 | /** @var IdentifiedSiteInterface $site */ |
||
63 | $site = $parameters['site']; |
||
64 | |||
65 | return $site->getIdentifier(); |
||
66 | } |
||
67 | |||
68 | if (\is_string($parameters['site'])) { |
||
69 | return $parameters['site']; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | $request = method_exists($this->requestStack, 'getMainRequest') ? $this->requestStack->getMainRequest() : $this->requestStack->getMasterRequest(); |
||
0 ignored issues
–
show
|
|||
74 | |||
75 | if ($request instanceof Request) { |
||
76 | $site = $request->attributes->get('site'); |
||
77 | |||
78 | if ($site instanceof IdentifiedSiteInterface) { |
||
79 | return $site->getIdentifier(); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | return null; |
||
84 | } |
||
85 | } |
||
86 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.