8000 feature #17125 Webprofiler add status code to search form (oktapodia) · symfony/symfony@33168b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 33168b0

Browse files
committed
feature #17125 Webprofiler add status code to search form (oktapodia)
This PR was submitted for the 3.0 branch but it was merged into the 3.1-dev branch instead (closes #17125). Discussion ---------- Webprofiler add status code to search form | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13034 | License | MIT With this PR, in the web profiler, you can filter by HTTP status codes. *Before filter* ![image](https://i.gyazo.com/8cb99295b12489cc9024ccc07fabf805.png) *After filter* ![image](https://i.gyazo.com/4caaf032b56ccfe84198a230dbb211a2.png) Commits ------- 7d3700a Webprofiler add status code to search form
2 parents 80f3410 + 7d3700a commit 33168b0

File tree

8 files changed

+52
-16
lines changed

8 files changed

+52
-16
lines changed

src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public function searchBarAction(Request $request)
216216
if (null === $session = $request->getSession()) {
217217
$ip =
218218
$method =
219+
$statusCode =
219220
$url =
220221
$start =
221222
$end =
@@ -224,6 +225,7 @@ public function searchBarAction(Request $request)
224225
} else {
225226
$ip = $request->query->get('ip', $session->get('_profiler_search_ip'));
226227
$method = $request->query->get('method', $session->get('_profiler_search_method'));
228+
$statusCode = $request->query->get('status_code', $session->get('_profiler_search_status_code'));
227229
$url = $request->query->get('url', $session->get('_profiler_search_url'));
228230
$start = $request->query->get('start', $session->get('_profiler_search_start'));
229231
$end = $request->query->get('end', $session->get('_profiler_search_end'));
@@ -236,6 +238,7 @@ public function searchBarAction(Request $request)
236238
'token' => $token,
237239
'ip' => $ip,
238240
'method' => $method,
241+
'status_code' => $statusCode,
239242
'url' => $url,
240243
'start' => $start,
241244
'end' => $end,
@@ -269,6 +272,7 @@ public function searchResultsAction(Request $request, $token)
269272

270273
$ip = $request->query->get('ip');
271274
$method = $request->query->get('method');
275+
$statusCode = $request->query->get('status_code');
272276
$url = $request->query->get('url');
273277
$start = $request->query->get('start', null);
274278
$end = $request->query->get('end', null);
@@ -278,9 +282,10 @@ public function searchResultsAction(Request $request, $token)
278282
'request' => $request,
279283
'token' => $token,
280284
'profile' => $profile,
281-
'tokens' => $this->profiler->find($ip, $url, $limit, $method, $start, $end),
285+
'tokens' => $this->profiler->find($ip, $url, $limit, $method, $start, $end, $statusCode),
282286
'ip' => $ip,
283287
'method' => $method,
288+
'status_code' => $statusCode,
284289
'url' => $url,
285290
'start' => $start,
286291
'end' => $end,
@@ -308,6 +313,7 @@ public function searchAction(Request $request)
308313

309314
$ip = preg_replace('/[^:\d\.]/', '', $request->query->get('ip'));
310315
$method = $request->query->get('method');
316+
$statusCode = $request->query->get('status_code');
311317
$url = $request->query->get('url');
312318
$start = $request->query->get('start', null);
313319
$end = $request->query->get('end', null);
@@ -317,6 +323,7 @@ public function searchAction(Request $request)
317323
if (null !== $session = $request->getSession()) {
318324
$session->set('_profiler_search_ip', $ip);
319325
$session->set('_profiler_search_method', $method);
326+
$session->set('_profiler_search_status_code', $statusCode);
320327
$session->set('_profiler_search_url', $url);
321328
$session->set('_profiler_search_start', $start);
322329
$session->set('_profiler_search_end', $end);
@@ -328,12 +335,13 @@ public function searchAction(Request $request)
328335
return new RedirectResponse($this->generator->generate('_profiler', array('token' => $token)), 302, array('Content-Type' => 'text/html'));
329336
}
330337

331-
$tokens = $this->profiler->find($ip, $url, $limit, $method, $start, $end);
338+
$tokens = $this->profiler->find($ip, $url, $limit, $method, $start, $end, $statusCode);
332339

333340
return new RedirectResponse($this->generator->generate('_profiler_search_results', array(
334341
'token' => $tokens ? $tokens[0]['token'] : 'empty',
335342
'ip' => $ip,
336343
'method' => $method,
344+
'status_code' => $statusCode,
337345
'url' => $url,
338346
'start' => $start,
339347
'end' => $end,

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/search.html.twig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
</select>
1616
</div>
1717

18+
<div class="form-group">
19+
<label for="status_code">Status</label>
20+
<input type="number" name="status_code" id="status_code" value="{{ status_code }}">
21+
</div>
22+
1823
<div class="form-group">
1924
<label for="url">URL</label>
2025
<input type="text" name="url" id="url" value="{{ url }}">

src/Symfony/Bundle/WebProfilerBundle/Tests/Controller/ProfilerControllerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function testSearchResult()
123123
'tokens' => $tokens,
124124
'ip' => '127.0.0.1',
125125
'method' => 'GET',
126+
'status_code' => null,
126127
'url' => 'http://example.com/',
127128
'start' => null,
128129
'end' => null,

src/Symfony/Component/HttpKernel/Profiler/FileProfilerStorage.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct($dsn)
4949
/**
5050
* {@inheritdoc}
5151
*/
52-
public function find($ip, $url, $limit, $method, $start = null, $end = null)
52+
public function find($ip, $url, $limit, $method, $start = null, $end = null, $statusCode = null)
5353
{
5454
$file = $this->getIndexFilename();
5555

@@ -63,12 +63,10 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
6363
$result = array();
6464
while (count($result) < $limit && $line = $this->readLineFromFile($file)) {
6565
$values = str_getcsv($line);
66-
list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent) = $values;
67-
$csvStatusCode = isset($values[6]) ? $values[6] : null;
68-
66+
list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode) = $values;
6967
$csvTime = (int) $csvTime;
7068

71-
if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method)) {
69+
if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method) || $statusCode && false === strpos($csvStatusCode, $statusCode)) {
7270
continue;
7371
}
7472

@@ -154,6 +152,7 @@ public function write(Profile $profile)
154152
'method' => $profile->getMethod(),
155153
'url' => $profile->getUrl(),
156154
'time' => $profile->getTime(),
155+
'status_code' => $profile->getStatusCode(),
157156
);
158157

159158
if (false === file_put_contents($file, serialize($data))) {
@@ -261,6 +260,7 @@ protected function createProfileFromData($token, $data, $parent = null)
261260
$profile->setMethod($data['method']);
262261
$profile->setUrl($data['url']);
263262
$profile->setTime($data['time']);
263+
$profile->setStatusCode($data['status_code']);
264264
$profile->setCollectors($data['data']);
265265

266266
if (!$parent && $data['parent']) {

src/Symfony/Component/HttpKernel/Profiler/Profile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,6 @@ public function hasCollector($name)
287287

288288
public function __sleep()
289289
{
290-
return array('token', 'parent', 'children', 'collectors', 'ip', 'method', 'url', 'time');
290+
return array('token', 'parent', 'children', 'collectors', 'ip', 'method', 'url', 'time', 'statusCode');
291291
}
292292
}

src/Symfony/Component/HttpKernel/Profiler/Profiler.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,21 @@ public function purge()
134134
/**
135135
* Finds profiler tokens for the given criteria.
136136
*
137-
* @param string $ip The IP
138-
* @param string $url The URL
139-
* @param string $limit The maximum number of tokens to return
140-
* @param string $method The request method
141-
* @param string $start The start date to search from
142-
* @param string $end The end date to search to
137+
* @param string $ip The IP
138+
* @param string $url The URL
139+
* @param string $limit The maximum number of tokens to return
140+
* @param string $method The request method
141+
* @param string $start The start date to search from
142+
* @param string $end The end date to search to
143+
* @param string $statusCode The request status code
143144
*
144145
* @return array An array of tokens
145146
*
146147
* @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
147148
*/
148-
public function find($ip, $url, $limit, $method, $start, $end)
149+
public function find($ip, $url, $limit, $method, $start, $end, $statusCode = null)
149150
{
150-
return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end));
151+
return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end), $statusCode);
151152
}
152153

153154
/**

src/Symfony/Component/HttpKernel/Tests/Profiler/FileProfilerStorageTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ public function testRetrieveByIp()
127127
$this->assertCount(0, $this->storage->find('127.0._.1', '', 10, 'GET'), '->find() does not interpret a "_" as a wildcard in the IP');
128128
}
129129

130+
public function testRetrieveByStatusCode()
131+
{
132+
$profile200 = new Profile('statuscode200');
133+
$profile200->setStatusCode(200);
134+
$this->storage->write($profile200);
135+
136+
$profile404 = new Profile('statuscode404');
137+
$profile404->setStatusCode(404);
138+
$this->storage->write($profile404);
139+
140+
$this->assertCount(1, $this->storage->find(null, null, 10, null, null, null, '200'), '->find() retrieve a record by Status code 200');
141+
$this->assertCount(1, $this->storage->find(null, null, 10, null, null, null, '404'), '->find() retrieve a record by Status code 404');
142+
}
143+
130144
public function testRetrieveByUrl()
131145
{
132146
$profile = new Profile('simple_quote');

src/Symfony/Component/HttpKernel/Tests/Profiler/ProfilerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ public function testFindWorksWithInvalidDates()
5959
$this->assertCount(0, $profiler->find(null, null, null, null, 'some string', ''));
6060
}
6161

62+
public function testFindWorksWithStatusCode()
63+
{
64+
$profiler = new Profiler($this->storage);
65+
66+
$this->assertCount(0, $profiler->find(null, null, null, null, null, null, '204'));
67+
}
68+
6269
protected function setUp()
6370
{
6471
$this->tmp = tempnam(sys_get_temp_dir(), 'sf2_profiler');

0 commit comments

Comments
 (0)
0