View source
<?php
namespace Drupal\multiversion\Redirect;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\State\StateInterface;
use Drupal\multiversion\Workspace\WorkspaceManagerInterface;
use Drupal\redirect\Entity\Redirect;
use Drupal\redirect\Exception\RedirectLoopException;
use Drupal\redirect\RedirectRepository as ContribRedirectRepository;
class RedirectRepository extends ContribRedirectRepository {
private $workspaceManager;
private $state;
public function __construct(EntityManagerInterface $manager, Connection $connection, ConfigFactoryInterface $config_factory, WorkspaceManagerInterface $workspace_manager, StateInterface $state) {
parent::__construct($manager, $connection, $config_factory);
$this->workspaceManager = $workspace_manager;
$this->state = $state;
}
public function findMatchingRedirect($source_path, array $query = [], $language = Language::LANGCODE_NOT_SPECIFIED) {
$enabled = $this->state
->get('multiversion.migration_done.redirect', FALSE);
if (!$enabled) {
return parent::findMatchingRedirect($source_path, $query, $language);
}
$hashes = [
Redirect::generateHash($source_path, $query, $language),
];
if ($language != Language::LANGCODE_NOT_SPECIFIED) {
$hashes[] = Redirect::generateHash($source_path, $query, Language::LANGCODE_NOT_SPECIFIED);
}
if (!empty($query) && $this->config
->get('passthrough_querystring')) {
$hashes[] = Redirect::generateHash($source_path, [], $language);
if ($language != Language::LANGCODE_NOT_SPECIFIED) {
$hashes[] = Redirect::generateHash($source_path, [], Language::LANGCODE_NOT_SPECIFIED);
}
}
$rid = $this->connection
->query('SELECT rid FROM {redirect} WHERE hash IN (:hashes[]) AND workspace = :workspace AND _deleted != 1 ORDER BY LENGTH(redirect_source__query) DESC', [
':hashes[]' => $hashes,
':workspace' => $this->workspaceManager
->getActiveWorkspaceId(),
])
->fetchField();
if (!empty($rid)) {
if (in_array($rid, $this->foundRedirects)) {
throw new RedirectLoopException('/' . $source_path, $rid);
}
$this->foundRedirects[] = $rid;
$redirect = $this
->load($rid);
if (empty($redirect) || !$redirect instanceof Redirect) {
return NULL;
}
if ($recursive = $this
->findByRedirect($redirect, $language)) {
$this->foundRedirects = [];
return $recursive;
}
return $redirect;
}
return NULL;
}
}