View source
<?php
namespace Drupal\redirect;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language;
use Drupal\redirect\Entity\Redirect;
use Drupal\redirect\Exception\RedirectLoopException;
class RedirectRepository {
protected $manager;
protected $connection;
protected $config;
protected $foundRedirects = [];
public function __construct(EntityTypeManagerInterface $manager, Connection $connection, ConfigFactoryInterface $config_factory) {
$this->manager = $manager;
$this->connection = $connection;
$this->config = $config_factory
->get('redirect.settings');
}
public function findMatchingRedirect($source_path, array $query = [], $language = Language::LANGCODE_NOT_SPECIFIED) {
$source_path = ltrim($source_path, '/');
$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[]) ORDER BY LENGTH(redirect_source__query) DESC', [
':hashes[]' => $hashes,
])
->fetchField();
if (!empty($rid)) {
if (in_array($rid, $this->foundRedirects)) {
throw new RedirectLoopException('/' . $source_path, $rid);
}
$this->foundRedirects[] = $rid;
$redirect = $this
->load($rid);
if ($recursive = $this
->findByRedirect($redirect, $language)) {
$this->foundRedirects = [];
return $recursive;
}
return $redirect;
}
$this->foundRedirects = [];
return NULL;
}
protected function findByRedirect(Redirect $redirect, $language) {
$uri = $redirect
->getRedirectUrl();
$base_url = \Drupal::request()
->getBaseUrl();
$generated_url = $uri
->toString(TRUE);
$path = ltrim(substr($generated_url
->getGeneratedUrl(), strlen($base_url)), '/');
$query = $uri
->getOption('query') ?: [];
$return_value = $this
->findMatchingRedirect($path, $query, $language);
return $return_value ? $return_value
->addCacheableDependency($generated_url) : $return_value;
}
public function findBySourcePath($source_path) {
$ids = $this->manager
->getStorage('redirect')
->getQuery()
->condition('redirect_source.path', $source_path, 'LIKE')
->execute();
return $this->manager
->getStorage('redirect')
->loadMultiple($ids);
}
public function findByDestinationUri(array $destination_uri) {
$storage = $this->manager
->getStorage('redirect');
$ids = $storage
->getQuery()
->condition('redirect_redirect.uri', $destination_uri, 'IN')
->execute();
return $storage
->loadMultiple($ids);
}
public function load($redirect_id) {
return $this->manager
->getStorage('redirect')
->load($redirect_id);
}
public function loadMultiple(array $redirect_ids = NULL) {
return $this->manager
->getStorage('redirect')
->loadMultiple($redirect_ids);
}
}