-
-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Presenter::storeRequest() & restoreRequest() are separated into Reque…
…stStorage service; restoreRequest() redirects to original URL Conflicts: src/Application/UI/Presenter.php tests/Application/Presenter.storeRequest().phpt
- Loading branch information
1 parent
2f4f606
commit 20e7595
Showing
7 changed files
with
312 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (http://nette.org) | ||
* Copyright (c) 2004 David Grudl (http://davidgrudl.com) | ||
*/ | ||
|
||
namespace Nette\Application; | ||
|
||
use Nette; | ||
|
||
|
||
/** | ||
* Interface for storing and restoring requests. | ||
* | ||
* @author Martin Major | ||
*/ | ||
interface IRequestStorage | ||
{ | ||
|
||
/** | ||
* Stores request and returns key. | ||
* @return string key | ||
*/ | ||
function store(Request $request, Nette\Http\Url $url); | ||
|
||
/** | ||
* Restores original URL. | ||
* @param string key | ||
* @return string|NULL | ||
*/ | ||
function getUrl($key); | ||
|
||
/** | ||
* Returns stored request. | ||
* @return Request|NULL | ||
*/ | ||
function restore(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Nette Framework (http://nette.org) | ||
* Copyright (c) 2004 David Grudl (http://davidgrudl.com) | ||
*/ | ||
|
||
namespace Nette\Application; | ||
|
||
use Nette, | ||
Nette\Http; | ||
|
||
|
||
/** | ||
* Service for storing and restoring requests from session. | ||
* | ||
* @author Martin Major | ||
*/ | ||
class RequestStorage extends Nette\Object implements IRequestStorage | ||
{ | ||
/** @var Http\Session */ | ||
private $session; | ||
|
||
/** @var Nette\Security\User */ | ||
private $user; | ||
|
||
|
||
public function __construct(Http\Session $session, Nette\Security\User $user) | ||
{ | ||
$this->session = $session; | ||
$this->user = $user; | ||
} | ||
|
||
|
||
/** | ||
* Stores request and returns key. | ||
* @return string key | ||
*/ | ||
public function store(Request $request, Http\Url $url, $expiration = '10 minutes') | ||
{ | ||
$session = $this->session->getSection(__CLASS__); | ||
do { | ||
$key = Nette\Utils\Random::generate(5); | ||
} while (isset($session[$key])); | ||
|
||
$session[$key] = [clone $request, clone $url, $this->user->getId()]; | ||
$session->setExpiration($expiration, $key); | ||
return $key; | ||
} | ||
|
||
|
||
/** | ||
* Restores original URL. | ||
* @param string key | ||
* @return string|NULL | ||
*/ | ||
public function getUrl($key) | ||
{ | ||
list($request, $url, $user) = $this->session->getSection(__CLASS__)->$key; | ||
if (!$request || !$url || ($user !== NULL && $user !== $this->user->getId())) { | ||
return; | ||
} | ||
|
||
$request->setFlag($request::RESTORED, TRUE); | ||
$this->session->getFlashSection(__CLASS__)->request = $request; | ||
|
||
$url->setQueryParameter(Http\Session::FLASH_KEY, $this->session->getFlashId()); | ||
return (string) $url; | ||
} | ||
|
||
|
||
/** | ||
* Returns stored request. | ||
* @return Request|NULL | ||
*/ | ||
public function restore() | ||
{ | ||
return $this->session->getFlashId() | ||
? $this->session->getFlashSection(__CLASS__)->request | ||
: NULL; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\Application\RequestStorage | ||
* | ||
* @author Martin Major | ||
*/ | ||
|
||
use Nette\Http, | ||
Nette\Application, | ||
Nette\Security\Identity, | ||
Tester\Assert; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
require __DIR__ . '/mocks.php'; | ||
|
||
|
||
$url = 'https://www.example.com/my/address?with=parameter'; | ||
|
||
$httpRequest = new Http\Request(new Http\UrlScript($url)); | ||
|
||
$session = new MockSession($httpRequest, new Http\Response); | ||
$session->mockSection = new MockSessionSection; | ||
$session->mockFlashSection = new MockSessionSection; | ||
|
||
$user = new MockUser; | ||
$user->mockIdentity = new Identity(42); | ||
|
||
$requestStorage = new Application\RequestStorage($session, $user); | ||
|
||
$applicationRequest = new Application\Request('Presenter', 'action', ['param' => 'value']); | ||
|
||
$key = $requestStorage->store($applicationRequest, $httpRequest->getUrl()); | ||
|
||
|
||
// restore key | ||
Assert::null($requestStorage->getUrl('bad_key')); | ||
|
||
$redirect = $requestStorage->getUrl($key); | ||
Assert::same($url . '&_fid=x', $redirect); | ||
|
||
|
||
// redirect to original URL | ||
$httpRequest = new Http\Request(new Http\UrlScript($redirect)); | ||
|
||
$application = new Application\Application(new MockPresenterFactory, new MockRouter, $httpRequest, new MockResponse, $requestStorage); | ||
|
||
$applicationRequest->setFlag(Application\Request::RESTORED); | ||
Assert::equal($applicationRequest, $application->createInitialRequest()); |
Oops, something went wrong.