← Back to team overview

anewt-developers team mailing list archive

[Branch ~uws/anewt/anewt.uws] Rev 1712: [core] Refactor static Request class into AnewtRequest

 

------------------------------------------------------------
revno: 1712
committer: Wouter Bolsterlee <uws@xxxxxxxxx>
branch nick: anewt.uws
timestamp: Sun 2009-08-02 12:54:09 +0200
message:
  [core] Refactor static Request class into AnewtRequest
  
  More API breakage! Cleanup AnewtRequest code and prepare for
  merging the AnewtGPC code into AnewtRequest.
modified:
  core/request.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 'core/request.lib.php'
--- core/request.lib.php	2008-12-05 15:42:09 +0000
+++ core/request.lib.php	2009-08-02 10:54:09 +0000
@@ -9,23 +9,21 @@
 
 
 /**
- * The Request class contains request information. This class provides several
- * static methods that can be used to get information about the current HTTP
- * request.
+ * The AnewtRequest class contains request information. This class provides
+ * several static methods that can be used to get information about the current
+ * HTTP request.
  */
-final class Request
+final class AnewtRequest
 {
-	/**
-	 * The constructor throws an error. This class only provides static methods.
+	/** \{
+	 * \name URL methods
+	 *
+	 * These method can be used to find out various forms of the request url,
+	 * e.g. relative and absolute urls, domain names, and so on.
 	 */
-	function Request()
-	{
-		trigger_error('Cannot create a new Request instance. The Request class
-				only provides static methods.', E_USER_ERROR);
-	}
 
 	/**
-	 * Alias for Request::relative_url().
+	 * Alias for AnewtRequest::relative_url().
 	 * 
 	 * \param $include_query_string
 	 *   Whether the query string (the part after the question mark in HTTP GET
@@ -33,11 +31,11 @@
 	 * \return
 	 *   The relative URL for the current request.
 	 *
-	 * \see Request::relative_url
+	 * \see AnewtRequest::relative_url
 	 */
-	static function url($include_query_string=true)
+	public static function url($include_query_string=true)
 	{
-		return Request::relative_url($include_query_string);
+		return AnewtRequest::relative_url($include_query_string);
 	}
 
 	/**
@@ -50,9 +48,9 @@
 	 * \return
 	 *   The relative URL for the current request.
 	 *
-	 * \see Request::canonical_url
+	 * \see AnewtRequest::canonical_url
 	 */
-	static function relative_url($include_query_string=true)
+	public static function relative_url($include_query_string=true)
 	{
 		assert('is_bool($include_query_string)');
 
@@ -82,22 +80,47 @@
 	}
 
 	/**
+	 * Returns the canonical URL for the current request. This includes the
+	 * http part, the hostname and (optionally) port number.
+	 *
+	 * \param $include_query_string
+	 *   Whether the query string (the part after the question mark in HTTP GET
+	 *   request should be included (optional, defaults to true)
+	 *
+	 * \return
+	 *   The canonical URL for the current request.
+	 *
+	 * \see AnewtRequest::relative_url
+	 * \see AnewtRequest::canonical_base_url
+	 */
+	public static function canonical_url($include_query_string=true)
+	{
+		$canonical_base_url = AnewtRequest::canonical_base_url();
+		$relative_url = AnewtRequest::relative_url($include_query_string);
+
+		if ($relative_url{0} != '/')
+			$relative_url = '/' . $relative_url;
+
+		return $canonical_base_url . $relative_url;
+	}
+
+	/**
 	 * Returns the canonical base URL for the current request. This includes the
 	 * http part, the hostname and (optionally) port number.
 	 *
 	 * \return
 	 *   The canonical base URL for the current request.
 	 *
-	 * \see Request::canonical_url
+	 * \see AnewtRequest::canonical_url
 	 */
-	static function canonical_base_url()
+	public static function canonical_base_url()
 	{
 		/* Protocol: http or https? */
 		$is_ssl = (array_get_default($_SERVER, 'HTTPS', 'off') === 'on');
 		$protocol = $is_ssl ? 'https' : 'http'; 
 
 		/* The host */
-		$host = Request::host();
+		$host = AnewtRequest::host();
 
 		/* Get the port number */
 		$port = array_get_int($_SERVER, 'SERVER_PORT', 80);
@@ -126,93 +149,11 @@
 	}
 
 	/**
-	 * Returns the canonical URL for the current request. This includes the
-	 * http part, the hostname and (optionally) port number.
-	 *
-	 * \param $include_query_string
-	 *   Whether the query string (the part after the question mark in HTTP GET
-	 *   request should be included (optional, defaults to true)
-	 *
-	 * \return
-	 *   The canonical URL for the current request.
-	 *
-	 * \see Request::relative_url
-	 * \see Request::canonical_base_url
-	 */
-	static function canonical_url($include_query_string=true)
-	{
-		$canonical_base_url = Request::canonical_base_url();
-		$relative_url = Request::relative_url($include_query_string);
-
-		if ($relative_url{0} != '/')
-			$relative_url = '/' . $relative_url;
-
-		return $canonical_base_url . $relative_url;
-	}
-
-	/**
-	 * Returns the method type of this request (GET or POST).
-	 *
-	 * \return
-	 *   A string telling which type of request this is. This can be either
-	 *   'GET' or 'POST'.
-	 *
-	 * \see Request::is_get
-	 * \see Request::is_post
-	 */
-	static function method()
-	{
-		$method = array_get_default($_SERVER, 'REQUEST_METHOD');
-		if (!is_null($method))
-		{
-			if ($method == 'GET') return 'GET';
-			elseif ($method == 'POST') return 'POST';
-			// else: fall-through to fallback code
-		}
-
-		/* As a fallback, the contents of the $_POST array are checked. If it
-		 * contains data, this is a POST request. It it's empty, it's (most
-		 * likely) a GET request. */
-		return (count($_POST) > 0)
-			? 'POST'
-			: 'GET';
-	}
-
-	/**
-	 * Check if the request is a HTTP GET request.
-	 *
-	 * \return
-	 *   True if the request is a GET request, false otherwise.
-	 *
-	 * \see Request::is_post
-	 * \see Request::method
-	 */
-	static function is_get()
-	{
-		return Request::method() === 'GET';
-	}
-
-	/**
-	 * Check if the request is a HTTP POST request.
-	 *
-	 * \return
-	 *   True if the request is a POST request, false otherwise.
-	 * 
-	 * \see Request::is_get
-	 * \see Request::method
-	 */
-	static function is_post()
-	{
-		return Request::method() === 'POST';
-	}
-
-
-	/**
 	 * Returns the hostname as provided in the server configuration.
 	 *
 	 * \return Host name for the current request.
 	 */
-	static function host()
+	public static function host()
 	{
 		return array_get_default($_SERVER, 'SERVER_NAME', 'localhost');
 	}
@@ -223,7 +164,7 @@
 	 *
 	 * \return Host name for the current request.
 	 */
-	static function http_host()
+	public static function http_host()
 	{
 		return array_get_default($_SERVER, 'HTTP_HOST', 'localhost');
 	}
@@ -234,13 +175,13 @@
 	 * domain names. A domain name is assumed to be the rightmost part of the
 	 * full hostname. A test on very short strings is used to detect top level
 	 * domains such as .com, .nl and double ones like .co.uk. This obviously
-	 * fails for longer top level domains en extremely short domain names.
+	 * fails for longer top level domains and extremely short domain names.
 	 *
 	 * \return Domain name for the current request.
 	 */
-	static function domain()
+	public static function domain()
 	{
-		$host = Request::host();
+		$host = AnewtRequest::host();
 
 		/* If it looks like an ip address we just return the IP address */
 		if (preg_match('/^[0-9]{1,3}(\.[0-9]{1,3}){3}$/', $host))
@@ -283,20 +224,89 @@
 	 * \return
 	 *   The refererring url of the current request, or null.
 	 */
-	static function referer()
+	public static function referer()
 	{
 		return array_get_default($_SERVER, 'HTTP_REFERER', null);
 	}
 
 	/**
-	 * Alias for Request::referer.
-	 *
-	 * \see Request::referer
-	 */
-	static function referrer()
-	{
-		return Request::referer();
-	}
+	 * Alias for AnewtRequest::referer.
+	 *
+	 * \see AnewtRequest::referer
+	 */
+	public static function referrer()
+	{
+		return AnewtRequest::referer();
+	}
+
+	/** \} */
+
+	/** \{
+	 * \name GET, POST, and cookie methods
+	 *
+	 * These methods allow you to find out FIXME and extract \c GET and \c POST
+	 * parameters and cookie values.
+	 */
+
+	/**
+	 * Returns the method type of this request (GET or POST).
+	 *
+	 * \return
+	 *   A string telling which type of request this is. This can be either
+	 *   'GET' or 'POST'.
+	 *
+	 * \see AnewtRequest::is_get
+	 * \see AnewtRequest::is_post
+	 */
+	public static function method()
+	{
+		$method = array_get_default($_SERVER, 'REQUEST_METHOD');
+		if (!is_null($method))
+		{
+			if ($method == 'GET') return 'GET';
+			elseif ($method == 'POST') return 'POST';
+			// else: fall-through to fallback code
+		}
+
+		/* As a fallback, the contents of the $_POST array are checked. If it
+		 * contains data, this is a POST request. It it's empty, it's (most
+		 * likely) a GET request. */
+		return (count($_POST) > 0)
+			? 'POST'
+			: 'GET';
+	}
+
+	/**
+	 * Check if the request is a HTTP GET request.
+	 *
+	 * \return
+	 *   True if the request is a GET request, false otherwise.
+	 *
+	 * \see AnewtRequest::is_post
+	 * \see AnewtRequest::method
+	 */
+	public static function is_get()
+	{
+		return AnewtRequest::method() === 'GET';
+	}
+
+	/**
+	 * Check if the request is a HTTP POST request.
+	 *
+	 * \return
+	 *   True if the request is a POST request, false otherwise.
+	 * 
+	 * \see AnewtRequest::is_get
+	 * \see AnewtRequest::method
+	 */
+	public static function is_post()
+	{
+		return AnewtRequest::method() === 'POST';
+	}
+
+	/* TODO: merge AnewtGPC here */
+
+	/** \} */
 }
 
 ?>