Session.php 1.29 KB
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 14/12/26
 * Time: 上午12:39
 */

namespace Hood;

use Hood\Core\Session\SessionNamespace;
use Hood\Core\Session\CacheSession;

class Session
{

    /**
     * 开启Session并设置NameSpace
     * @param $namespace
     * @return SessionNamespace
     */
    static public function start($namespace = 'session_default')
    {
        if (self::isSessionStarted() == false) {
            $_session = new CacheSession();
            session_set_save_handler(
                array(&$_session, "open"),
                array(&$_session, "close"),
                array(&$_session, "read"),
                array(&$_session, "write"),
                array(&$_session, "destroy"),
                array(&$_session, "gc")
            );
            session_start();
        }
        return new SessionNamespace ($namespace);
    }

    /**
     * 检查Session是否开启
     * @return bool
     */
    static private function isSessionStarted()
    {
        if (php_sapi_name() !== 'cli') {
            if (version_compare(phpversion(), '5.4.0', '>=')) {
                return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
            } else {
                return session_id() === '' ? FALSE : TRUE;
            }
        }
        return FALSE;
    }
}