DomainRedirectEventSubscriber.php in Domain 301 Redirect 8
File
src/EventSubscriber/DomainRedirectEventSubscriber.php
View source
<?php
namespace Drupal\domain_301_redirect\EventSubscriber;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\domain_301_redirect\Domain301Redirect;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\KernelEvents;
class DomainRedirectEventSubscriber implements EventSubscriberInterface {
protected $config;
protected $request;
protected $userAccount;
protected $pathMatcher;
protected $pathAliasManager;
public function __construct(ConfigFactoryInterface $config_factory, RequestStack $request_stack, AccountProxyInterface $user_account, PathMatcherInterface $path_matcher, AliasManagerInterface $pathAliasManager) {
$this->config = $config_factory
->get('domain_301_redirect.settings');
$this->request = $request_stack
->getCurrentRequest();
$this->userAccount = $user_account;
$this->pathMatcher = $path_matcher;
$this->pathAliasManager = $pathAliasManager;
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST] = [
'requestHandler',
34,
];
return $events;
}
public function requestHandler(GetResponseEvent $event) {
if (!$this->config
->get('enabled')) {
return;
}
if ($this->userAccount
->hasPermission('bypass domain 301 redirect')) {
return;
}
$domain = trim(trim($this->config
->get('domain')), '/');
if (empty($domain)) {
return;
}
if ($this
->checkPath()) {
return;
}
if (!preg_match('|^https?://|', $domain)) {
$domain = 'http://' . $domain;
}
$domain_parts = parse_url($domain);
$parsed_domain = $domain_parts['host'];
$parsed_domain .= !empty($domain_parts['port']) ? ':' . $domain_parts['port'] : '';
$parsed_scheme = $domain_parts['scheme'];
$scheme = $this->request
->getScheme();
$host = $this->request
->getHttpHost();
if ($parsed_domain != $host || $parsed_scheme != $scheme) {
$uri = $this->request
->getRequestUri();
$response = new TrustedRedirectResponse($domain . $uri, 301);
$response->headers
->add([
'X-Redirect-ID' => 0,
]);
$response
->addCacheableDependency(CacheableMetadata::createFromRenderArray([
'#cache' => [
'max-age' => Cache::PERMANENT,
'contexts' => [
'url',
'user.permissions',
],
'tags' => [
'config:domain_301_redirect.settings',
],
],
]));
$event
->setResponse($response);
}
}
private function checkPath() {
$current_path = $this->request
->getRequestUri();
$path = $this->pathAliasManager
->getAliasByPath($current_path);
$path_match = FALSE;
$bypass = FALSE;
if ($this->pathMatcher
->matchPath($path, $this->config
->get('pages'))) {
$path_match = TRUE;
}
switch ($this->config
->get('applicability')) {
case Domain301Redirect::EXCLUDE_METHOD:
if ($path_match) {
$bypass = TRUE;
}
break;
case Domain301Redirect::INCLUDE_METHOD:
if (!$path_match) {
$bypass = TRUE;
}
break;
}
return $bypass;
}
}