You may access the currently active session in userland code. In order to do this,
inject TYPO3\Flow\Session\SessionInterface and NOT just TYPO3\Flow\Session\Session.
The former will be a unique instance (singleton) representing the current session
while the latter would be a completely new session instance!
You can use the Session Manager for accessing sessions which are not currently
active.
/** * Initializes the session instance from the passed JSON string. If the encoded * data contains objects, they will be unserialized before reattached to the * session instance. * * @param string $marshalled The marshaled session representation * * @return \AppserverIo\Psr\Servlet\ServletSessionInterface The un-marshaled servlet session instance */ protected function unmarshall($marshalled) { // create a new and empty servlet session instance $servletSession = Session::emptyInstance(); // unmarshall the session data $this->getSessionMarshaller()->unmarshall($servletSession, $marshalled); // returns the initialized servlet session instance return $servletSession; }
/** * This is invoked on every iteration of the daemons while() loop. * * @param integer $timeout The timeout before the daemon wakes up * * @return void */ public function iterate($timeout) { // call parent method and sleep for the default timeout parent::iterate($timeout); // create sessions and add them to the pool $this->synchronized(function ($self) { // check the method we want to invoke switch ($self->action) { // we want to create a new session instance case SessionFactory::ACTION_NEXT_FROM_POOL: $self->uniqueId = uniqid(); $self->sessionPool->set($self->uniqueId, Session::emptyInstance()); $self->sessionAvailable = true; // send a notification that method invocation has been processed $self->notify(); break; // we want to remove a session instance from the pool // we want to remove a session instance from the pool case SessionFactory::ACTION_REMOVE_BY_SESSION_ID: foreach ($self->sessionPool as $uniqueId => $session) { if ($session instanceof ServletSessionInterface && $session->getId() === $self->sessionId) { $self->sessionPool->remove($uniqueId); } } break; // do nothing, because we've an unknown action // do nothing, because we've an unknown action default: break; } // reset the action $self->action = null; }, $this); // profile the size of the sessions if ($this->profileLogger) { $this->profileLogger->debug(sprintf('Size of session pool is: %d', sizeof($this->sessionPool))); } }
/** * Creates a new session with the passed session ID and session name if given. * * @param mixed $id The session ID * @param string $name The session name * @param integer|\DateTime $lifetime Date and time after the session expires * @param integer|null $maximumAge Number of seconds until the session expires * @param string|null $domain The host to which the user agent will send this cookie * @param string $path The path describing the scope of this cookie * @param boolean $secure If this cookie should only be sent through a "secure" channel by the user agent * @param boolean $httpOnly If this cookie should only be used through the HTTP protocol * * @return \AppserverIo\Psr\Servlet\ServletSessionInterface The requested session */ public function create($id, $name, $lifetime = null, $maximumAge = null, $domain = null, $path = null, $secure = null, $httpOnly = null) { // copy the default session configuration for lifetime from the settings if ($lifetime == null) { $lifetime = time() + $this->getSessionSettings()->getSessionCookieLifetime(); } // copy the default session configuration for maximum from the settings if ($maximumAge == null) { $maximumAge = $this->getSessionSettings()->getSessionMaximumAge(); } // copy the default session configuration for cookie domain from the settings if ($domain == null) { $domain = $this->getSessionSettings()->getSessionCookieDomain(); } // copy the default session configuration for the cookie path from the settings if ($path == null) { $path = $this->getSessionSettings()->getSessionCookiePath(); } // copy the default session configuration for the secure flag from the settings if ($secure == null) { $secure = $this->getSessionSettings()->getSessionCookieSecure(); } // copy the default session configuration for the http only flag from the settings if ($httpOnly == null) { $httpOnly = $this->getSessionSettings()->getSessionCookieHttpOnly(); } // initialize and return the session instance $session = Session::emptyInstance(); $session->init($id, $name, $lifetime, $maximumAge, $domain, $path, $secure, $httpOnly); // attach the session to the manager $this->attach($session); // return the session return $session; }