Version 1
[yaffs-website] / web / modules / contrib / redirect / src / RedirectRepository.php
1 <?php
2
3 namespace Drupal\redirect;
4
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;
11
12 class RedirectRepository {
13
14   /**
15    * @var \Drupal\Core\Entity\EntityManagerInterface
16    */
17   protected $manager;
18
19   /**
20    * @var \Drupal\Core\Database\Connection
21    */
22   protected $connection;
23
24   /**
25    * @var \Drupal\Core\Config\ImmutableConfig
26    */
27   protected $config;
28
29   /**
30    * An array of found redirect IDs to avoid recursion.
31    *
32    * @var array
33    */
34   protected $foundRedirects = [];
35
36   /**
37    * Constructs a \Drupal\redirect\EventSubscriber\RedirectRequestSubscriber object.
38    *
39    * @param \Drupal\Core\Entity\EntityManagerInterface $manager
40    *   The entity manager service.
41    * @param \Drupal\Core\Database\Connection $connection
42    *   The database connection.
43    */
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');
48   }
49
50   /**
51    * Gets a redirect for given path, query and language.
52    *
53    * @param string $source_path
54    *   The redirect source path.
55    * @param array $query
56    *   The redirect source path query.
57    * @param $language
58    *   The language for which is the redirect.
59    *
60    * @return \Drupal\redirect\Entity\Redirect
61    *   The matched redirect entity.
62    *
63    * @throws \Drupal\redirect\Exception\RedirectLoopException
64    */
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);
69     }
70
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);
76       }
77     }
78
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();
81
82     if (!empty($rid)) {
83       // Check if this is a loop.
84       if (in_array($rid, $this->foundRedirects)) {
85         throw new RedirectLoopException('/' . $source_path, $rid);
86       }
87       $this->foundRedirects[] = $rid;
88
89       $redirect = $this->load($rid);
90
91       // Find chained redirects.
92       if ($recursive = $this->findByRedirect($redirect, $language)) {
93         // Reset found redirects.
94         $this->foundRedirects = [];
95         return $recursive;
96       }
97
98       return $redirect;
99     }
100
101     return NULL;
102   }
103
104   /**
105    * Helper function to find recursive redirects.
106    *
107    * @param \Drupal\redirect\Entity\Redirect
108    *   The redirect object.
109    * @param string $language
110    *   The language to use.
111    */
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);
118   }
119
120   /**
121    * Finds redirects based on the source path.
122    *
123    * @param string $source_path
124    *   The redirect source path (without the query).
125    *
126    * @return \Drupal\redirect\Entity\Redirect[]
127    *   Array of redirect entities.
128    */
129   public function findBySourcePath($source_path) {
130     $ids = $this->manager->getStorage('redirect')->getQuery()
131       ->condition('redirect_source.path', $source_path, 'LIKE')
132       ->execute();
133     return $this->manager->getStorage('redirect')->loadMultiple($ids);
134   }
135
136   /**
137    * Load redirect entity by id.
138    *
139    * @param int $redirect_id
140    *   The redirect id.
141    *
142    * @return \Drupal\redirect\Entity\Redirect
143    */
144   public function load($redirect_id) {
145     return $this->manager->getStorage('redirect')->load($redirect_id);
146   }
147
148   /**
149    * Loads multiple redirect entities.
150    *
151    * @param array $redirect_ids
152    *   Redirect ids to load.
153    *
154    * @return \Drupal\redirect\Entity\Redirect[]
155    *   List of redirect entities.
156    */
157   public function loadMultiple(array $redirect_ids = NULL) {
158     return $this->manager->getStorage('redirect')->loadMultiple($redirect_ids);
159   }
160 }