Permalink
Browse files

BackwardsCompatibilityBreak - fSession::add(), fSession::delete(), fS…

…ession::get(), fSession::set(), fTemplating::add(), fTemplating::delete(), fTemplating::get(), fTemplating::set() all now intepret `[` and `]` as array syntax and thus these can not be used in key or element names. fTemplating::remove() was renamed to fTemplating::filter().

Added the `$beginning` parameter to fSession::add() and fTemplating::add(). Added the methods fSession::remove() and fTemplating::remove().
  • Loading branch information...
wbond committed Sep 12, 2010
1 parent 6b1b0ea commit e7b5f79934e7759de3b25e8220ce9bfe35c48b13
Showing with 415 additions and 97 deletions.
  1. +24 −51 classes/fRequest.php
  2. +174 −12 classes/fSession.php
  3. +217 −34 classes/fTemplating.php
View
@@ -16,7 +16,8 @@
* @package Flourish
* @link http://flourishlib.com/fRequest
*
* @version 1.0.0b13
* @version 1.0.0b14
* @changes 1.0.0b14 Rewrote ::set() to not require recursion for array syntax [wb, 2010-09-12]
* @changes 1.0.0b13 Fixed ::set() to work with `PUT` requests [wb, 2010-06-30]
* @changes 1.0.0b12 Fixed a bug with ::getBestAcceptLanguage() returning the second-best language [wb, 2010-05-27]
* @changes 1.0.0b11 Added ::isAjax() [al, 2010-03-15]
@@ -611,36 +612,6 @@ static private function processAcceptHeader($header)
}
/**
* Recursively sets an array request value
*
* @param mixed $existing_value The existing value that the value will be added to, or overwrite
* @param array $array_keys An array of the keys to use for the value
* @param mixed $value The value to set the key to
* @return array The modified `$existing_value` with the new value added
*/
static private function recursiveSet($existing_value, $array_keys, $value)
{
if (!$array_keys) {
return $value;
}
$key = array_shift($array_keys);
if (!is_array($existing_value)) {
$existing_value = array();
}
$existing_value[$key] = self::recursiveSet(
isset($existing_value[$key]) ? $existing_value[$key] : array(),
$array_keys,
$value
);
return $existing_value;
}
/**
* Resets the configuration and data of the class
*
@@ -669,31 +640,33 @@ static public function reset()
*/
static public function set($key, $value)
{
$array_dereference = NULL;
if (strpos($key, '[')) {
$bracket_pos = strpos($key, '[');
if (self::isPost()) {
$tip =& $_POST;
} elseif (self::isGet()) {
$tip =& $_GET;
} elseif (self::isDelete() || self::isPut()) {
self::initPutDelete();
$tip =& self::$put_delete;
}
if ($bracket_pos = strpos($key, '[')) {
$array_dereference = substr($key, $bracket_pos);
$key = substr($key, 0, $bracket_pos);
preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER);
$array_keys = array_map('current', $array_keys);
$value = self::recursiveSet(self::get($key), $array_keys, $value);
}
if (self::isPost()) {
$_POST[$key] = $value;
return;
}
if (self::isGet()) {
$_GET[$key] = $value;
return;
}
if (self::isDelete() || self::isPut()) {
self::initPutDelete();
self::$put_delete[$key] = $value;
return;
array_unshift($array_keys, $key);
foreach (array_slice($array_keys, 0, -1) as $array_key) {
if (!isset($tip[$array_key]) || !is_array($tip[$array_key])) {
$tip[$array_key] = array();
}
$tip =& $tip[$array_key];
}
$tip[end($array_keys)] = $value;
} else {
$tip[$key] = $value;
}
}
View
@@ -15,7 +15,8 @@
* @package Flourish
* @link http://flourishlib.com/fSession
*
* @version 1.0.0b13
* @version 1.0.0b14
* @changes 1.0.0b14 Backwards Compatibility Break - ::add(), ::delete(), ::get() and ::set() now interpret `[` and `]` as array shorthand and thus they can not be used in keys - added `$beginning` parameter to ::add(), added ::remove() method [wb, 2010-09-12]
* @changes 1.0.0b13 Fixed a bug that prevented working with existing sessions since they did not have the `fSession::expires` key [wb, 2010-08-24]
* @changes 1.0.0b12 Changed ::enablePersistence() to always regenerate the session ID, which ensures the function works even if the ID has already been regenerated by fAuthorizaton [wb, 2010-08-21]
* @changes 1.0.0b11 Updated the class to make sure ::enablePersistence() is called after ::ignoreSubdomain(), ::setLength() and ::setPath() [wb, 2010-05-29]
@@ -81,25 +82,57 @@ class fSession
/**
* Adds a value to an already-existing array value, or to a new array value
*
* @param string $key The name to access the array under
* @param mixed $value The value to add to the array
* @param string $key The name to access the array under
* @param mixed $value The value to add to the array
* @param boolean $beginning If the value should be added to the beginning
* @return void
*/
static public function add($key, $value)
static public function add($key, $value, $beginning=FALSE)
{
self::open();
if (!isset($_SESSION[$key])) {
$_SESSION[$key] = array();
$tip =& $_SESSION;
if ($bracket_pos = strpos($key, '[')) {
$original_key = $key;
$array_dereference = substr($key, $bracket_pos);
$key = substr($key, 0, $bracket_pos);
preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER);
$array_keys = array_map('current', $array_keys);
array_unshift($array_keys, $key);
foreach (array_slice($array_keys, 0, -1) as $array_key) {
if (!isset($tip[$array_key])) {
$tip[$array_key] = array();
} elseif (!is_array($tip[$array_key])) {
throw new fProgrammerException(
'%1$s was called for the key, %2$s, which is not an array',
__CLASS__ . '::add()',
$original_key
);
}
$tip =& $tip[$array_key];
}
$key = end($array_keys);
}
if (!is_array($_SESSION[$key])) {
if (!isset($tip[$key])) {
$tip[$key] = array();
} elseif (!is_array($tip[$key])) {
throw new fProgrammerException(
'%1$s was called for the key, %2$s, which is not an array',
__CLASS__ . '::add()',
$key
);
}
$_SESSION[$key][] = $value;
}
if ($beginning) {
array_unshift($tip[$key], $value);
} else {
$tip[$key][] = $value;
}
}
/**
@@ -158,7 +191,37 @@ static public function delete($key)
{
self::open();
unset($_SESSION[$key]);
if ($bracket_pos = strpos($key, '[')) {
$original_key = $key;
$array_dereference = substr($key, $bracket_pos);
$key = substr($key, 0, $bracket_pos);
if (!isset($_SESSION[$key])) {
return;
}
preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER);
$array_keys = array_map('current', $array_keys);
$tip =& $_SESSION[$key];
foreach (array_slice($array_keys, 0, -1) as $array_key) {
if (!isset($tip[$array_key])) {
return;
} elseif (!is_array($tip[$array_key])) {
throw new fProgrammerException(
'%1$s was called for an element, %2$s, which is not an array',
__CLASS__ . '::delete()',
$original_key
);
}
$tip =& $tip[$array_key];
}
unset($tip[end($array_keys)]);
} else {
unset($_SESSION[$key]);
}
}
@@ -236,7 +299,31 @@ static public function enablePersistence()
static public function get($key, $default_value=NULL)
{
self::open();
return (isset($_SESSION[$key])) ? $_SESSION[$key] : $default_value;
$array_dereference = NULL;
if ($bracket_pos = strpos($key, '[')) {
$array_dereference = substr($key, $bracket_pos);
$key = substr($key, 0, $bracket_pos);
}
if (!isset($_SESSION[$key])) {
return $default_value;
}
$value = $_SESSION[$key];
if ($array_dereference) {
preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER);
$array_keys = array_map('current', $array_keys);
foreach ($array_keys as $array_key) {
if (!is_array($value) || !isset($value[$array_key])) {
$value = $default_value;
break;
}
$value = $value[$array_key];
}
}
return $value;
}
@@ -343,6 +430,61 @@ static public function regenerateID()
}
/**
* Removes and returns the value from the end of an array value
*
* @param string $key The name of the element to remove the value from
* @param boolean $beginning If the value should be removed to the beginning
* @return mixed The value that was removed
*/
static public function remove($key, $beginning=FALSE)
{
self::open();
$tip =& $_SESSION;
if ($bracket_pos = strpos($key, '[')) {
$original_key = $key;
$array_dereference = substr($key, $bracket_pos);
$key = substr($key, 0, $bracket_pos);
preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER);
$array_keys = array_map('current', $array_keys);
array_unshift($array_keys, $key);
foreach (array_slice($array_keys, 0, -1) as $array_key) {
if (!isset($tip[$array_key])) {
return NULL;
} elseif (!is_array($tip[$array_key])) {
throw new fProgrammerException(
'%1$s was called for the key, %2$s, which is not an array',
__CLASS__ . '::remove()',
$original_key
);
}
$tip =& $tip[$array_key];
}
$key = end($array_keys);
}
if (!isset($tip[$key])) {
return NULL;
} elseif (!is_array($tip[$key])) {
throw new fProgrammerException(
'%1$s was called for the key, %2$s, which is not an array',
__CLASS__ . '::remove()',
$key
);
}
if ($beginning) {
return array_shift($tip[$key]);
}
return array_pop($tip[$key]);
}
/**
* Resets the configuration of the class
*
@@ -370,7 +512,27 @@ static public function reset()
static public function set($key, $value)
{
self::open();
$_SESSION[$key] = $value;
$tip =& $_SESSION;
if ($bracket_pos = strpos($key, '[')) {
$array_dereference = substr($key, $bracket_pos);
$key = substr($key, 0, $bracket_pos);
preg_match_all('#(?<=\[)[^\[\]]+(?=\])#', $array_dereference, $array_keys, PREG_SET_ORDER);
$array_keys = array_map('current', $array_keys);
array_unshift($array_keys, $key);
foreach (array_slice($array_keys, 0, -1) as $array_key) {
if (!isset($tip[$array_key]) || !is_array($tip[$array_key])) {
$tip[$array_key] = array();
}
$tip =& $tip[$array_key];
}
$tip[end($array_keys)] = $value;
} else {
$tip[$key] = $value;
}
}
Oops, something went wrong.

0 comments on commit e7b5f79

Please sign in to comment.