Skip to content

Allowing dot-notation arrays to be used in session data. #251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 24 additions & 32 deletions src/Illuminate/Session/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ protected function isExpired($session)

/**
* Get the full array of session data, including flash data.
*
*
* @return array
*/
public function all()
Expand Down Expand Up @@ -210,28 +210,26 @@ public function get($key, $default = null)
// First we will check for the value in the general session data and if it
// is not present in that array we'll check the session flash datas to
// get the data from there. If netiher is there we give the default.
if (isset($data[$key]))
{
return $data[$key];
}

// Session flash data is only persisted for the next request into the app
// which makes it convenient for temporary status messages or various
// other strings. We'll check all of this flash data for the items.
elseif (isset($data[':new:'][$key]))
return array_get($data, $key, function() use ($data, $key, $default)
{
return $data[':new:'][$key];
}

// The "old" flash data are the data flashed during the previous request
// while the "new" data is the data flashed during the course of this
// current request. Usually developers will be retrieving the olds.
elseif (isset($data[':old:'][$key]))
{
return $data[':old:'][$key];
}
// Session flash data is only persisted for the next request into the app
// which makes it convenient for temporary status messages or various
// other strings. We'll check all of this flash data for the items.
if ($value = array_get($data, ":new:.$key"))
{
return $value;
}

// The "old" flash data are the data flashed during the previous request
// while the "new" data is the data flashed during the course of this
// current request. Usually developers will be retrieving the olds.
if ($value = array_get($data, ":old:.$key"))
{
return $value;
}

return $default instanceof Closure ? $default() : $default;
return $default instanceof Closure ? $default() : $default;
});
}

/**
Expand All @@ -252,14 +250,8 @@ public function getOldInput($key = null, $default = null)
{
return $input;
}
elseif (array_key_exists($key, $input))
{
return $input[$key];
}
else
{
return $default instanceof Closure ? $default() : $default;
}

return array_get($input, $key, $default);
}

/**
Expand All @@ -281,7 +273,7 @@ public function getToken()
*/
public function put($key, $value)
{
$this->session['data'][$key] = $value;
array_set($this->session['data'], $key, $value);
}

/**
Expand Down Expand Up @@ -343,7 +335,7 @@ public function keep($keys)
*/
public function forget($key)
{
unset($this->session['data'][$key]);
array_forget($this->session['data'], $key);
}

/**
Expand Down Expand Up @@ -636,4 +628,4 @@ public function offsetUnset($key)
$this->forget($key);
}

}
}
24 changes: 22 additions & 2 deletions tests/Session/SessionStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function testFinishMethodCallsCreateMethodAndAgesFlashData()
$store->setExists(false);
$response = new Response;
$cookie = $this->getCookieJarMock();
$store->finish($response, $cookie, 0);
$store->finish($response, $cookie, 0);
}


Expand Down Expand Up @@ -236,6 +236,26 @@ public function testFlashInputFlashesInput()
}


public function testComplexArrayPayloadManipulation()
{
$store = $this->storeMock('isInvalid');
$cookies = m::mock('Illuminate\Cookie\CookieJar');
$cookies->shouldReceive('get')->once()->with('illuminate_session')->andReturn('foo');
$store->start($cookies);

$store->put('foo.bar', 'baz');
$this->assertEquals('baz', $store->get('foo.bar'));
$this->assertEquals(array('bar' => 'baz'), $store->get('foo'));
$store->put('foo.bat', 'qux');
$this->assertCount(2, $store->get('foo'));
$this->assertEquals(array('bar' => 'baz', 'bat' => 'qux'), $store->get('foo'));
$this->assertTrue($store->has('foo.bat'));
$store->forget('foo.bat');
$this->assertEquals(array('bar' => 'baz'), $store->get('foo'));
$this->assertFalse($store->has('foo.bat'));
}


protected function dummySession()
{
return array('id' => '123', 'data' => array(':old:' => array(), ':new:' => array()), 'last_activity' => '9999999999');
Expand Down Expand Up @@ -287,4 +307,4 @@ public function sweep($expiration)
//
}

}
}