Skip to content

Commit d6d454a

Browse files
committed
refactor RequestStack to handle empty currentRequest & add popRequest()
1 parent fed4cf8 commit d6d454a

File tree

2 files changed

+166
-26
lines changed

2 files changed

+166
-26
lines changed

src/RequestStack.php

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class RequestStack extends Nette\Object implements Nette\Http\IRequest
2121
{
2222

2323
/**
24-
* @var array|Nette\Http\IRequest
24+
* @var array|Nette\Http\IRequest[]
2525
*/
2626
private $requests = [];
2727

2828
/**
29-
* @var Nette\Http\IRequest
29+
* @var Nette\Http\IRequest|NULL
3030
*/
3131
private $current;
3232

@@ -41,7 +41,24 @@ public function pushRequest(Nette\Http\IRequest $request)
4141

4242

4343
/**
44-
* @return Nette\Http\IRequest
44+
* @return Nette\Http\IRequest|null
45+
*/
46+
public function popRequest()
47+
{
48+
if (count($this->requests) === 0) {
49+
return NULL;
50+
}
51+
52+
$head = array_pop($this->requests);
53+
$current = end($this->requests);
54+
$this->current = ($current !== FALSE) ? $current : NULL;
55+
return $head;
56+
}
57+
58+
59+
60+
/**
61+
* @return Nette\Http\IRequest|NULL
4562
*/
4663
public function getCurrentRequest()
4764
{
@@ -55,7 +72,7 @@ public function getCurrentRequest()
5572
*/
5673
public function getUrl()
5774
{
58-
return $this->current->getUrl();
75+
return $this->current !== NULL ? $this->current->getUrl() : NULL;
5976
}
6077

6178

@@ -66,10 +83,10 @@ public function getUrl()
6683
public function getQuery($key = NULL, $default = NULL)
6784
{
6885
if (func_num_args() === 0) {
69-
return $this->current->getQuery();
86+
return $this->current !== NULL ? $this->current->getQuery() : [];
7087
}
7188

72-
return $this->current->getQuery($key, $default);
89+
return $this->current !== NULL ? $this->current->getQuery($key, $default) : NULL;
7390
}
7491

7592

@@ -80,10 +97,10 @@ public function getQuery($key = NULL, $default = NULL)
8097
public function getPost($key = NULL, $default = NULL)
8198
{
8299
if (func_num_args() === 0) {
83-
return $this->current->getPost();
100+
return $this->current !== NULL ? $this->current->getPost() : [];
84101
}
85102

86-
return $this->current->getPost($key, $default);
103+
return $this->current !== NULL ? $this->current->getPost($key, $default) : NULL;
87104
}
88105

89106

@@ -93,7 +110,7 @@ public function getPost($key = NULL, $default = NULL)
93110
*/
94111
public function getFile($key)
95112
{
96-
return $this->current->getFile($key);
113+
return $this->current !== NULL ? $this->current->getFile($key) : NULL;
97114
}
98115

99116

@@ -103,7 +120,7 @@ public function getFile($key)
103120
*/
104121
public function getFiles()
105122
{
106-
return $this->current->getFiles();
123+
return $this->current !== NULL ? $this->current->getFiles() : [];
107124
}
108125

109126

@@ -113,7 +130,7 @@ public function getFiles()
113130
*/
114131
public function getCookie($key, $default = NULL)
115132
{
116-
return $this->current->getCookie($key, $default);
133+
return $this->current !== NULL ? $this->current->getCookie($key, $default) : NULL;
117134
}
118135

119136

@@ -123,7 +140,7 @@ public function getCookie($key, $default = NULL)
123140
*/
124141
public function getCookies()
125142
{
126-
return $this->current->getCookies();
143+
return $this->current !== NULL ? $this->current->getCookies() : [];
127144
}
128145

129146

@@ -133,7 +150,7 @@ public function getCookies()
133150
*/
134151
public function getMethod()
135152
{
136-
return $this->current->getMethod();
153+
return $this->current !== NULL ? $this->current->getMethod() : NULL;
137154
}
138155

139156

@@ -143,7 +160,7 @@ public function getMethod()
143160
*/
144161
public function isMethod($method)
145162
{
146-
return $this->current->isMethod($method);
163+
return $this->current !== NULL ? $this->current->isMethod($method) : FALSE;
147164
}
148165

149166

@@ -153,7 +170,7 @@ public function isMethod($method)
153170
*/
154171
public function getHeader($header, $default = NULL)
155172
{
156-
return $this->current->getHeader($header, $default);
173+
return $this->current !== NULL ? $this->current->getHeader($header, $default) : NULL;
157174
}
158175

159176

@@ -163,7 +180,7 @@ public function getHeader($header, $default = NULL)
163180
*/
164181
public function getHeaders()
165182
{
166-
return $this->current->getHeaders();
183+
return $this->current !== NULL ? $this->current->getHeaders() : [];
167184
}
168185

169186

@@ -173,7 +190,7 @@ public function getHeaders()
173190
*/
174191
public function isSecured()
175192
{
176-
return $this->current->isSecured();
193+
return $this->current !== NULL ? $this->current->isSecured() : FALSE;
177194
}
178195

179196

@@ -183,7 +200,7 @@ public function isSecured()
183200
*/
184201
public function isAjax()
185202
{
186-
return $this->current->isAjax();
203+
return $this->current !== NULL ? $this->current->isAjax() : FALSE;
187204
}
188205

189206

@@ -193,7 +210,7 @@ public function isAjax()
193210
*/
194211
public function getRemoteAddress()
195212
{
196-
return $this->current->getRemoteAddress();
213+
return $this->current !== NULL ? $this->current->getRemoteAddress() : NULL;
197214
}
198215

199216

@@ -203,7 +220,7 @@ public function getRemoteAddress()
203220
*/
204221
public function getRemoteHost()
205222
{
206-
return $this->current->getRemoteHost();
223+
return $this->current !== NULL ? $this->current->getRemoteHost() : NULL;
207224
}
208225

209226

@@ -213,7 +230,7 @@ public function getRemoteHost()
213230
*/
214231
public function getRawBody()
215232
{
216-
return $this->current->getRawBody();
233+
return $this->current !== NULL ? $this->current->getRawBody() : NULL;
217234
}
218235

219236

@@ -226,19 +243,20 @@ public function getRawBody()
226243
*/
227244
public function detectLanguage(array $langs)
228245
{
229-
if ($this->current instanceof Nette\Http\Request) {
230-
return $this->current->detectLanguage($langs);
231-
}
246+
return $this->current instanceof Nette\Http\Request
247+
? $this->current->detectLanguage($langs)
248+
: NULL;
232249
}
233250

234251

235252

236253
/**
237-
* @return Url|null
254+
* @return Url|NULL
238255
*/
239256
public function getReferer()
240257
{
241-
return ($url = $this->getHeader('referer')) ? new Url($url) : NULL;
258+
$url = $this->getHeader('referer');
259+
return $url !== NULL ? new Url($url) : NULL;
242260
}
243261

244262
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
/**
4+
* @testCase
5+
*/
6+
7+
namespace KdybyTests\RequestStack;
8+
9+
use Kdyby;
10+
use Kdyby\RequestStack\RequestStack;
11+
use KdybyTests;
12+
use Nette;
13+
use Nette\Http\Url;
14+
use Nette\Http\UrlScript;
15+
use Tester;
16+
use Tester\Assert;
17+
18+
require_once __DIR__ . '/../bootstrap.php';
19+
20+
21+
22+
/**
23+
* @author Filip Procházka <[email protected]>
24+
*/
25+
class RequestStackTest extends Tester\TestCase
26+
{
27+
28+
public function testPushAndPop()
29+
{
30+
$stack = new RequestStack();
31+
32+
$url = new UrlScript('https://www.kdyby.org/');
33+
34+
$request1 = new Nette\Http\Request($url);
35+
$stack->pushRequest($request1);
36+
Assert::same($request1, $stack->getCurrentRequest());
37+
38+
$request2 = new Nette\Http\Request($url);
39+
$stack->pushRequest($request2);
40+
Assert::same($request2, $stack->getCurrentRequest());
41+
42+
$head = $stack->popRequest();
43+
Assert::same($request2, $head);
44+
Assert::same($request1, $stack->getCurrentRequest());
45+
46+
$head = $stack->popRequest();
47+
Assert::same($request1, $head);
48+
Assert::null($stack->getCurrentRequest());
49+
}
50+
51+
public function testEmptyStack()
52+
{
53+
$stack = new RequestStack();
54+
55+
Assert::null($stack->getCurrentRequest());
56+
Assert::null($stack->popRequest());
57+
Assert::null($stack->getCurrentRequest());
58+
Assert::null($stack->getUrl());
59+
Assert::same([], $stack->getQuery());
60+
Assert::null($stack->getQuery('foo'));
61+
Assert::same([], $stack->getPost());
62+
Assert::null($stack->getPost('foo'));
63+
Assert::null($stack->getFile('f'));
64+
Assert::same([], $stack->getFiles());
65+
Assert::null($stack->getCookie('c'));
66+
Assert::same([], $stack->getCookies());
67+
Assert::null($stack->getMethod());
68+
Assert::false($stack->isMethod('GET'));
69+
Assert::null($stack->getHeader('Accept-Language'));
70+
Assert::same([], $stack->getHeaders());
71+
Assert::false($stack->isSecured());
72+
Assert::false($stack->isAjax());
73+
Assert::null($stack->getRemoteAddress());
74+
Assert::null($stack->getRemoteHost());
75+
Assert::null($stack->getRawBody());
76+
Assert::null($stack->detectLanguage(['en']));
77+
Assert::null($stack->getReferer());
78+
}
79+
80+
public function testNonEmptyStack()
81+
{
82+
$stack = new RequestStack();
83+
84+
$httpRequest = new Nette\Http\Request(
85+
new UrlScript('https://www.kdyby.org/?hello=no'),
86+
NULL,
87+
['foo' => 'foo'],
88+
['f' => $f = new Nette\Http\FileUpload([])],
89+
['c' => 'C'],
90+
['accept-language' => 'en', 'x-requested-with' => 'XMLHttpRequest', 'referer' => 'https://www.example.com'],
91+
'GET',
92+
'127.0.0.1',
93+
'localhost'
94+
);
95+
$stack->pushRequest($httpRequest);
96+
97+
Assert::same($httpRequest, $stack->getCurrentRequest());
98+
Assert::equal($httpRequest->getUrl(), $stack->getUrl());
99+
Assert::same(['hello' => 'no'], $stack->getQuery());
100+
Assert::same('no', $stack->getQuery('hello'));
101+
Assert::same(['foo' => 'foo'], $stack->getPost());
102+
Assert::same('foo', $stack->getPost('foo'));
103+
Assert::same($f, $stack->getFile('f'));
104+
Assert::same(['f' => $f], $stack->getFiles());
105+
Assert::same('C', $stack->getCookie('c'));
106+
Assert::same(['c' => 'C'], $stack->getCookies());
107+
Assert::same('GET', $stack->getMethod());
108+
Assert::true($stack->isMethod('GET'));
109+
Assert::same(['accept-language' => 'en', 'x-requested-with' => 'XMLHttpRequest', 'referer' => 'https://www.example.com'], $stack->getHeaders());
110+
Assert::same('en', $stack->getHeader('Accept-Language'));
111+
Assert::true($stack->isSecured());
112+
Assert::true($stack->isAjax());
113+
Assert::same('127.0.0.1', $stack->getRemoteAddress());
114+
Assert::same('localhost', $stack->getRemoteHost());
115+
Assert::same('', $stack->getRawBody());
116+
Assert::same('en', $stack->detectLanguage(['en']));
117+
Assert::equal(new Url('https://www.example.com'), $stack->getReferer());
118+
}
119+
120+
}
121+
122+
(new RequestStackTest())->run();

0 commit comments

Comments
 (0)