freshheads /
FHMultiSiteBundle
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace FH\Bundle\MultiSiteBundle\Site; |
||
| 6 | |||
| 7 | use Symfony\Component\Routing\RequestContext; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @author Joris van de Sande <[email protected]> |
||
| 11 | */ |
||
| 12 | final class PrefixedPathIdentifiedSiteResolver implements SiteResolverInterface |
||
| 13 | { |
||
| 14 | private $siteRepository; |
||
| 15 | private $defaultIdentifier; |
||
| 16 | private $identifiers; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param string[] $identifiers |
||
| 20 | */ |
||
| 21 | public function __construct(SiteRepositoryInterface $siteRepository, array $identifiers, ?string $defaultIdentifier = null) |
||
| 22 | { |
||
| 23 | $this->siteRepository = $siteRepository; |
||
| 24 | $this->defaultIdentifier = $defaultIdentifier; |
||
| 25 | $this->identifiers = $identifiers; |
||
| 26 | } |
||
| 27 | |||
| 28 | public function resolve(RequestContext $requestContext): SiteInterface |
||
| 29 | { |
||
| 30 | $matches = []; |
||
| 31 | |||
| 32 | if (preg_match('#^/([^/]+)/?#', $requestContext->getPathInfo(), $matches) > 0) { |
||
| 33 | $identifier = $matches[1]; |
||
| 34 | } else { |
||
| 35 | $identifier = $this->defaultIdentifier; |
||
| 36 | } |
||
| 37 | |||
| 38 | if (empty($identifier)) { |
||
| 39 | throw SiteNotFoundException::withRequestContext($requestContext); |
||
| 40 | } |
||
| 41 | |||
| 42 | foreach ($this->siteRepository->findAll() as $site) { |
||
| 43 | if (!$site instanceof IdentifiedSiteInterface) { |
||
| 44 | continue; |
||
| 45 | } |
||
| 46 | |||
| 47 | if ($site->matches($identifier)) { |
||
| 48 | return $site; |
||
| 49 | } |
||
| 50 | |||
| 51 | if ($site->matches($this->defaultIdentifier)) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 52 | $defaultSite = $site; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | if (!empty($defaultSite) && $defaultSite instanceof IdentifiedSiteInterface) { |
||
| 57 | return $defaultSite; |
||
| 58 | } |
||
| 59 | |||
| 60 | throw SiteNotFoundException::withRequestContext($requestContext); |
||
| 61 | } |
||
| 62 | } |
||
| 63 |