Skip to content

Commit 206d8c1

Browse files
committed
Merge pull request #251 from bencorlett/feature/deep-sessions
Allowing dot-notation arrays to be used in session data.
2 parents 5db2ae9 + 357e48c commit 206d8c1

File tree

2 files changed

+46
-34
lines changed

2 files changed

+46
-34
lines changed

src/Illuminate/Session/Store.php

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ protected function isExpired($session)
177177

178178
/**
179179
* Get the full array of session data, including flash data.
180-
*
180+
*
181181
* @return array
182182
*/
183183
public function all()
@@ -210,28 +210,26 @@ public function get($key, $default = null)
210210
// First we will check for the value in the general session data and if it
211211
// is not present in that array we'll check the session flash datas to
212212
// get the data from there. If netiher is there we give the default.
213-
if (isset($data[$key]))
214-
{
215-
return $data[$key];
216-
}
217-
218-
// Session flash data is only persisted for the next request into the app
219-
// which makes it convenient for temporary status messages or various
220-
// other strings. We'll check all of this flash data for the items.
221-
elseif (isset($data[':new:'][$key]))
213+
return array_get($data, $key, function() use ($data, $key, $default)
222214
{
223-
return $data[':new:'][$key];
224-
}
225-
226-
// The "old" flash data are the data flashed during the previous request
227-
// while the "new" data is the data flashed during the course of this
228-
// current request. Usually developers will be retrieving the olds.
229-
elseif (isset($data[':old:'][$key]))
230-
{
231-
return $data[':old:'][$key];
232-
}
215+
// Session flash data is only persisted for the next request into the app
216+
// which makes it convenient for temporary status messages or various
217+
// other strings. We'll check all of this flash data for the items.
218+
if ($value = array_get($data, ":new:.$key"))
219+
{
220+
return $value;
221+
}
222+
223+
// The "old" flash data are the data flashed during the previous request
224+
// while the "new" data is the data flashed during the course of this
225+
// current request. Usually developers will be retrieving the olds.
226+
if ($value = array_get($data, ":old:.$key"))
227+
{
228+
return $value;
229+
}
233230

234-
return $default instanceof Closure ? $default() : $default;
231+
return $default instanceof Closure ? $default() : $default;
232+
});
235233
}
236234

237235
/**
@@ -252,14 +250,8 @@ public function getOldInput($key = null, $default = null)
252250
{
253251
return $input;
254252
}
255-
elseif (array_key_exists($key, $input))
256-
{
257-
return $input[$key];
258-
}
259-
else
260-
{
261-
return $default instanceof Closure ? $default() : $default;
262-
}
253+
254+
return array_get($input, $key, $default);
263255
}
264256

265257
/**
@@ -281,7 +273,7 @@ public function getToken()
281273
*/
282274
public function put($key, $value)
283275
{
284-
$this->session['data'][$key] = $value;
276+
array_set($this->session['data'], $key, $value);
285277
}
286278

287279
/**
@@ -343,7 +335,7 @@ public function keep($keys)
343335
*/
344336
public function forget($key)
345337
{
346-
unset($this->session['data'][$key]);
338+
array_forget($this->session['data'], $key);
347339
}
348340

349341
/**
@@ -636,4 +628,4 @@ public function offsetUnset($key)
636628
$this->forget($key);
637629
}
638630

639-
}
631+
}

tests/Session/SessionStoreTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function testFinishMethodCallsCreateMethodAndAgesFlashData()
181181
$store->setExists(false);
182182
$response = new Response;
183183
$cookie = $this->getCookieJarMock();
184-
$store->finish($response, $cookie, 0);
184+
$store->finish($response, $cookie, 0);
185185
}
186186

187187

@@ -236,6 +236,26 @@ public function testFlashInputFlashesInput()
236236
}
237237

238238

239+
public function testComplexArrayPayloadManipulation()
240+
{
241+
$store = $this->storeMock('isInvalid');
242+
$cookies = m::mock('Illuminate\Cookie\CookieJar');
243+
$cookies->shouldReceive('get')->once()->with('illuminate_session')->andReturn('foo');
244+
$store->start($cookies);
245+
246+
$store->put('foo.bar', 'baz');
247+
$this->assertEquals('baz', $store->get('foo.bar'));
248+
$this->assertEquals(array('bar' => 'baz'), $store->get('foo'));
249+
$store->put('foo.bat', 'qux');
250+
$this->assertCount(2, $store->get('foo'));
251+
$this->assertEquals(array('bar' => 'baz', 'bat' => 'qux'), $store->get('foo'));
252+
$this->assertTrue($store->has('foo.bat'));
253+
$store->forget('foo.bat');
254+
$this->assertEquals(array('bar' => 'baz'), $store->get('foo'));
255+
$this->assertFalse($store->has('foo.bat'));
256+
}
257+
258+
239259
protected function dummySession()
240260
{
241261
return array('id' => '123', 'data' => array(':old:' => array(), ':new:' => array()), 'last_activity' => '9999999999');
@@ -287,4 +307,4 @@ public function sweep($expiration)
287307
//
288308
}
289309

290-
}
310+
}

0 commit comments

Comments
 (0)