diff --git a/src/Http/Session.php b/src/Http/Session.php index 721304a6..548b5dbc 100644 --- a/src/Http/Session.php +++ b/src/Http/Session.php @@ -28,6 +28,12 @@ class Session extends Nette\Object /** Default file lifetime is 3 hours */ const DEFAULT_FILE_LIFETIME = 10800; + /** Default flash lifetime */ + const DEFAULT_FLASH_LIFETIME = '10 seconds'; + + /** Session ID in URL */ + const FLASH_KEY = '_fid'; + /** @var bool has been session ID regenerated? */ private $regenerated; @@ -63,11 +69,17 @@ class Session extends Nette\Object /** @var IResponse */ private $response; + /** @var string|NULL */ + private $flashId; + public function __construct(IRequest $request, IResponse $response) { $this->request = $request; $this->response = $response; + if (is_string($flashId = $request->getQuery(self::FLASH_KEY))) { + $this->flashId = $flashId; + } } @@ -348,6 +360,33 @@ public function clean() } + /********************* flash sections ****************d*g**/ + + + /** + * @return string|NULL + */ + public function getFlashId() + { + return $this->flashId; + } + + + /** + * Returns specified flash section and creates flash ID if doesn't exist. + * @param string + * @return SessionSection + */ + public function getFlashSection($section) + { + if (!$this->flashId) { + $this->flashId = Nette\Utils\Random::generate(4); + } + return $this->getSection("$section/$this->flashId") + ->setExpiration(self::DEFAULT_FLASH_LIFETIME); + } + + /********************* configuration ****************d*g**/ diff --git a/tests/Http/Session.flash.phpt b/tests/Http/Session.flash.phpt new file mode 100644 index 00000000..bec4c386 --- /dev/null +++ b/tests/Http/Session.flash.phpt @@ -0,0 +1,36 @@ +getFlashId() ); + + $section = $session->getFlashSection('f'); + Assert::type( 'Nette\Http\SessionSection', $section ); + + Assert::match( '%a%', $session->getFlashId() ); +}); + + +test(function() { + $session = new Session(new Nette\Http\Request(new Nette\Http\UrlScript('?_fid=123')), new Nette\Http\Response); + Assert::same( '123', $session->getFlashId() ); +}); + + +test(function() { + $session = new Session(new Nette\Http\Request(new Nette\Http\UrlScript('?_fid[]=123')), new Nette\Http\Response); + Assert::null( $session->getFlashId() ); +});