CookieMonster.php in Bakery Single Sign-On System 8.2
File
src/EventSubscriber/CookieMonster.php
View source
<?php
namespace Drupal\bakery\EventSubscriber;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class CookieMonster implements EventSubscriberInterface {
protected $time;
protected $config;
protected $cookieJar;
private $freshness;
private $domain;
private $secure;
public function __construct(TimeInterface $time, ConfigFactoryInterface $config_factory, ParameterBag $cookie_jar) {
$this->time = $time;
$this->config = $config_factory
->get('bakery.settings');
$this->cookieJar = $cookie_jar;
$this->domain = $this->config
->get('bakery_domain');
$this->freshness = (int) $this->config
->get('bakery_freshness');
$this->secure = !empty(ini_get('session.cookie_secure'));
}
public function meWantCookie(ResponseEvent $event) {
$response = $event
->getResponse();
$this->cookieJar
->all();
if ($this->cookieJar
->count()) {
foreach ($this->cookieJar
->all() as $name => $cookie) {
if (empty($cookie)) {
$response->headers
->clearCookie($name, '/', '', $this->secure);
$response->headers
->clearCookie($name, '/', $this->domain, $this->secure);
}
else {
$response->headers
->setCookie(Cookie::create($name, $cookie, $this->time
->getRequestTime() + $this->freshness, '/', $this->domain, $this->secure));
}
}
}
}
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = [
'meWantCookie',
-1000,
];
return $events;
}
}