Created
May 10, 2011 00:11
-
-
Save isaiahdw/963687 to your computer and use it in GitHub Desktop.
session.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Bind a value on an array by path. | |
* | |
* @see Arr::path() | |
* @param array $array Array to update | |
* @param string $path Path | |
* @param mixed $value Value to set | |
* @param string $delimiter Path delimiter | |
*/ | |
public static function bind_path( & $array, $path, & $value, $delimiter = NULL) | |
{ | |
if ( ! $delimiter) | |
{ | |
// Use the default delimiter | |
$delimiter = Arr::$delimiter; | |
} | |
// Split the keys by delimiter | |
$keys = explode($delimiter, $path); | |
// Set current $array to inner-most array path | |
while (count($keys) > 1) | |
{ | |
$key = array_shift($keys); | |
if (ctype_digit($key)) | |
{ | |
// Make the key an integer | |
$key = (int) $key; | |
} | |
if ( ! isset($array[$key])) | |
{ | |
$array[$key] = array(); | |
} | |
$array = & $array[$key]; | |
} | |
// Set key on inner-most array | |
$array[array_shift($keys)] = & $value; | |
} | |
/** | |
* Delete a key on an array by path. | |
* | |
* @see Arr::path() | |
* @param array $array Array to update | |
* @param string $path Path | |
* @param mixed $value Value to set | |
* @param string $delimiter Path delimiter | |
*/ | |
public static function delete_path( & $array, $path, $delimiter = NULL) | |
{ | |
if ( ! $delimiter) | |
{ | |
// Use the default delimiter | |
$delimiter = Arr::$delimiter; | |
} | |
// Split the keys by delimiter | |
$keys = explode($delimiter, $path); | |
// Set current $array to inner-most array path | |
while (count($keys) > 1) | |
{ | |
$key = array_shift($keys); | |
if (ctype_digit($key)) | |
{ | |
// Make the key an integer | |
$key = (int) $key; | |
} | |
if ( ! isset($array[$key])) | |
{ | |
$array[$key] = array(); | |
} | |
$array = & $array[$key]; | |
} | |
// unset key | |
unset($array[array_shift($keys)]); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class Session extends Kohana_Session { | |
/** | |
* Get a variable from the session array, accepts paths | |
* | |
* $foo = $session->get('foo'); | |
* $bar = $session->get('foo.bar'); | |
* | |
* @param string variable name, or path | |
* @param mixed default value to return | |
* @return mixed | |
*/ | |
public function get($key, $default = NULL) | |
{ | |
// if $key is a path | |
if (strpos($key, '.') !== FALSE) | |
{ | |
return Arr::path($this->_data, $key, $default, '.'); | |
} | |
else | |
{ | |
// use parent get: | |
return parent::get($key, $default); | |
} | |
} | |
/** | |
* Get and delete a variable from the session array. | |
* | |
* $bar = $session->get_once('bar'); | |
* $foo = $session->get_once('bar.foo'); | |
* | |
* @param string variable name or path | |
* @param mixed default value to return | |
* @return mixed | |
* @uses Session::get, Session::delete | |
*/ | |
public function get_once($key, $default = NULL) | |
{ | |
// if $key is a path | |
if (strpos($key, '.') !== FALSE) | |
{ | |
$value = $this->get($key, $default); | |
$this->delete($key); | |
return $value; | |
} | |
else | |
{ | |
// use parent get_once: | |
return parent::get_once($key, $default); | |
} | |
} | |
/** | |
* Set a variable in the session array. | |
* | |
* $session->set('foo', 'bar'); | |
* $session->set('foo.bar', 'value'); | |
* | |
* @param string variable name or path | |
* @param mixed value | |
* @return $this | |
*/ | |
public function set($key, $value) | |
{ | |
// if $key is a path | |
if (strpos($key, '.') !== FALSE) | |
{ | |
Arr::set_path($this->_data, $key, $value, '.'); | |
} | |
else | |
{ | |
return parent::set($key, $value); | |
} | |
} | |
/** | |
* Set a variable by reference. | |
* | |
* $session->bind('foo', $foo); | |
* $session->bind('foo.bar', $bar); | |
* | |
* @param string variable name or path | |
* @param mixed referenced value | |
* @return $this | |
*/ | |
public function bind($key, & $value) | |
{ | |
// if $key is a path | |
if (strpos($key, '.') !== FALSE) | |
{ | |
Arr::bind_path($this->_data, $key, $value, '.'); | |
} | |
else | |
{ | |
$this->_data[$key] =& $value; | |
} | |
return $this; | |
} | |
/** | |
* Removes a variable in the session array. | |
* | |
* $session->delete('foo'); | |
* $session->delete('foo.bar'); | |
* | |
* @param string variable name or path | |
* @param ... | |
* @return $this | |
*/ | |
public function delete($key) | |
{ | |
$args = func_get_args(); | |
foreach ($args as $key) | |
{ | |
if (strpos($key, '.') !== FALSE) | |
{ | |
Arr::delete_path($this->_data, $key, '.'); | |
} | |
else | |
{ | |
unset($this->_data[$key]); | |
} | |
} | |
return $this; | |
} | |
} // Session |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment