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

[NodeBundle] Improve url helper performance #3474

Open
wants to merge 1 commit into
base: 7.x
Choose a base branch
from
Open
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
90 changes: 47 additions & 43 deletions src/Kunstmaan/NodeBundle/Helper/URLHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down