Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00017 final class SessionNotStartedException extends BaseException
00018 {
00019 public function __construct()
00020 {
00021 return
00022 parent::__construct(
00023 'start session before assign or access session variables'
00024 );
00025 }
00026 }
00027
00033 final class Session extends StaticFactory
00034 {
00035 private static $isStarted = false;
00036
00037 public static function start()
00038 {
00039 session_start();
00040 self::$isStarted = true;
00041 }
00042
00046 public static function destroy()
00047 {
00048 if (!self::$isStarted)
00049 throw new SessionNotStartedException();
00050
00051 self::$isStarted = false;
00052
00053 try {
00054 session_destroy();
00055 } catch (BaseException $e) {
00056
00057 }
00058
00059 setcookie(session_name(), null, 0, '/');
00060 }
00061
00062 public static function flush()
00063 {
00064 return session_unset();
00065 }
00066
00070 public static function assign($var, $val)
00071 {
00072 if (!self::isStarted())
00073 throw new SessionNotStartedException();
00074
00075 $_SESSION[$var] = $val;
00076 }
00077
00082 public static function exist()
00083 {
00084 if (!self::isStarted())
00085 throw new SessionNotStartedException();
00086
00087 if (!func_num_args())
00088 throw new WrongArgumentException('missing argument(s)');
00089
00090 foreach (func_get_args() as $arg) {
00091 if (!isset($_SESSION[$arg]))
00092 return false;
00093 }
00094
00095 return true;
00096 }
00097
00101 public static function get($var)
00102 {
00103 if (!self::isStarted())
00104 throw new SessionNotStartedException();
00105
00106 return isset($_SESSION[$var]) ? $_SESSION[$var] : null;
00107 }
00108
00109 public static function &getAll()
00110 {
00111 return $_SESSION;
00112 }
00113
00118 public static function drop()
00119 {
00120 if (!self::isStarted())
00121 throw new SessionNotStartedException();
00122
00123 if (!func_num_args())
00124 throw new WrongArgumentException('missing argument(s)');
00125
00126 foreach (func_get_args() as $arg)
00127 unset($_SESSION[$arg]);
00128 }
00129
00133 public static function dropAll()
00134 {
00135 if (!self::isStarted())
00136 throw new SessionNotStartedException();
00137
00138 if ($_SESSION) {
00139 foreach (array_keys($_SESSION) as $key) {
00140 self::drop($key);
00141 }
00142 }
00143 }
00144
00145 public static function isStarted()
00146 {
00147 return self::$isStarted;
00148 }
00149
00153 public static function arrayAssign($scope, $array)
00154 {
00155 Assert::isArray($array);
00156
00157 foreach ($array as $var) {
00158 if (isset($scope[$var])) {
00159 $_SESSION[$var] = $scope[$var];
00160 }
00161 }
00162 }
00163
00167 public static function getName()
00168 {
00169 if (!self::isStarted())
00170 throw new SessionNotStartedException();
00171
00172 return session_name();
00173 }
00174
00178 public static function getId()
00179 {
00180 if (!self::isStarted())
00181 throw new SessionNotStartedException();
00182
00183 return session_id();
00184 }
00185 }
00186 ?>