Skip to content

Commit 83214a9

Browse files
Merge pull request #5 from M4nu/router-listener
Router listener
2 parents a8a4ca5 + 44d71ec commit 83214a9

7 files changed

Lines changed: 92 additions & 94 deletions

File tree

DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public function getConfigTreeBuilder()
2626
->isRequired()
2727
->prototype('scalar')->end()
2828
->end()
29+
->scalarNode('excluded_paths')
30+
->defaultNull()
31+
->info('Regex of excluded paths. Ex : ^/api')
32+
->end()
2933
->arrayNode('persistence')
3034
->addDefaultsIfNotSet()
3135
->children()

DependencyInjection/M4nuMultiDomainExtension.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@ public function load(array $configs, ContainerBuilder $container)
2222
$configuration = new Configuration();
2323
$config = $this->processConfiguration($configuration, $configs);
2424

25-
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26-
$loader->load('services.xml');
27-
28-
$container->setParameter($this->getAlias() . '.domains', $config['domains']);
29-
3025
if ($config['persistence']['phpcr']['enabled']) {
31-
$container->setParameter($this->getAlias() . '.backend_type_phpcr', true);
26+
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
27+
$loader->load('services.xml');
3228

33-
$loader->load('phpcr.xml');
29+
$container->setParameter($this->getAlias() . '.domains', $config['domains']);
30+
$container->setParameter($this->getAlias() . '.excluded_paths', $config['excluded_paths']);
31+
$container->setParameter($this->getAlias() . '.backend_type_phpcr', true);
3432
}
3533
}
3634
}

EventListener/LocaleListener.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
<?php
22
namespace M4nu\MultiDomainBundle\EventListener;
33

4-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
4+
use Symfony\Component\HttpKernel\Event\RequestEvent;
55
use Symfony\Component\HttpKernel\HttpKernelInterface;
66

77
class LocaleListener
88
{
99
private $domains;
1010

11-
public function __construct(array $domains)
11+
private $excludedPaths;
12+
13+
public function __construct(array $domains, ?string $excludedPaths)
1214
{
1315
$this->domains = $domains;
16+
$this->excludedPaths = $excludedPaths;
1417
}
1518

16-
public function onKernelRequest(GetResponseEvent $event)
19+
public function onKernelRequest(RequestEvent $event)
1720
{
1821
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
1922
return;
2023
}
2124

2225
$request = $event->getRequest();
26+
27+
if (null !== $this->excludedPaths && preg_match('#'.$this->excludedPaths.'#', $request->getPathInfo())) {
28+
return;
29+
}
30+
2331
$host = $request->getHost();
2432

2533
if (false !== $locale = array_search($host, $this->domains)) {
2634
$request->setLocale($locale);
35+
$request->attributes->set('_locale', $locale);
2736
}
2837
}
2938
}

EventListener/RouteListener.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

EventListener/RouterListener.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace M4nu\MultiDomainBundle\EventListener;
3+
4+
use Symfony\Cmf\Component\Routing\Event\RouterGenerateEvent;
5+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
6+
use Symfony\Component\Routing\RouterInterface;
7+
8+
class RouterListener
9+
{
10+
private $router;
11+
12+
private $routeBasePaths;
13+
14+
private $domains;
15+
16+
public function __construct(RouterInterface $router, array $routeBasePaths, array $domains)
17+
{
18+
$this->router = $router;
19+
$this->routeBasePaths = $routeBasePaths;
20+
$this->domains = $domains;
21+
}
22+
23+
public function onGenerate(RouterGenerateEvent $event)
24+
{
25+
// If _locale parameter exists, use corresponding domain and force absolute URL
26+
if ($locale = $event->getParameters()['_locale'] ?? null) {
27+
$domain = $this->domains[$locale];
28+
29+
$event->setReferenceType(UrlGeneratorInterface::ABSOLUTE_URL);
30+
$this->updateRouterContext($domain);
31+
32+
return;
33+
}
34+
35+
// Else, search what is current route locale
36+
$route = $event->getRoute();
37+
38+
if (!is_string($route)) {
39+
return;
40+
}
41+
42+
foreach ($this->routeBasePaths as $routeBasePath) {
43+
foreach ($this->domains as $locale => $domain) {
44+
if (0 === strpos($route, sprintf('%s/%s', $routeBasePath, $locale))) {
45+
$this->updateRouterContext($domain);
46+
$event->setParameter('_locale', $locale);
47+
48+
return;
49+
}
50+
}
51+
}
52+
}
53+
54+
private function updateRouterContext(string $host): void
55+
{
56+
$this->router
57+
->getContext()
58+
->setHost($host)
59+
;
60+
}
61+
}

Resources/config/phpcr.xml

Lines changed: 0 additions & 20 deletions
This file was deleted.

Resources/config/services.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@
66

77
<parameters>
88
<parameter key="m4nu_multi_domain.locale_listener.class">M4nu\MultiDomainBundle\EventListener\LocaleListener</parameter>
9+
<parameter key="m4nu_multi_domain.router_listener.class">M4nu\MultiDomainBundle\EventListener\RouterListener</parameter>
910
</parameters>
1011

1112
<services>
1213
<service id="m4nu_multi_domain.locale_listener" class="%m4nu_multi_domain.locale_listener.class%">
13-
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" />
14+
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="20" />
15+
<argument>%m4nu_multi_domain.domains%</argument>
16+
<argument>%m4nu_multi_domain.excluded_paths%</argument>
17+
</service>
18+
19+
<service id="m4nu_multi_domain.router_listener" class="%m4nu_multi_domain.router_listener.class%">
20+
<tag name="kernel.event_listener" event="cmf_routing.pre_dynamic_generate" method="onGenerate" />
21+
<argument type="service" id="router" />
22+
<argument>%cmf_routing.dynamic.persistence.phpcr.route_basepaths%</argument>
1423
<argument>%m4nu_multi_domain.domains%</argument>
1524
</service>
1625
</services>

0 commit comments

Comments
 (0)