/** * Transforms the passed session instance into a JSON encoded string. If the data contains * objects, each of them will be serialized before store them to the persistence layer. * * @param \AppserverIo\Psr\Servlet\ServletSessionInterface $servletSession The session to be transformed * * @return string The JSON encoded session representation * @see \AppserverIo\Appserver\ServletEngine\SessionMarshaller::marshall() */ public function marshall(ServletSessionInterface $servletSession) { // create the stdClass (that can easy be transformed into an JSON object) $stdClass = new \stdClass(); // copy the values to the stdClass $stdClass->id = $servletSession->getId(); $stdClass->name = $servletSession->getName(); $stdClass->lifetime = $servletSession->getLifetime(); $stdClass->maximumAge = $servletSession->getMaximumAge(); $stdClass->domain = $servletSession->getDomain(); $stdClass->path = $servletSession->getPath(); $stdClass->secure = $servletSession->isSecure(); $stdClass->httpOnly = $servletSession->isHttpOnly(); // initialize the array for the session data $stdClass->data = array(); // append the session data foreach (get_object_vars($servletSession->data) as $key => $value) { $stdClass->data[$key] = serialize($value); } // returns the JSON encoded session instance return json_encode($stdClass); }
/** * Saves the passed session to the persistence layer. * * @param \AppserverIo\Psr\Servlet\ServletSessionInterface $session The session to save * * @return void * @throws AppserverIo\Appserver\ServletEngine\SessionCanNotBeSavedException Is thrown if the session can't be saved */ public function save(ServletSessionInterface $session) { // don't save the session if it has been destroyed if ($session->getId() == null) { return; } // update the checksum and the file that stores the session data if (apc_store($session->getId(), $this->marshall($session)) === false) { throw new SessionCanNotBeSavedException(sprintf('Session with ID %s can\'t be saved')); } }
/** * Attaches the passed session to the manager and returns the instance. * If a session * with the session identifier already exists, it will be overwritten. * * @param \AppserverIo\Psr\Servlet\ServletSessionInterface $session The session to attach * * @return void */ public function attach(ServletSessionInterface $session) { // load session ID $id = $session->getId(); // register checksum + session $this->getSessions()->set($id, $session); }
/** * Saves the passed session to the persistence layer. * * @param \AppserverIo\Psr\Servlet\ServletSessionInterface $session The session to save * * @return void * @throws AppserverIo\Appserver\ServletEngine\SessionCanNotBeSavedException Is thrown if the session can't be saved */ public function save(ServletSessionInterface $session) { // don't save the session if it has been destroyed if ($session->getId() == null) { return; } // prepare the session filename $sessionFilename = $this->getSessionSavePath($this->getSessionSettings()->getSessionFilePrefix() . $session->getId()); // update the checksum and the file that stores the session data if (file_put_contents($sessionFilename, $this->marshall($session)) === false) { throw new SessionCanNotBeSavedException(sprintf('Session with ID %s can\'t be saved')); } }