Permalink
Browse files

ENHANCEMENT: Improving Cookie class to allow for extendability

Previous to this the Cookie class has been very inflexible (cookies are
all set using the static Cookie::set() and so the functionality is not
extendable). Cookie class has been adjusted so extension is now a
possibility for those wishing to alter its functionality. Improves
compliance to the law of demeter.
  • Loading branch information...
1 parent 976f1f5 commit ebb2458f22f44e740f93fc99c49240cb72e9c6e6 Matt Lewis committed Jun 29, 2012
Showing with 50 additions and 16 deletions.
  1. +50 −16 control/Cookie.php
View
@@ -6,15 +6,29 @@
* @subpackage misc
*/
class Cookie {
-
+
/**
* @var boolean
*/
static $report_errors = true;
-
+
+ /**
+ * Cookie class and instance for extendability purposes
+ */
+ static $cookie_class = 'Cookie';
+
+ static $inst = null;
+
+ public static function get_inst() {
+ if(is_null(self::$inst)) {
+ self::$inst = new self::$cookie_class();
+ }
+ return self::$inst;
+ }
+
/**
* Set a cookie variable
- *
+ *
* @param string $name The variable name
* @param string $value The variable value.
* @param int $expiry The expiry time, in days. Defaults to 90.
@@ -24,6 +38,29 @@ class Cookie {
* @param boolean $httpOnly See http://php.net/set_session
*/
static function set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false, $httpOnly = false) {
+ return self::get_inst()->inst_set($name, $value, $expiry, $path, $domain, $secure, $httpOnly);
+ }
+
+ /**
+ * Get a cookie variable
+ */
+ static function get($name) {
+ return self::get_inst()->inst_get($name);
+ }
+
+ static function forceExpiry($name, $path = null, $domain = null) {
+ return self::get_inst()->inst_forceExpiry($name, $path, $domain);
+ }
+
+ static function set_report_errors($reportErrors) {
+ return self::get_inst()->inst_set_report_errors($reportErrors);
+ }
+
+ static function report_errors() {
+ return self::get_inst()->inst_report_errors();
+ }
+
+ public function inst_set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false, $httpOnly = false) {
if(!headers_sent($file, $line)) {
$expiry = $expiry > 0 ? time()+(86400*$expiry) : $expiry;
$path = ($path) ? $path : Director::baseURL();
@@ -34,25 +71,22 @@ static function set($name, $value, $expiry = 90, $path = null, $domain = null, $
}
}
}
-
- /**
- * Get a cookie variable
- */
- static function get($name) {
- return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
- }
-
- static function forceExpiry($name, $path = null, $domain = null) {
+
+ public function inst_get($name) {
+ return isset($_COOKIE[$name]) ? $_COOKIE[$name] : null;
+ }
+
+ public function inst_forceExpiry($name, $path = null, $domain = null) {
if(!headers_sent($file, $line)) {
self::set($name, null, -20, $path, $domain);
}
}
-
- static function set_report_errors($reportErrors) {
+
+ public function inst_set_report_errors($reportErrors) {
self::$report_errors = $reportErrors;
}
-
- static function report_errors() {
+
+ public function report_errors() {
return self::$report_errors;
}
}

0 comments on commit ebb2458

Please sign in to comment.