← Back to team overview

anewt-developers team mailing list archive

[Branch ~uws/anewt/anewt.uws] Rev 1722: [session] Refactored AnewtSession class replacing old code

 

------------------------------------------------------------
revno: 1722
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Sun 2009-08-02 19:59:08 +0200
message:
  [session] Refactored AnewtSession class replacing old code
  
  Refactored the session code into a clean AnewtSession class
  that is up to modern Anewt standards ;)
modified:
  doc/manual/examples/session-usage.php
  session/main.lib.php
  session/module.doc.xml
  session/session.lib.php


--
lp:anewt
https://code.launchpad.net/~uws/anewt/anewt.uws

Your team Anewt developers is subscribed to branch lp:anewt.
To unsubscribe from this branch go to https://code.launchpad.net/~uws/anewt/anewt.uws/+edit-subscription.
=== modified file 'doc/manual/examples/session-usage.php'
--- doc/manual/examples/session-usage.php	2006-07-24 21:16:41 +0000
+++ doc/manual/examples/session-usage.php	2009-08-02 17:59:08 +0000
@@ -1,8 +1,8 @@
-// Start a new session, eg. after a succesful login attempt
-Session::init('yourapp');
-
-// Store some data in the session
-Session::set('username', $username);
-
-// Destroy the session, eg. on logout
-Session::destroy();
+/* Start a new session, eg. after a succesful login attempt */
+AnewtSession::init('yourapp');
+
+/* Store some data in the session */
+AnewtSession::set('username', $username);
+
+/* Destroy the session, eg. on logout */
+AnewtSession::destroy();

=== modified file 'session/main.lib.php'
--- session/main.lib.php	2006-05-16 07:08:18 +0000
+++ session/main.lib.php	2009-08-02 17:59:08 +0000
@@ -3,21 +3,8 @@
 /*
  * Anewt, Almost No Effort Web Toolkit, session module
  *
- * Copyright (C) 2004  Wouter Bolsterlee <uws@xxxxxxxxx>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA+
+ * This code is copyrighted and distributed under the terms of the GNU LGPL.
+ * See the README file for more information.
  */
 
 

=== modified file 'session/module.doc.xml'
--- session/module.doc.xml	2009-08-02 16:32:09 +0000
+++ session/module.doc.xml	2009-08-02 17:59:08 +0000
@@ -8,7 +8,7 @@
 	<anewt:subtitle>Module for handling sessions</anewt:subtitle>
 
 	<anewt:classes>
-		<anewt:class>Session</anewt:class>
+		<anewt:class>AnewtSession</anewt:class>
 	</anewt:classes>
 
 
@@ -17,42 +17,17 @@
 		<anewt:title>Overview</anewt:title>
 
 		<p>The session module provides you with an easy way to deal with sessions.
-			PHP offers a really ugly session API, mixing both functions and global
-			variables. Anewt offers a consistent, small wrapper API that hides the
-			internal details and automates some boring (and sometimes subtly annoying)
-			details through the <anewt:classref>Session</anewt:classref> class, which
-			only provides static methods. This means you cannot create session objects
-			by using the <code>new</code> operator, since only one session can be used
-			at any time.</p>
+		PHP offers a really ugly session API, mixing both functions and global
+		variables. Anewt offers a consistent, small wrapper API that hides the
+		internal details and automates some boring (and sometimes subtly annoying)
+		details through the <anewt:classref>AnewtSession</anewt:classref> class,
+		which only provides static methods. This means you cannot create session
+		objects by using the <code>new</code> operator, since only one session can
+		be used at any time.</p>
 
 		<p>The API is quite simple and resembles the API offered by the
-			<anewt:classref>AnewtContainer</anewt:classref> class, but a bit simpler:</p>
-
-		<dl>
-
-			<dt><code>Session::init($name, $timeout)</code></dt>
-			<dd>Initialize the session with the given name. The timeout value is
-				optional.</dd>
-
-			<dt><code>Session::destroy()</code></dt>
-			<dd>Destroy the session and all data associated to it.</dd>
-
-			<dt><code>Session::get($name)</code></dt>
-			<dd>Retrieve the value specified by the given name from the session data.</dd>
-
-			<dt><code>Session::set($name, $value)</code></dt>
-			<dd>Store a name/value pair in the session.</dd>
-
-			<dt><code>Session::delete($name)</code></dt>
-			<dt><code>Session::del($name)</code></dt>
-			<dd>Remove a name/value pair from the session data.</dd>
-
-			<dt><code>Session::is_set($name)</code></dt>
-			<dd>Check if name/value pair is available in the session data.</dd>
-
-		</dl>
-
-		<p>A simple code snippet to illustrate how to use sessions:</p>
+		<anewt:classref>AnewtContainer</anewt:classref> class, but a bit simpler.
+		This simple code snippet illustrates how to use sessions:</p>
 
 		<anewt:example src="session-usage">
 			<anewt:title>Using a session</anewt:title>

=== modified file 'session/session.lib.php'
--- session/session.lib.php	2008-01-29 13:01:05 +0000
+++ session/session.lib.php	2009-08-02 17:59:08 +0000
@@ -3,21 +3,8 @@
 /*
  * Anewt, Almost No Effort Web Toolkit, session module
  *
- * Copyright (C) 2004  Wouter Bolsterlee <uws@xxxxxxxxx>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA+
+ * This code is copyrighted and distributed under the terms of the GNU LGPL.
+ * See the README file for more information.
  */
 
 
@@ -28,15 +15,22 @@
 /**
  * The session class provides several static methods for session handling.
  */
-class Session {
+class AnewtSession
+{
+	/** \{
+	 * \name Methods to start and destroy sessions
+	 *
+	 * These methods will setup and teardown user sessions.
+	 */
 
 	/**
-	 * Initializes the session with the given name.
+	 * Initialize the session with the given name.
 	 *
 	 * \param $name The name this session should have.
 	 * \param $timeout The timeout value for this session.
 	 */
-	function init($name, $timeout=null) {
+	public static function init($name, $timeout=null)
+	{
 		assert('is_string($name)');
 
 		global $anewt_session_current_name;
@@ -44,10 +38,11 @@
 		/* Do not break when initializing the same session twice. Note that this
 		 * still throws a warning if the user tries to register a session with
 		 * a different name (session_start will do that). */
-		if ($anewt_session_current_name == $name)
+		if ($anewt_session_current_name === $name)
 			return;
 
-		if (!is_null($timeout)) {
+		if (!is_null($timeout))
+		{
 			assert('is_int($timeout)');
 			session_set_cookie_params($timeout);
 		}
@@ -59,35 +54,51 @@
 	}
 
 	/**
-	 * Destroys the current session.
+	 * Destroy the current session.
 	 */
-	function destroy() {
+	public static function destroy()
+	{
 		$_SESSION = array();
 		session_destroy();
 	}
 
+	/** \} */
+
+
+	/** \{
+	 * \name Methods for handling session data
+	 *
+	 * These methods can be used to get and set session data.
+	 */
+
 	/**
-	 * Stores a variable in the session.
+	 * Store a variable in the session.
 	 *
 	 * \param $name The variable name.
 	 * \param $value The value of the variable.
 	 */
-	function set($name, $value) {
+	public static function set($name, $value)
+	{
 		assert('is_string($name)');
 
 		$_SESSION[$name] = $value;
 	}
 
 	/**
-	 * Returns a variable from the session.
+	 * Obtain a value from the session.
+	 *
+	 * If no value was set for the provided name, an error is thrown.
 	 *
 	 * \param $name The variable name.
 	 *
 	 * \return The value of the variable.
 	 */
-	function get($name) {
+	public static function get($name)
+	{
 		assert('is_string($name)');
-		assert('Session::is_set($name)');
+
+		if (!AnewtSession::is_set($name))
+			throw new AnewtException('Session variabe "%s" is not set.', $name);
 
 		return $_SESSION[$name];
 	}
@@ -95,13 +106,18 @@
 	/**
 	 * Deletes a variable from the session.
 	 *
+	 * If no value was set for the provided name, an error is thrown.
+	 *
 	 * \param $name The name of the variable to delete.
 	 *
 	 * \return The value of deleted variable.
 	 */
-	function delete($name) {
+	public static function delete($name)
+	{
 		assert('is_string($name)');
-		assert('Session::is_set($name)');
+
+		if (!AnewtSession::is_set($name))
+			throw new AnewtException('Session variabe "%s" is not set.', $name);
 
 		$result = $_SESSION[$name];
 		unset($_SESSION[$name]);
@@ -109,30 +125,20 @@
 	}
 
 	/**
-	 * Shorthand for the delete() function. Deletes a variable from the session.
-	 *
-	 * \param $name The name of the variable to delete.
-	 *
-	 * \return The value of deleted variable.
-	 *
-	 * \see Session::delete
-	 */
-	function del($name) {
-		return Session::delete($name);
-	}
-
-	/**
 	 * Checks if a variable is defined in the session.
 	 *
 	 * \param $name The variable name to check for.
 	 *
 	 * \return True if the variable is available, false otherwise.
 	 */
-	function is_set($name) {
+	public static function is_set($name)
+	{
 		assert('is_string($name)');
 
-		return isset($_SESSION[$name]);
+		return array_key_exists($name, $_SESSION);
 	}
+
+	/** \} */
 }
 
 ?>