RedirectDestination.php in Bakery Single Sign-On System 8.2
Namespace
Drupal\bakeryFile
src/RedirectDestination.phpView source
<?php
namespace Drupal\bakery;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RequestStack;
class RedirectDestination implements RedirectDestinationInterface {
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* The URL generator.
*
* @var \Drupal\Core\Routing\UrlGeneratorInterface
*/
protected $urlGenerator;
/**
* Bakery settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* Destination service.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface
*/
protected $redirectDestination;
/**
* The destination used by the current request.
*
* @var \Drupal\Core\Url|string
*/
protected $destination;
/**
* Constructs a new RedirectDestination instance.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
* The URL generator.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Configuration factory.
* @param \Drupal\Core\Routing\RedirectDestinationInterface|null $destination
* Core destination redirect destination service.
*/
public function __construct(RequestStack $request_stack, UrlGeneratorInterface $url_generator, ConfigFactoryInterface $config_factory, RedirectDestinationInterface $destination = NULL) {
$this->requestStack = $request_stack;
$this->urlGenerator = $url_generator;
$this->config = $config_factory
->get('bakery.settings');
$this->redirectDestination = $destination ?? \Drupal::destination();
}
/**
* {@inheritdoc}
*/
public function get() {
if (!isset($this->destination)) {
if ($this->config
->get('bakery_is_master')) {
$this->destination = $this
->getChildDestination();
}
else {
$this->destination = $this
->getParentDestination();
}
}
if ($this->destination instanceof Url) {
return $this->destination
->toString();
}
return $this->destination;
}
/**
* Get link to destination on current site for parent.
*
* @return \Drupal\Core\Url
*/
private function getParentDestination() {
$query = $this->requestStack
->getCurrentRequest()->query;
if ($query
->has('bd')) {
// @TODO do something?
}
return Url::fromRoute('<current>')
->setAbsolute(TRUE);
}
/**
* Get link to destination on referring child.
*
* @return \Drupal\Core\Url
*/
private function getChildDestination() {
$query = $this->requestStack
->getCurrentRequest()->query;
if ($query
->has('bd')) {
$external_uri = $query
->get('bd');
if (is_string($external_uri) && ($external_uri = UrlHelper::stripDangerousProtocols($external_uri))) {
foreach ($this->config
->get('bakery_slaves') as $subsite) {
if (str_starts_with($external_uri, $subsite)) {
return Url::fromUri(urldecode($external_uri))
->setAbsolute(TRUE);
}
}
}
}
return Url::fromRoute('<front>')
->setAbsolute(TRUE);
}
/**
* {@inheritdoc}
*/
public function getAsArray() {
// TODO...
return [
'bd' => $this
->get(),
];
}
/**
* {@inheritdoc}
*/
public function set($new_destination) {
$this->destination = $new_destination;
return $this;
}
}
Classes
Name![]() |
Description |
---|---|
RedirectDestination |