Skip to content

Commit 961fb2e

Browse files
committed
Session: added getFlashSection()
1 parent 9583852 commit 961fb2e

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

src/Http/Session.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ class Session extends Nette\Object
2828
/** Default file lifetime is 3 hours */
2929
const DEFAULT_FILE_LIFETIME = 10800;
3030

31+
/** Default flash lifetime */
32+
const DEFAULT_FLASH_LIFETIME = '10 seconds';
33+
34+
/** Session ID in URL */
35+
const FLASH_KEY = '_fid';
36+
3137
/** @var bool has been session ID regenerated? */
3238
private $regenerated;
3339

@@ -63,11 +69,17 @@ class Session extends Nette\Object
6369
/** @var IResponse */
6470
private $response;
6571

72+
/** @var string|NULL */
73+
private $flashId;
74+
6675

6776
public function __construct(IRequest $request, IResponse $response)
6877
{
6978
$this->request = $request;
7079
$this->response = $response;
80+
if (is_string($flashId = $request->getQuery(self::FLASH_KEY))) {
81+
$this->flashId = $flashId;
82+
}
7183
}
7284

7385

@@ -348,6 +360,33 @@ public function clean()
348360
}
349361

350362

363+
/********************* flash sections ****************d*g**/
364+
365+
366+
/**
367+
* @return string|NULL
368+
*/
369+
public function getFlashId()
370+
{
371+
return $this->flashId;
372+
}
373+
374+
375+
/**
376+
* Returns specified flash section and creates flash ID if doesn't exist.
377+
* @param string
378+
* @return SessionSection
379+
*/
380+
public function getFlashSection($section)
381+
{
382+
if (!$this->flashId) {
383+
$this->flashId = Nette\Utils\Random::generate(4);
384+
}
385+
return $this->getSection("$section/$this->flashId")
386+
->setExpiration(self::DEFAULT_FLASH_LIFETIME);
387+
}
388+
389+
351390
/********************* configuration ****************d*g**/
352391

353392

tests/Http/Session.flash.phpt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Http\Session flash sections.
5+
*/
6+
7+
use Nette\Http\Session,
8+
Nette\Http\SessionSection,
9+
Tester\Assert;
10+
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
15+
test(function() {
16+
$session = new Session(new Nette\Http\Request(new Nette\Http\UrlScript), new Nette\Http\Response);
17+
18+
Assert::null( $session->getFlashId() );
19+
20+
$section = $session->getFlashSection('f');
21+
Assert::type( 'Nette\Http\SessionSection', $section );
22+
23+
Assert::match( '%a%', $session->getFlashId() );
24+
});
25+
26+
27+
test(function() {
28+
$session = new Session(new Nette\Http\Request(new Nette\Http\UrlScript('?_fid=123')), new Nette\Http\Response);
29+
Assert::same( '123', $session->getFlashId() );
30+
});
31+
32+
33+
test(function() {
34+
$session = new Session(new Nette\Http\Request(new Nette\Http\UrlScript('?_fid[]=123')), new Nette\Http\Response);
35+
Assert::null( $session->getFlashId() );
36+
});

0 commit comments

Comments
 (0)