Version 1
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_domain / src / EventSubscriber / DomainRedirectRequestSubscriber.php
1 <?php
2
3 namespace Drupal\redirect_domain\EventSubscriber;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Path\PathMatcherInterface;
7 use Drupal\Core\Routing\TrustedRedirectResponse;
8 use Drupal\Core\Url;
9 use Drupal\redirect\RedirectChecker;
10 use Symfony\Component\HttpKernel\KernelEvents;
11 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
12 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14 /**
15  * Redirect subscriber for controller requests.
16  */
17 class DomainRedirectRequestSubscriber implements EventSubscriberInterface {
18
19   /**
20    * @var \Drupal\redirect\RedirectChecker
21    */
22   protected $redirectChecker;
23
24   /**
25    * Domain redirect configuration.
26    *
27    * @var \Drupal\Core\Config\Config
28    */
29   protected $domainConfig;
30
31   /**
32    * The path matcher.
33    *
34    * @var \Drupal\Core\Path\PathMatcherInterface
35    */
36   protected $pathMatcher;
37
38   /**
39    * Redirect configuration.
40    *
41    * @var \Drupal\Core\Config\Config
42    */
43   protected  $redirectConfig;
44
45   /**
46    * Constructs a \Drupal\redirect\EventSubscriber\RedirectRequestSubscriber object.
47    *
48    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
49    *   The config factory.
50    * @param \Drupal\redirect\RedirectChecker $redirect_checker
51    *   The redirect checker service.
52    * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
53    *   The path matcher.
54    */
55   public function __construct(ConfigFactoryInterface $config_factory, RedirectChecker $redirect_checker, PathMatcherInterface $path_matcher) {
56     $this->domainConfig = $config_factory->get('redirect_domain.domains');
57     $this->redirectConfig = $config_factory->get('redirect.settings');
58     $this->redirectChecker = $redirect_checker;
59     $this->pathMatcher = $path_matcher;
60   }
61
62   /**
63    * Handles the domain redirect if any found.
64    *
65    * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
66    *   The event to process.
67    */
68   public function onKernelRequestCheckDomainRedirect(GetResponseEvent $event) {
69     $request = clone $event->getRequest();
70
71     if (!$this->redirectChecker->canRedirect($request)) {
72       return;
73     }
74
75     // Redirect between domains configuration.
76     $domains = $this->domainConfig->get('domain_redirects');
77     if (!empty($domains)) {
78       $host = $request->getHost();
79       $path = $request->getPathInfo();
80       $protocol = $request->getScheme() . '://';
81       $destination = NULL;
82
83       // Checks if there is a redirect domain in the configuration.
84       if (isset($domains[str_replace('.', ':', $host)])) {
85         foreach ($domains[str_replace('.', ':', $host)] as $item) {
86           if ($this->pathMatcher->matchPath($path, $item['sub_path'])) {
87             $destination = $item['destination'];
88             break;
89           }
90         }
91         if ($destination) {
92           // Use the default status code from Redirect.
93           $response = new TrustedRedirectResponse(
94             $protocol . $destination,
95             $this->redirectConfig->get('default_status_code')
96           );
97           $event->setResponse($response);
98           return;
99         }
100       }
101     }
102   }
103
104   /**
105    * Prior to set the response it check if we can redirect.
106    *
107    * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
108    *   The event object.
109    * @param \Drupal\Core\Url $url
110    *   The Url where we want to redirect.
111    */
112   protected function setResponse(GetResponseEvent $event, Url $url) {
113     $request = $event->getRequest();
114
115     parse_str($request->getQueryString(), $query);
116     $url->setOption('query', $query);
117     $url->setAbsolute(TRUE);
118   }
119
120   /**
121    * {@inheritdoc}
122    */
123   public static function getSubscribedEvents() {
124     // This needs to run before RouterListener::onKernelRequest(), which has
125     // a priority of 32 and
126     // RedirectRequestSubscriber::onKernelRequestCheckRedirect(), which has
127     // a priority of 33. Otherwise, that aborts the request if no matching
128     // route is found.
129     $events[KernelEvents::REQUEST][] = ['onKernelRequestCheckDomainRedirect', 34];
130     return $events;
131   }
132
133 }