/** * {@inheritdoc} */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, \Closure $next) { $cookies = $request->getCookieParams(); $outerID = null; if (isset($cookies[self::COOKIE])) { if ($this->store()->isStarted()) { $outerID = $this->store()->getID(); } //Mounting ID retrieved from cookies $this->store()->setID($cookies[self::COOKIE]); } $response = $next(); if (empty($this->store) && $this->container->hasInstance(SessionStore::class)) { //Store were started by itself $this->store = $this->container->get(SessionStore::class); } if (!empty($this->store) && ($this->store->isStarted() || $this->store->isDestroyed())) { $response = $this->setCookie($request, $response, $this->store, $cookies); } //Restoring original session, not super efficient operation if (!empty($outerID)) { $this->store->setID($outerID); } return $response; }