lithium\storage\session\adapter\Php::read PHP Method

read() public method

Read a value from the session.
public read ( null | string $key = null, array $options = [] ) : Closure
$key null | string Key of the entry to be read. If no key is passed, all current session data is returned.
$options array Options array. Not used for this adapter method.
return Closure Function returning data in the session if successful, `false` otherwise.
    public function read($key = null, array $options = array())
    {
        if (!$this->isStarted() && !$this->_start()) {
            throw new RuntimeException('Could not start session.');
        }
        return function ($self, $params) {
            $key = $params['key'];
            if (!$key) {
                return $_SESSION;
            }
            if (strpos($key, '.') === false) {
                return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
            }
            $filter = function ($keys, $data) use(&$filter) {
                $key = array_shift($keys);
                if (isset($data[$key])) {
                    return empty($keys) ? $data[$key] : $filter($keys, $data[$key]);
                }
            };
            return $filter(explode('.', $key), $_SESSION);
        };
    }

Usage Example

Beispiel #1
0
 public function testIsStartedNoInit()
 {
     $this->_destroySession(session_name());
     $Php = new Php(array('init' => false));
     $result = $Php->isStarted();
     $this->assertFalse($result);
     $Php = new Php();
     $Php->read();
     $result = $Php->isStarted();
     $this->assertTrue($result);
 }