3 namespace Drupal\redirect;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Database\Connection;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Language\Language;
9 use Drupal\redirect\Entity\Redirect;
10 use Drupal\redirect\Exception\RedirectLoopException;
12 class RedirectRepository {
15 * @var \Drupal\Core\Entity\EntityManagerInterface
20 * @var \Drupal\Core\Database\Connection
22 protected $connection;
25 * @var \Drupal\Core\Config\ImmutableConfig
30 * An array of found redirect IDs to avoid recursion.
34 protected $foundRedirects = [];
37 * Constructs a \Drupal\redirect\EventSubscriber\RedirectRequestSubscriber object.
39 * @param \Drupal\Core\Entity\EntityManagerInterface $manager
40 * The entity manager service.
41 * @param \Drupal\Core\Database\Connection $connection
42 * The database connection.
44 public function __construct(EntityManagerInterface $manager, Connection $connection, ConfigFactoryInterface $config_factory) {
45 $this->manager = $manager;
46 $this->connection = $connection;
47 $this->config = $config_factory->get('redirect.settings');
51 * Gets a redirect for given path, query and language.
53 * @param string $source_path
54 * The redirect source path.
56 * The redirect source path query.
58 * The language for which is the redirect.
60 * @return \Drupal\redirect\Entity\Redirect
61 * The matched redirect entity.
63 * @throws \Drupal\redirect\Exception\RedirectLoopException
65 public function findMatchingRedirect($source_path, array $query = [], $language = Language::LANGCODE_NOT_SPECIFIED) {
66 $hashes = [Redirect::generateHash($source_path, $query, $language)];
67 if ($language != Language::LANGCODE_NOT_SPECIFIED) {
68 $hashes[] = Redirect::generateHash($source_path, $query, Language::LANGCODE_NOT_SPECIFIED);
71 // Add a hash without the query string if using passthrough querystrings.
72 if (!empty($query) && $this->config->get('passthrough_querystring')) {
73 $hashes[] = Redirect::generateHash($source_path, [], $language);
74 if ($language != Language::LANGCODE_NOT_SPECIFIED) {
75 $hashes[] = Redirect::generateHash($source_path, [], Language::LANGCODE_NOT_SPECIFIED);
79 // Load redirects by hash. A direct query is used to improve performance.
80 $rid = $this->connection->query('SELECT rid FROM {redirect} WHERE hash IN (:hashes[]) ORDER BY LENGTH(redirect_source__query) DESC', [':hashes[]' => $hashes])->fetchField();
83 // Check if this is a loop.
84 if (in_array($rid, $this->foundRedirects)) {
85 throw new RedirectLoopException('/' . $source_path, $rid);
87 $this->foundRedirects[] = $rid;
89 $redirect = $this->load($rid);
91 // Find chained redirects.
92 if ($recursive = $this->findByRedirect($redirect, $language)) {
93 // Reset found redirects.
94 $this->foundRedirects = [];
105 * Helper function to find recursive redirects.
107 * @param \Drupal\redirect\Entity\Redirect
108 * The redirect object.
109 * @param string $language
110 * The language to use.
112 protected function findByRedirect(Redirect $redirect, $language) {
113 $uri = $redirect->getRedirectUrl();
114 $baseUrl = \Drupal::request()->getBaseUrl();
115 $path = ltrim(substr($uri->toString(), strlen($baseUrl)), '/');
116 $query = $uri->getOption('query') ?: [];
117 return $this->findMatchingRedirect($path, $query, $language);
121 * Finds redirects based on the source path.
123 * @param string $source_path
124 * The redirect source path (without the query).
126 * @return \Drupal\redirect\Entity\Redirect[]
127 * Array of redirect entities.
129 public function findBySourcePath($source_path) {
130 $ids = $this->manager->getStorage('redirect')->getQuery()
131 ->condition('redirect_source.path', $source_path, 'LIKE')
133 return $this->manager->getStorage('redirect')->loadMultiple($ids);
137 * Load redirect entity by id.
139 * @param int $redirect_id
142 * @return \Drupal\redirect\Entity\Redirect
144 public function load($redirect_id) {
145 return $this->manager->getStorage('redirect')->load($redirect_id);
149 * Loads multiple redirect entities.
151 * @param array $redirect_ids
152 * Redirect ids to load.
154 * @return \Drupal\redirect\Entity\Redirect[]
155 * List of redirect entities.
157 public function loadMultiple(array $redirect_ids = NULL) {
158 return $this->manager->getStorage('redirect')->loadMultiple($redirect_ids);