Skip to content

Commit

Permalink
Reimplementing Session::read()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 17, 2014
1 parent 7170e65 commit 826614b
Showing 1 changed file with 23 additions and 38 deletions.
61 changes: 23 additions & 38 deletions src/Network/Session.php
Expand Up @@ -286,6 +286,29 @@ public function check($name = null) {
return Hash::get($_SESSION, $name) !== null;
}

/**
* Returns given session variable, or all of them, if no parameters given.
*
* @param string|array $name The name of the session variable (or a path as sent to Set.extract)
* @return mixed The value of the session variable, null if session not available,
* session not started, or provided name not found in the session.
*/
public function read($name = null) {
if (empty($name) && $name !== null) {
return null;
}

if ($this->_hasSession() && !$this->started()) {
$this->start();
}

if ($name === null) {
return $_SESSION ?: [];
}

return Hash::get($_SESSION, $name);
}

/**
* Returns the session id.
* Calling this method will not auto start the session. You might have to manually
Expand Down Expand Up @@ -345,44 +368,6 @@ protected static function _overwrite(&$old, $new) {
}
}

/**
* Returns given session variable, or all of them, if no parameters given.
*
* @param string|array $name The name of the session variable (or a path as sent to Set.extract)
* @return mixed The value of the session variable, null if session not available,
* session not started, or provided name not found in the session.
*/
public static function read($name = null) {
if (empty($name) && $name !== null) {
return false;
}
if (!static::_hasSession() || !static::start()) {
return null;
}
if ($name === null) {
return static::_returnSessionVars();
}
$result = Hash::get($_SESSION, $name);

if (isset($result)) {
return $result;
}
return null;
}

/**
* Returns all session variables.
*
* @return mixed Full $_SESSION array, or false on error.
*/
protected static function _returnSessionVars() {
if (!empty($_SESSION)) {
return $_SESSION;
}
static::_setError(2, 'No Session vars set');
return false;
}

/**
* Writes value to given session variable name.
*
Expand Down

0 comments on commit 826614b

Please sign in to comment.