Skip to content

Session: added getFlashSection() #5

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

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 39 additions & 0 deletions src/Http/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really responsibility of Session? Session doesn't care about URL.


/** @var bool has been session ID regenerated? */
private $regenerated;

Expand Down Expand Up @@ -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;
}
}


Expand Down Expand Up @@ -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**/


Expand Down
36 changes: 36 additions & 0 deletions tests/Http/Session.flash.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* Test: Nette\Http\Session flash sections.
*/

use Nette\Http\Session,
Nette\Http\SessionSection,
Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test(function() {
$session = new Session(new Nette\Http\Request(new Nette\Http\UrlScript), new Nette\Http\Response);

Assert::null( $session->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() );
});