Session.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: Session.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00018     final class SessionNotStartedException extends BaseException
00019     {
00020         public function __construct()
00021         {
00022             return
00023                 parent::__construct(
00024                     'start session before assign or access session variables'
00025                 );
00026         }
00027     }
00028     
00034     final class Session extends StaticFactory
00035     {
00036         private static $isStarted = false;
00037         
00038         public static function start()
00039         {
00040             session_start();
00041             self::$isStarted = true;
00042         }
00043         
00047         /* void */ public static function destroy()
00048         {
00049             if (!self::$isStarted)
00050                 throw new SessionNotStartedException();
00051             
00052             self::$isStarted = false;
00053             
00054             try {
00055                 session_destroy();
00056             } catch (BaseException $e) {
00057                 // stfu
00058             }
00059             
00060             setcookie(session_name(), null, 0, '/');
00061         }
00062         
00063         public static function flush()
00064         {
00065             return session_unset();
00066         }
00067         
00071         /* void */ public static function assign($var, $val)
00072         {
00073             if (!self::isStarted())
00074                 throw new SessionNotStartedException();
00075             
00076             $_SESSION[$var] = $val;
00077         }
00078         
00083         public static function exist(/* ... */)
00084         {
00085             if (!self::isStarted())
00086                 throw new SessionNotStartedException();
00087             
00088             if (!func_num_args())
00089                 throw new WrongArgumentException('missing argument(s)');
00090             
00091             foreach (func_get_args() as $arg) {
00092                 if (!isset($_SESSION[$arg]))
00093                     return false;
00094             }
00095             
00096             return true;
00097         }
00098         
00102         public static function get($var)
00103         {
00104             if (!self::isStarted())
00105                 throw new SessionNotStartedException();
00106             
00107             return isset($_SESSION[$var]) ? $_SESSION[$var] : null;
00108         }
00109         
00110         public static function &getAll()
00111         {
00112             return $_SESSION;
00113         }
00114         
00119         /* void */ public static function drop(/* ... */)
00120         {
00121             if (!self::isStarted())
00122                 throw new SessionNotStartedException();
00123             
00124             if (!func_num_args())
00125                 throw new WrongArgumentException('missing argument(s)');
00126             
00127             foreach (func_get_args() as $arg)
00128                 unset($_SESSION[$arg]);
00129         }
00130         
00134         /* void */ public static function dropAll()
00135         {
00136             if (!self::isStarted())
00137                 throw new SessionNotStartedException();
00138             
00139             if ($_SESSION) {
00140                 foreach (array_keys($_SESSION) as $key) {
00141                     self::drop($key);
00142                 }
00143             }
00144         }
00145         
00146         public static function isStarted()
00147         {
00148             return self::$isStarted;
00149         }
00150         
00154         /* void */ public static function arrayAssign(&$scope, $array)
00155         {
00156             Assert::isArray($array);
00157             
00158             foreach ($array as $var) {
00159                 if (isset($scope[$var])) {
00160                     $_SESSION[$var] = $scope[$var];
00161                 }
00162             }
00163         }
00164         
00168         public static function getName()
00169         {
00170             if (!self::isStarted())
00171                 throw new SessionNotStartedException();
00172             
00173             return session_name();
00174         }
00175         
00179         public static function getId()
00180         {
00181             if (!self::isStarted())
00182                 throw new SessionNotStartedException();
00183             
00184             return session_id();
00185         }
00186     }
00187 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4