jablonka.czprosek.czf

freenet-router

Subversion Repositories:
[/] [trunk/] [freenet-router/] [var/] [www/] [freenet-router/] [Framework/] [Environment/] [Session.php] - Rev 2

Compare with Previous - Blame - Download


<?php

namespace Phem\Environment;

use Phem\Core\Object;

/**
 * Class representing the session
 * 
 * @author Jakub PetrŞílka <petrzilka@czweb.net>
 */
class Session extends Object
{

    private $writeLock = false;

    public function init()
    {
        /* Start session if not started already (nested application) */
        if (session_status() != PHP_SESSION_ACTIVE)
        {
            session_start();
        }
    }

    public function isWriteLocked()
    {
        return $this->writeLock;
    }

    /**
     * Returns a variable from session array
     * 
     * @param string $name Name of the variable
     * @param mixed $defaultValue The default value which is used if session variable with given name is not set
     * @return mixed Value of the variable
     */
    function getVar($name, $defaultValue = null)
    {
        if (isset($_SESSION[$name]))
            return $_SESSION[$name];
        else
            return $defaultValue;
    }

    /**
     * Sets the session variable to the given value
     * 
     * @param string $name Name of the variable
     * @param mixed $value The value
     */
    function setVar($name, $value)
    {
        if (!$this->writeLock)
            $_SESSION[$name] = $value;
        else
            throw new SessionException('Trying to write session variable ' . $name . ' after writing closed');
    }

    /**
     * Destroys all data registered in session
     */
    function destroy()
    {
        session_destroy();
    }

    function getId()
    {
        return session_id();
    }

    function writeClose()
    {
        $this->writeLock = true;
        session_write_close();
    }

    function reopen()
    {
        ini_set('session.use_only_cookies', false);
        ini_set('session.use_cookies', false);
        ini_set('session.use_trans_sid', false);
        ini_set('session.cache_limiter', null);
        session_start();
        $this->writeLock = false;
    }

}

Powered by WebSVN 2.2.1