f7cd1416ae101907769db7c98d04ce4ba7369945
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_404 / src / EventSubscriber / Redirect404Subscriber.php
1 <?php
2
3 namespace Drupal\redirect_404\EventSubscriber;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Language\LanguageManagerInterface;
7 use Drupal\Core\Path\PathMatcherInterface;
8 use Drupal\redirect_404\RedirectNotFoundStorageInterface;
9 use Drupal\Core\Path\CurrentPathStack;
10 use Symfony\Component\HttpFoundation\RequestStack;
11 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
12 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13 use Symfony\Component\HttpKernel\KernelEvents;
14 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
16 /**
17  * An EventSubscriber that listens to redirect 404 errors.
18  */
19 class Redirect404Subscriber implements EventSubscriberInterface {
20
21   /**
22    * The current path.
23    *
24    * @var \Drupal\Core\Path\CurrentPathStack
25    */
26   protected $currentPath;
27
28   /**
29    * The path matcher.
30    *
31    * @var \Drupal\Core\Path\PathMatcherInterface
32    */
33   protected $pathMatcher;
34
35   /**
36    * The request stack (get the URL argument(s) and combined it with the path).
37    *
38    * @var \Symfony\Component\HttpFoundation\RequestStack
39    */
40   protected $requestStack;
41
42   /**
43    * The language manager.
44    *
45    * @var \Drupal\Core\Language\LanguageManagerInterface
46    */
47   protected $languageManager;
48
49   /**
50    * The redirect storage.
51    *
52    * @var \Drupal\redirect_404\RedirectNotFoundStorageInterface
53    */
54   protected $redirectStorage;
55
56   /**
57    * The configuration factory.
58    *
59    * @var \Drupal\Core\Config\ConfigFactoryInterface
60    */
61   protected $config;
62
63   /**
64    * Constructs a new Redirect404Subscriber.
65    *
66    * @param \Drupal\Core\Path\CurrentPathStack $current_path
67    *   The current path.
68    * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
69    *   The path matcher service.
70    * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
71    *   The request stack.
72    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
73    *   The language manager.
74    * @param \Drupal\redirect_404\RedirectNotFoundStorageInterface $redirect_storage
75    *   A redirect storage.
76    * @param \Drupal\Core\Config\ConfigFactoryInterface $config
77    *   The configuration factory.
78    */
79   public function __construct(CurrentPathStack $current_path, PathMatcherInterface $path_matcher, RequestStack $request_stack, LanguageManagerInterface $language_manager, RedirectNotFoundStorageInterface $redirect_storage, ConfigFactoryInterface $config) {
80     $this->currentPath = $current_path;
81     $this->pathMatcher = $path_matcher;
82     $this->requestStack = $request_stack;
83     $this->languageManager = $language_manager;
84     $this->redirectStorage = $redirect_storage;
85     $this->config = $config->get('redirect_404.settings');
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public static function getSubscribedEvents() {
92     $events[KernelEvents::EXCEPTION][] = 'onKernelException';
93     return $events;
94   }
95
96   /**
97    * Logs an exception of 404 Redirect errors.
98    *
99    * @param GetResponseForExceptionEvent $event
100    *   Is given by the event dispatcher.
101    */
102   public function onKernelException(GetResponseForExceptionEvent $event) {
103     // Only log page not found (404) errors.
104     if ($event->getException() instanceof NotFoundHttpException) {
105       $path = $this->currentPath->getPath();
106
107       // Ignore paths specified in the redirect settings.
108       if ($pages = mb_strtolower($this->config->get('pages'))) {
109         // Do not trim a trailing slash if that is the complete path.
110         $path_to_match = $path === '/' ? $path : rtrim($path, '/');
111
112         if ($this->pathMatcher->matchPath(mb_strtolower($path_to_match), $pages)) {
113           return;
114         }
115       }
116
117       // Allow to store paths with arguments.
118       if ($query_string = $this->requestStack->getCurrentRequest()->getQueryString()) {
119         $query_string = '?' . $query_string;
120       }
121       $path .= $query_string;
122       $langcode = $this->languageManager->getCurrentLanguage()->getId();
123
124       // Write record.
125       $this->redirectStorage->logRequest($path, $langcode);
126     }
127   }
128
129 }