View source
<?php
namespace Drupal\globalredirect\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\AliasManager;
use Drupal\Core\Routing\MatchingRouteNotFoundException;
use Drupal\Core\Url;
use Drupal\globalredirect\RedirectChecker;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\RequestContext;
class GlobalredirectSubscriber implements EventSubscriberInterface {
protected $config;
protected $aliasManager;
protected $languageManager;
protected $moduleHandler;
protected $entityManager;
protected $redirectChecker;
protected $context;
protected $urlGenerator;
public function __construct(ConfigFactoryInterface $config_factory, AliasManager $alias_manager, LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager, RedirectChecker $redirect_checker, RequestContext $context) {
$this->config = $config_factory
->get('globalredirect.settings');
$this->aliasManager = $alias_manager;
$this->languageManager = $language_manager;
$this->moduleHandler = $module_handler;
$this->entityManager = $entity_manager;
$this->redirectChecker = $redirect_checker;
$this->context = $context;
}
public function globalredirectCleanUrls(GetResponseEvent $event) {
if (!$this->config
->get('nonclean_to_clean') || $event
->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
return;
}
$request = $event
->getRequest();
$uri = $request
->getUri();
if (strpos($uri, 'index.php')) {
$url = str_replace('/index.php', '', $uri);
$event
->setResponse(new RedirectResponse($url, 301));
}
}
public function globalredirectDeslash(GetResponseEvent $event) {
if (!$this->config
->get('deslash') || $event
->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
return;
}
$path_info = ltrim($event
->getRequest()
->getPathInfo(), '/');
if (substr($path_info, -1, 1) === '/') {
$path_info = trim($path_info, '/');
try {
$path_info = $this->aliasManager
->getPathByAlias($path_info);
$this
->setResponse($event, Url::fromUri('internal:/' . $path_info));
} catch (MatchingRouteNotFoundException $e) {
}
}
}
public function globalredirectFrontPage(GetResponseEvent $event) {
if (!$this->config
->get('frontpage_redirect') || $event
->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
return;
}
$request = $event
->getRequest();
$path = trim($request
->getPathInfo(), '/');
if ($this
->isFrontPage($path)) {
$this
->setResponse($event, Url::fromRoute('<front>'));
}
}
public function globalredirectNormalizeAliases(GetResponseEvent $event) {
if ($event
->getRequestType() != HttpKernelInterface::MASTER_REQUEST || !$this->config
->get('normalize_aliases') || !($path = trim($event
->getRequest()
->getPathInfo(), '/'))) {
return;
}
$system_path = $this->aliasManager
->getPathByAlias($path);
$alias = $this->aliasManager
->getAliasByPath($system_path, $this->languageManager
->getCurrentLanguage()
->getId());
if ($alias != $path) {
if ($url = \Drupal::pathValidator()
->getUrlIfValid($alias)) {
$this
->setResponse($event, $url);
}
}
}
public function globalredirectForum(GetResponseEvent $event) {
$request = $event
->getRequest();
if ($event
->getRequestType() != HttpKernelInterface::MASTER_REQUEST || !$this->config
->get('term_path_handler') || !$this->moduleHandler
->moduleExists('forum') || !preg_match('/taxonomy\\/term\\/([0-9]+)$/', $request
->getUri(), $matches)) {
return;
}
$term = $this->entityManager
->getStorage('taxonomy_term')
->load($matches[1]);
if (!empty($term) && $term
->url() != $request
->getPathInfo()) {
$this
->setResponse($event, Url::fromUri('entity:taxonomy_term/' . $term
->id()));
}
}
protected function setResponse(GetResponseEvent $event, Url $url) {
$request = $event
->getRequest();
$this->context
->fromRequest($request);
parse_str($request
->getQueryString(), $query);
$url
->setOption('query', $query);
$url
->setAbsolute(TRUE);
if (!$url
->isRouted() || $this->redirectChecker
->canRedirect($url
->getRouteName(), $request)) {
$headers = [
'X-Drupal-Cache-Tags' => 'rendered',
];
$event
->setResponse(new RedirectResponse($url
->toString(), 301, $headers));
}
}
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array(
'globalredirectCleanUrls',
33,
);
$events[KernelEvents::REQUEST][] = array(
'globalredirectDeslash',
34,
);
$events[KernelEvents::REQUEST][] = array(
'globalredirectFrontPage',
35,
);
$events[KernelEvents::REQUEST][] = array(
'globalredirectNormalizeAliases',
36,
);
$events[KernelEvents::REQUEST][] = array(
'globalredirectForum',
37,
);
return $events;
}
protected function isFrontPage($path) {
$front = \Drupal::config('system.site')
->get('page.front');
$alias_path = \Drupal::service('path.alias_manager')
->getPathByAlias($path);
return !empty($path) && ($path == $front || $alias_path == $front);
}
}