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 all 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
7 changes: 7 additions & 0 deletions UPGRADE-7.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ PagePartBundle
--------------

- Not passing a HasPagePartsInterface as second parameter in PagePartEvent is deprecated and will be required in 8.0.


NodeBundle
-----------------

- The node-bundle/multidomain-bundle has now some improved logic in the router itself. Using the old router logic is deprecated and the new will be the default in 8.0.
To enable the new and improved router, set the `kunstmaan_node.enable_improved_router` config to `true`.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->end()
->end()
->end();

Expand Down
73 changes: 33 additions & 40 deletions src/Kunstmaan/MultiDomainBundle/Router/DomainBasedLocaleRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@

class DomainBasedLocaleRouter extends SlugRouter
{
/** @var RouteCollection */
protected $routeCollectionMultiLanguage;
dannyvw marked this conversation as resolved.
Show resolved Hide resolved

/**
* @var array|null
* @var RouteCollection
*
* @deprecated routeCollectionMultiLanguage property is deprecated in 7.2 and will be removed in 8.0. There is no replacement for this property.
*/
private $otherSite;
protected $routeCollectionMultiLanguage;

/**
* @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 +33,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 +44,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 +71,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 +90,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,15 +156,19 @@ 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();
if (!$this->enabledImprovedRouter) {
trigger_deprecation('kunstmaan/multidomain-bundle', '7.2', 'Not enabling the improved router is deprecated and the changed and improved router will be the default in 8.0. Set the "kunstmaan_node.enable_improved_router" config to true.');

$this->addMultiLangPreviewRoute();
$this->addMultiLangSlugRoute();
}
if (($this->otherSite && $this->isMultiLanguage($this->otherSite['host'])) || (!$this->otherSite && $this->isMultiLanguage())) {
if (!$this->routeCollectionMultiLanguage) {
$this->routeCollectionMultiLanguage = new RouteCollection();

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

return $this->routeCollectionMultiLanguage;
}
}

if (!$this->routeCollection) {
Expand All @@ -207,6 +195,8 @@ protected function addSlugRoute()
*/
protected function addMultiLangPreviewRoute()
dannyvw marked this conversation as resolved.
Show resolved Hide resolved
{
trigger_deprecation('kunstmaan/multidomain-bundle', '7.2', 'This method is deprecated and will be removed in 8.0.');

$routeParameters = $this->getPreviewRouteParameters();
$this->addMultiLangRoute('_slug_preview', $routeParameters);
}
Expand All @@ -216,6 +206,8 @@ protected function addMultiLangPreviewRoute()
*/
protected function addMultiLangSlugRoute()
{
trigger_deprecation('kunstmaan/multidomain-bundle', '7.2', 'This method is deprecated and will be removed in 8.0.');

$routeParameters = $this->getSlugRouteParameters();
$this->addMultiLangRoute('_slug', $routeParameters);
}
Expand All @@ -225,6 +217,8 @@ protected function addMultiLangSlugRoute()
*/
protected function addMultiLangRoute($name, array $parameters = [])
{
trigger_deprecation('kunstmaan/multidomain-bundle', '7.2', 'This method is deprecated and will be removed in 8.0.');

$this->routeCollectionMultiLanguage->add(
$name,
new Route(
Expand All @@ -247,26 +241,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
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function testAddMultiLangSlugRoute()
$request = $this->getRequest('http://singlelangdomain.tld/');

$object = new DomainBasedLocaleRouter($domainConfiguration, $this->getRequestStack($request), $this->getEntityManager(new NodeTranslation()), 'admin');
$object->enabledImprovedRouter(true);

$mirror = new \ReflectionClass(DomainBasedLocaleRouter::class);
$property = $mirror->getProperty('otherSite');
Expand Down Expand Up @@ -140,6 +141,7 @@ public function testGetRouteCollection()

/** @var Container $container */
$object = new DomainBasedLocaleRouter($domainConfiguration, $this->getRequestStack($request), $this->getEntityManager(new NodeTranslation()), 'admin');
$object->enabledImprovedRouter(true);
$mirror = new \ReflectionClass(DomainBasedLocaleRouter::class);
$property = $mirror->getProperty('otherSite');
$property->setAccessible(true);
Expand Down Expand Up @@ -216,6 +218,9 @@ private function getNodeTranslationRepository($nodeTranslation = null)

private function getDomainBasedLocaleRouter($request, $nodeTranslation = null)
{
return new DomainBasedLocaleRouter($this->getDomainConfiguration(), $this->getRequestStack($request), $this->getEntityManager($nodeTranslation), 'admin');
$router = new DomainBasedLocaleRouter($this->getDomainConfiguration(), $this->getRequestStack($request), $this->getEntityManager($nodeTranslation), 'admin');
$router->enabledImprovedRouter(true);

return $router;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->booleanNode('enabled')->defaultFalse()->end()
->end()
->end()
->booleanNode('enable_improved_router')->defaultFalse()->end()
->end();

return $treeBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public function load(array $configs, ContainerBuilder $container): void

$loader->load('services.yml');
$loader->load('commands.yml');

$enableImprovedRouter = $config['enable_improved_router'] ?? false;
if (!$enableImprovedRouter) {
trigger_deprecation('kunstmaan/node-bundle', '7.2', 'Not setting the "kunstmaan_node.enable_improved_router" config to true is deprecated, it will always be true in 8.0.');
}
$slugRouter = $container->findDefinition('kunstmaan_node.slugrouter');
$slugRouter->addMethodCall('enabledImprovedRouter', [$enableImprovedRouter]);
}

public function prepend(ContainerBuilder $container): void
Expand Down
25 changes: 21 additions & 4 deletions src/Kunstmaan/NodeBundle/Router/SlugRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class SlugRouter implements RouterInterface
/** @var string */
protected $slugPattern;

/**
* NEXT_MAJOR: Remove property
*
* @internal
*/
protected bool $enabledImprovedRouter = false;

public function __construct(
DomainConfigurationInterface $domainConfiguration,
RequestStack $requestStack,
Expand Down Expand Up @@ -182,16 +189,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 +220,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 Expand Up @@ -341,4 +348,14 @@ protected function getEscapedLocales($locales)

return implode('|', $escapedLocales);
}

/**
* NEXT_MAJOR: Remove method
*
* @interal
*/
public function enabledImprovedRouter(bool $enabled): void
{
$this->enabledImprovedRouter = $enabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function testConfigGeneratesAsExpected()
'threshold' => 35,
],
'enable_permissions' => true,
'enable_improved_router' => false,
];

$this->assertProcessedConfigurationEquals([$array], $array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Kunstmaan\NodeBundle\DependencyInjection\KunstmaanNodeExtension;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;

class KunstmaanNodeExtensionTest extends AbstractExtensionTestCase
{
use ExpectDeprecationTrait;

/**
* @return ExtensionInterface[]
*/
Expand All @@ -19,7 +22,7 @@ protected function getContainerExtensions(): array
public function testCorrectParametersHaveBeenSet()
{
$this->container->setParameter('twig.form.resources', []);
$this->load();
$this->load(['enable_improved_router' => true]);

$this->assertContainerBuilderHasParameter('twig.form.resources');
$this->assertContainerBuilderHasParameter('kunstmaan_node.show_add_homepage', true);
Expand All @@ -30,4 +33,14 @@ public function testCorrectParametersHaveBeenSet()
$this->assertContainerBuilderHasParameter('kunstmaan_node.version_timeout', 3600);
$this->assertContainerBuilderHasParameter('kunstmaan_node.url_chooser.lazy_increment', 2);
}

/**
* @group legacy
*/
public function testImprovedRouterConfigDeprecation()
{
$this->expectDeprecation('Since kunstmaan/node-bundle 7.2: Not setting the "kunstmaan_node.enable_improved_router" config to true is deprecated, it will always be true in 8.0.');
$this->container->setParameter('twig.form.resources', []);
$this->load();
}
}