Author: Tim Wagner (tw@appserver.io)
Inheritance: extends AppserverIo\Psr\Servlet\Http\HttpSessionWrapper
Exemple #1
0
 /**
  * Returns the session for this request.
  *
  * @param boolean $create TRUE to create a new session, else FALSE
  *
  * @return null|\AppserverIo\Psr\Servlet\Http\HttpSessionInterface The session instance
  *
  * @throws \Exception
  */
 public function getSession($create = false)
 {
     // load the proposed session-ID and name
     $id = $this->getProposedSessionId();
     $sessionName = $this->getRequestedSessionName();
     // if no session has already been load, initialize the session manager
     /** @var \AppserverIo\Appserver\ServletEngine\SessionManagerInterface $manager */
     $manager = $this->getSessionManager();
     // if no session manager was found, we don't support sessions
     if ($manager == null) {
         return;
     }
     // find or create a new session (if flag has been set)
     $session = $manager->find($id);
     // if we can't find a session or session has been expired and we want to create a new one
     if ($session == null && $create === true) {
         // check if a session ID has been specified
         if ($id == null) {
             // if not, generate a unique one
             $id = SessionUtils::generateRandomString();
         }
         // create a new session and register ID in request
         $session = $manager->create($id, $sessionName);
     }
     // if we can't find a session and we should NOT create one, return nothing
     if ($create === false && $session == null) {
         return;
     }
     // if we can't find a session although we SHOULD create one, we throw an exception
     if ($create === true && $session == null) {
         throw new \Exception('Can\'t create a new session!');
     }
     // initialize the session wrapper
     $wrapper = new SessionWrapper();
     $wrapper->injectSession($session);
     $wrapper->injectRequest($this);
     // return the found session
     return $wrapper;
 }