From 7167e5a653f0734e87b06c0f36b2d6d1ca7dc5ee Mon Sep 17 00:00:00 2001 From: Danny van Wijk Date: Thu, 21 Nov 2024 12:42:11 +0100 Subject: [PATCH] Improve url helper performance --- src/Kunstmaan/NodeBundle/Helper/URLHelper.php | 90 ++++++++++--------- 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/src/Kunstmaan/NodeBundle/Helper/URLHelper.php b/src/Kunstmaan/NodeBundle/Helper/URLHelper.php index 743950261f..14b78efebe 100644 --- a/src/Kunstmaan/NodeBundle/Helper/URLHelper.php +++ b/src/Kunstmaan/NodeBundle/Helper/URLHelper.php @@ -63,56 +63,60 @@ public function replaceUrl($text) } if ($this->isInternalLink($text)) { - preg_match_all("/\[(([a-z_A-Z\.]+):)?NT([0-9]+)\]/", $text, $matches, PREG_SET_ORDER); - - if (\count($matches) > 0) { - foreach ($matches as $match) { - $fullTag = $match[0]; - $hostId = $match[2]; - - $hostConfig = !empty($hostId) ? $this->domainConfiguration->getFullHostById($hostId) : null; - $host = null !== $hostConfig && array_key_exists('host', $hostConfig) ? $hostConfig['host'] : null; - $hostBaseUrl = $this->domainConfiguration->getHostBaseUrl($host); - - $nodeTranslationId = $match[3]; - $nodeTranslation = $this->getNodeTranslation($nodeTranslationId); - - if ($nodeTranslation) { - $urlParams = ['url' => $nodeTranslation['url']]; - // Only add locale if multilingual site - if ($this->domainConfiguration->isMultiLanguage($host)) { - $urlParams['_locale'] = $nodeTranslation['lang']; - } - - // Only add other site, when having this. - if ($hostId) { - $urlParams['otherSite'] = $hostId; - } - - $url = $this->router->generate('_slug', $urlParams); - - $text = str_replace($fullTag, $hostId ? $hostBaseUrl . $url : $url, $text); - } else { - $this->logger->error('No NodeTranslation found in the database when replacing url tag ' . $fullTag); - } + preg_match_all('/\[(([a-z_A-Z\.]+):)?NT([0-9]+)\]/', $text, $matches, PREG_SET_ORDER); + + foreach ($matches as $match) { + $fullTag = $match[0]; + $isSchemaLink = empty($match[3]); + + $nodeTranslationId = $isSchemaLink ? $match[6] : $match[3]; + $nodeTranslation = $this->getNodeTranslation($nodeTranslationId); + if (!$nodeTranslation) { + $this->logger->error('No NodeTranslation found in the database when replacing url tag ' . $fullTag); + + continue; + } + + $hostId = $isSchemaLink ? $match[5] : $match[2]; + + $hostConfig = !empty($hostId) ? $this->domainConfiguration->getFullHostById($hostId) : null; + $host = $hostConfig['host'] ?? null; + if (!$host) { + // Reduce multiple getHost calls + $host = $this->domainConfiguration->getHost(); + } + + $hostBaseUrl = $this->domainConfiguration->getHostBaseUrl($host); + + $urlParams = ['url' => $nodeTranslation['url']]; + // Only add locale if multilingual site + if ($this->domainConfiguration->isMultiLanguage($host)) { + $urlParams['_locale'] = $nodeTranslation['lang']; + } + + // Only add other site, when having this. + if ($hostId) { + $urlParams['otherSite'] = $hostId; } + + $url = $this->router->generate('_slug', $urlParams); + + $text = str_replace($fullTag, $hostId ? $hostBaseUrl . $url : $url, $text); } } if ($this->isInternalMediaLink($text)) { preg_match_all("/\[(([a-z_A-Z]+):)?M([0-9]+)\]/", $text, $matches, PREG_SET_ORDER); - if (\count($matches) > 0) { - foreach ($matches as $match) { - $fullTag = $match[0]; - $mediaId = $match[3]; - - $mediaItem = $this->getMedia($mediaId); - if ($mediaItem) { - $text = str_replace($fullTag, $mediaItem['url'], $text); - } else { - $this->logger->error('No Media found in the database when replacing url tag ' . $fullTag); - } + foreach ($matches as $match) { + $fullTag = $match[0]; + $mediaId = $match[3]; + + $mediaItem = $this->getMedia($mediaId); + if ($mediaItem) { + $text = str_replace($fullTag, $mediaItem['url'], $text); + } else { + $this->logger->error('No Media found in the database when replacing url tag ' . $fullTag); } } }