Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MultidomainBundle] Improve domain based locale router performance #3444

Merged
merged 6 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 12 additions & 81 deletions src/Kunstmaan/MultiDomainBundle/Router/DomainBasedLocaleRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,12 @@
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;
dannyvw marked this conversation as resolved.
Show resolved Hide resolved

/**
* @var array|null
*/
private $otherSite;

/**
* @var array
*/
private $cachedNodeTranslations = [];
private ?array $otherSite = null;
dannyvw marked this conversation as resolved.
Show resolved Hide resolved
private array $cachedNodeTranslations = [];

/**
* Generate an url for a supplied route
Expand All @@ -36,7 +25,7 @@ class DomainBasedLocaleRouter extends SlugRouter
*/
public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
if ('_slug' === $name && $this->isMultiLanguage() && $this->isMultiDomainHost()) {
if ('_slug' === $name && $this->isMultiDomainHost() && $this->isMultiLanguage()) {
$locale = isset($parameters['_locale']) ? $parameters['_locale'] : $this->getRequestLocale();

$reverseLocaleMap = $this->getReverseLocaleMap();
Expand All @@ -47,8 +36,6 @@ public function generate($name, $parameters = [], $referenceType = UrlGeneratorI

if (isset($parameters['otherSite'])) {
$this->otherSite = $this->domainConfiguration->getFullHostById($parameters['otherSite']);
} else {
$this->otherSite = null;
}

$this->urlGenerator = new UrlGenerator(
Expand Down Expand Up @@ -76,13 +63,8 @@ public function match($pathinfo): array
$result = $urlMatcher->match($pathinfo);
if (!empty($result)) {
// Remap locale for front-end requests
if ($this->isMultiDomainHost()
&& $this->isMultiLanguage()
&& !$result['preview']
) {
$localeMap = $this->getLocaleMap();
$locale = $result['_locale'];
$result['_locale'] = $localeMap[$locale];
if (!$result['preview'] && $this->isMultiDomainHost() && $this->isMultiLanguage()) {
$result['_locale'] = $this->getLocaleMap()[$result['_locale']];
}

$nodeTranslation = $this->getNodeTranslation($result);
Expand All @@ -100,13 +82,7 @@ public function match($pathinfo): array
*/
protected function getRequestLocale()
{
$request = $this->getMasterRequest();
$locale = $this->getDefaultLocale();
if (!\is_null($request)) {
$locale = $request->getLocale();
}

return $locale;
return $this->getMasterRequest()?->getLocale() ?: $this->getDefaultLocale();
}

/**
Expand Down Expand Up @@ -172,17 +148,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 +167,6 @@ protected function addSlugRoute()
$this->addRoute('_slug', $routeParameters);
}

/**
* Add the slug route to the route collection
*/
protected function addMultiLangPreviewRoute()
dannyvw marked this conversation as resolved.
Show resolved Hide resolved
{
$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 @@ -247,26 +179,25 @@ protected function getSlugRouteParameters()
'_controller' => SlugController::class . '::slugAction',
'preview' => false,
'url' => '',
'_locale' => $this->getDefaultLocale(),
];
$slugRequirements = [
'url' => $this->getSlugPattern(),
];

$locales = [];

// If other site provided and multilingual, get the locales from the host config.
$locales = [];
if ($this->otherSite && $this->isMultiLanguage($this->otherSite['host'])) {
$locales = $this->getHostLocales();
} elseif ($this->isMultiLanguage() && !$this->otherSite) {
} elseif (!$this->otherSite && $this->isMultiLanguage()) {
$locales = $this->getFrontendLocales();
}

// Make locale availables for the slug routes.
// Make locale available for the slug routes.
if (!empty($locales)) {
$slugPath = '/{_locale}' . $slugPath;
unset($slugDefaults['_locale']);
$slugRequirements['_locale'] = $this->getEscapedLocales($this->getHostLocales());
$slugRequirements['_locale'] = $this->getEscapedLocales(!$this->otherSite ? $locales : $this->getHostLocales());
} else {
$slugDefaults['_locale'] = $this->getDefaultLocale();
}

return [
Expand Down
8 changes: 4 additions & 4 deletions src/Kunstmaan/NodeBundle/Router/SlugRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,16 @@ protected function getPreviewRouteParameters()
'_controller' => SlugController::class . '::slugAction',
'preview' => true,
'url' => '',
'_locale' => $this->getDefaultLocale(),
];
$previewRequirements = [
'url' => $this->getSlugPattern(),
];

if ($this->isMultiLanguage()) {
$previewPath = '/{_locale}' . $previewPath;
unset($previewDefaults['_locale']);
$previewRequirements['_locale'] = $this->getEscapedLocales($this->getBackendLocales());
} else {
$previewDefaults['_locale'] = $this->getDefaultLocale();
}

return [
Expand All @@ -213,16 +213,16 @@ protected function getSlugRouteParameters()
'_controller' => SlugController::class . '::slugAction',
'preview' => false,
'url' => '',
'_locale' => $this->getDefaultLocale(),
];
$slugRequirements = [
'url' => $this->getSlugPattern(),
];

if ($this->isMultiLanguage()) {
$slugPath = '/{_locale}' . $slugPath;
unset($slugDefaults['_locale']);
$slugRequirements['_locale'] = $this->getEscapedLocales($this->getFrontendLocales());
} else {
$slugDefaults['_locale'] = $this->getDefaultLocale();
}

return [
Expand Down