Skip to content

Commit b508d98

Browse files
Merge pull request #6 from M4nu/host-locale-match
Fix host locale uri matching
2 parents 83214a9 + 592d307 commit b508d98

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

DependencyInjection/Compiler/OverrideRouteBasepathsCompilerPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace M4nu\MultiDomainBundle\DependencyInjection\Compiler;
33

4+
use M4nu\MultiDomainBundle\Doctrine\Phpcr\HostCandidates;
45
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
56
use Symfony\Component\DependencyInjection\ContainerBuilder;
67

@@ -28,7 +29,10 @@ public function process(ContainerBuilder $container)
2829

2930
$container
3031
->getDefinition('cmf_routing.phpcr_candidates_prefix')
32+
->setClass(HostCandidates::class)
3133
->replaceArgument(0, $routeBasePathsWithLocale)
34+
->replaceArgument(1, $domains)
35+
->setArgument(4, $routeBasePaths)
3236
;
3337
}
3438
}

Doctrine/Phpcr/HostCandidates.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace M4nu\MultiDomainBundle\Doctrine\Phpcr;
4+
5+
use Doctrine\Persistence\ManagerRegistry;
6+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\PrefixCandidates;
7+
use Symfony\Component\HttpFoundation\Request;
8+
9+
/**
10+
* Host based strategy.
11+
*/
12+
class HostCandidates extends PrefixCandidates
13+
{
14+
/**
15+
* @var array
16+
*/
17+
protected $domains = [];
18+
19+
/**
20+
* @var array
21+
*/
22+
protected $routeBasepaths = [];
23+
24+
public function __construct(array $prefixes, array $domains = [], ManagerRegistry $doctrine = null, $limit = 20, array $routeBasepaths)
25+
{
26+
parent::__construct($prefixes, array_keys($domains), $doctrine, $limit);
27+
$this->domains = $domains;
28+
$this->routeBasepaths = $routeBasepaths;
29+
}
30+
31+
public function getCandidates(Request $request)
32+
{
33+
$candidates = parent::getCandidates($request);
34+
$host = $request->getHost();
35+
$locale = array_search($host, $this->domains);
36+
37+
foreach ($candidates as $key => $candidate) {
38+
foreach ($this->routeBasepaths as $routeBasePath) {
39+
if (0 === strpos($candidate, $routeBasePath.'/'.$locale)) {
40+
continue;
41+
}
42+
43+
unset($candidates[$key]);
44+
}
45+
}
46+
47+
return $candidates;
48+
}
49+
}

0 commit comments

Comments
 (0)