Skip to content

Commit

Permalink
Improve domain based locale router performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Danny van Wijk committed Oct 8, 2024
1 parent 30e1afc commit 0274826
Showing 1 changed file with 1 addition and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class DomainBasedLocaleRouter extends SlugRouter
{
/** @var RouteCollection */
protected $routeCollectionMultiLanguage;

/**
* @var array|null
*/
Expand Down Expand Up @@ -172,17 +168,6 @@ private function getReverseLocaleMap(): array
*/
public function getRouteCollection(): RouteCollection
{
if (($this->otherSite && $this->isMultiLanguage($this->otherSite['host'])) || (!$this->otherSite && $this->isMultiLanguage())) {
if (!$this->routeCollectionMultiLanguage) {
$this->routeCollectionMultiLanguage = new RouteCollection();

$this->addMultiLangPreviewRoute();
$this->addMultiLangSlugRoute();
}

return $this->routeCollectionMultiLanguage;
}

if (!$this->routeCollection) {
$this->routeCollection = new RouteCollection();

Expand All @@ -202,39 +187,6 @@ protected function addSlugRoute()
$this->addRoute('_slug', $routeParameters);
}

/**
* Add the slug route to the route collection
*/
protected function addMultiLangPreviewRoute()
{
$routeParameters = $this->getPreviewRouteParameters();
$this->addMultiLangRoute('_slug_preview', $routeParameters);
}

/**
* Add the slug route to the route collection multilanguage
*/
protected function addMultiLangSlugRoute()
{
$routeParameters = $this->getSlugRouteParameters();
$this->addMultiLangRoute('_slug', $routeParameters);
}

/**
* @param string $name
*/
protected function addMultiLangRoute($name, array $parameters = [])
{
$this->routeCollectionMultiLanguage->add(
$name,
new Route(
$parameters['path'],
$parameters['defaults'],
$parameters['requirements']
)
);
}

/**
* Return slug route parameters
*
Expand All @@ -258,7 +210,7 @@ protected function getSlugRouteParameters()
// If other site provided and multilingual, get the locales from the host config.
if ($this->otherSite && $this->isMultiLanguage($this->otherSite['host'])) {
$locales = $this->getHostLocales();
} elseif ($this->isMultiLanguage() && !$this->otherSite) {
} elseif (!$this->otherSite && $this->isMultiLanguage()) {
$locales = $this->getFrontendLocales();
}

Expand Down

0 comments on commit 0274826

Please sign in to comment.