8000 feature #13034 [HttpKernel] [WebProfilerBundle] added HTTP status to … · symfony/symfony@a962914 · GitHub
[go: up one dir, main page]

Skip to content

Commit a962914

Browse files
committed
feature #13034 [HttpKernel] [WebProfilerBundle] added HTTP status to profiler search result (xelaris)
This PR was squashed before being merged into the 2.7 branch (closes #13034). Discussion ---------- [HttpKernel] [WebProfilerBundle] added HTTP status to profiler search result | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | #12944 | License | MIT | Doc PR | With this PR HTTP status codes are provided in the search results to simplify identification of particular requests. For the schema-less storage implementations (memcache(d), redis, mongodb) and the default file storage, it should work without purging existing profiles. But the code could be simplified, if it is an option to expect old profiles to be purged. For database driven storages (mysql, sqlite), the `sf_profiler_data` table must be dropped anyway (if the table isn't altered manually by adding the `status_code` column). Thus the changes are not fully BC. ![profiler-search-results](https://cloud.githubusercontent.com/assets/2466932/5499453/3cc6ad70-8730-11e4-81e5-13bfd1140d00.png) Commits ------- 34ecda5 [HttpKernel] [WebProfilerBundle] added HTTP status to profiler search result
2 parents 3ed00a8 + 34ecda5 commit a962914

File tree

15 files changed

+142
-11
lines changed

15 files changed

+142
-11
lines changed

src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.7.0
5+
-----
6+
7+
* [BC BREAK] if you are using a DB to store profiles, the table must be dropped
8+
* added the HTTP status code to profiles
9+
410
2.3.0
511
-----
612

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<th scope="col">Method</th>
1313
<th scope="col">URL</th>
1414
<th scope="col">Time</th>
15+
<th scope="col">Status</th>
1516
</tr>
1617
</thead>
1718
<tbody>
@@ -22,6 +23,13 @@
2223
<td>{{ elements.method }}</td>
2324
<td>{{ elements.url }}</td>
2425
<td>{{ elements.time|date('r') }}</td>
26+
<td>
27+
{% if elements.status_code is defined and elements.status_code %}
28+
{{ elements.status_code }}
29+
{% else %}
30+
unknown
31+
{% endif %}
32+
</td>
2533
</tr>
2634
{% endfor %}
2735
</tbody>

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,66 @@ public function testReturns404onTokenNotFound()
7373
$response = $controller->toolbarAction(Request::create('/_wdt/notFound'), 'notFound');
7474
$this->assertEquals(404, $response->getStatusCode());
7575
}
76+
77+
public function testSearchResult()
78+
{
79+
$urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
80+
$twig = $this->getMock('Twig_Environment');
81+
$profiler = $this
82+
->getMockBuilder('Symfony\Component\HttpKernel\Profiler\Profiler')
83+
->disableOriginalConstructor()
84+
->getMock();
85+
86+
$controller = new ProfilerController($urlGenerator, $profiler, $twig, array());
87+
88+
$tokens = array(
89+
array(
90+
'token' => 'token1',
91+
'ip' => '127.0.0.1',
92+
'method' => 'GET',
93+
'url' => 'http://example.com/',
94+
'time' => 0,
95+
'parent' => null,
96+
'status_code' => 200,
97+
),
98+
array(
99+
'token' => 'token2',
100+
'ip' => '127.0.0.1',
101+
'method' => 'GET',
102+
'url' => 'http://example.com/not_found',
103+
'time' => 0,
104+
'parent' => null,
105+
'status_code' => 404,
106+
),
107+
);
108+
$profiler
109+
->expects($this->once())
110+
->method('find')
111+
->will($this->returnValue($tokens));
112+
113+
$twig->expects($this->once())
114+
->method('render')
115+
->with($this->stringEndsWith('results.html.twig'), $this->equalTo(array(
116+
'token' => 'empty',
117+
'profile' => null,
118+
'tokens' => $tokens,
119+
'ip' => '127.0.0.1',
120+
'method' => 'GET',
121+
'url' => 'http://example.com/',
122+
'start' => null,
123+
'end' => null,
124+
'limit' => 2,
125+
'panel' => null,
126+
)));
127+
128+
$response = $controller->searchResultsAction(
129+
Request::create(
130+
'/_profiler/empty/search/results',
131+
'GET',
132+
array('limit' => 2, 'ip' => '127.0.0.1', 'method' => 'GET', 'url' => 'http://example.com/')
133+
),
134+
'empty'
135+
);
136+
$this->assertEquals(200, $response->getStatusCode());
137+
}
76138
}

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.7.0
5+
-----
6+
7+
* added the HTTP status code to profiles
8+
49
2.6.0
510
-----
611

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
6161
continue;
6262
}
6363

64-
list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = explode("\t", $item, 6);
64+
$values = explode("\t", $item, 7);
65+
list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = $values;
66+
$statusCode = isset($values[6]) ? $values[6] : null;
6567

6668
$itemTime = (int) $itemTime;
6769

@@ -84,6 +86,7 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
8486
'url' => $itemUrl,
8587
'time' => $itemTime,
8688
'parent' => $itemParent,
89+
'status_code' => $statusCode,
8790
);
8891
--$limit;
8992
}
@@ -176,6 +179,7 @@ public function write(Profile $profile)
176179
$profile->getUrl(),
177180
$profile->getTime(),
178181
$profile->getParentToken(),
182+
$profile->getStatusCode(),
179183
))."\n";
180184

181185
return $this->appendValue($indexName, $indexRow, $this->lifetime);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
6161

6262
$result = array();
6363
while (count($result) < $limit && $line = $this->readLineFromFile($file)) {
64-
list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent) = str_getcsv($line);
64+
$values = str_getcsv($line);
65+
list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent) = $values;
66+
$csvStatusCode = isset($values[6]) ? $values[6] : null;
6567

6668
$csvTime = (int) $csvTime;
6769

@@ -84,6 +86,7 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
8486
'url' => $csvUrl,
8587
'time' => $csvTime,
8688
'parent' => $csvParent,
89+
'status_code' => $csvStatusCode,
8790
);
8891
}
8992

@@ -167,6 +170,7 @@ public function write(Profile $profile)
167170
$profile->getUrl(),
168171
$profile->getTime(),
169172
$profile->getParentToken(),
173+
$profile->getStatusCode(),
170174
));
171175
fclose($file);
172176
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct($dsn, $username = '', $password = '', $lifetime = 86
3636
*/
3737
public function find($ip, $url, $limit, $method, $start = null, $end = null)
3838
{
39-
$cursor = $this->getMongo()->find($this->buildQuery($ip, $url, $method, $start, $end), array('_id', 'parent', 'ip', 'method', 'url', 'time'))->sort(array('time' => -1))->limit($limit);
39+
$cursor = $this->getMongo()->find($this->buildQuery($ip, $url, $method, $start, $end), array('_id', 'parent', 'ip', 'method', 'url', 'time', 'status_code'))->sort(array('time' => -1))->limit($limit);
4040

4141
$tokens = array();
4242
foreach ($cursor as $profile) {
@@ -83,6 +83,7 @@ public function write(Profile $profile)
8383
'method' => $profile->getMethod(),
8484
'url' => $profile->getUrl(),
8585
'time' => $profile->getTime(),
86+
'status_code' => $profile->getStatusCode(),
8687
);
8788

8889
$result = $this->getMongo()->update(array('_id' => $profile->getToken()), array_filter($record, function ($v) { return !empty($v); }), array('upsert' => true));
@@ -212,6 +213,7 @@ private function getData(array $data)
212213
'url' => isset($data['url']) ? $data['url'] : null,
213214
'time' => isset($data['time']) ? $data['time'] : null,
214215
'data' => isset($data['data']) ? $data['data'] : null,
216+
'status_code' => isset($data['status_code']) ? $data['status_code'] : null,
215217
);
216218
}
217219

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function initDb()
3333
}
3434

3535
$db = new \PDO($this->dsn, $this->username, $this->password);
36-
$db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token VARCHAR(255) PRIMARY KEY, data LONGTEXT, ip VARCHAR(64), method VARCHAR(6), url VARCHAR(255), time INTEGER UNSIGNED, parent VARCHAR(255), created_at INTEGER UNSIGNED, KEY (created_at), KEY (ip), KEY (method), KEY (url), KEY (parent))');
36+
$db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token VARCHAR(255) PRIMARY KEY, data LONGTEXT, ip VARCHAR(64), method VARCHAR(6), url VARCHAR(255), time INTEGER UNSIGNED, parent VARCHAR(255), created_at INTEGER UNSIGNED, status_code SMALLINT UNSIGNED, KEY (created_at), KEY (ip), KEY (method), KEY (url), KEY (parent))');
3737

3838
$this->db = $db;
3939
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
5959
$criteria = $criteria ? 'WHERE '.implode(' AND ', $criteria) : '';
6060

6161
$db = $this->initDb();
62-
$tokens = $this->fetch($db, 'SELECT token, ip, method, url, time, parent FROM sf_profiler_data '.$criteria.' ORDER BY time DESC LIMIT '.((int) $limit), $args);
62+
$tokens = $this->fetch($db, 'SELECT token, ip, method, url, time, parent, status_code FROM sf_profiler_data '.$criteria.' ORDER BY time DESC LIMIT '.((int) $limit), $args);
6363
$this->close($db);
6464

6565
return $tokens;
@@ -94,13 +94,14 @@ public function write(Profile $profile)
9494
':url' => $profile->getUrl(),
9595
':time' => $profile->getTime(),
9696
':created_at' => time(),
97+
':status_code' => $profile->getStatusCode(),
9798
);
9899

99100
try {
100101
if ($this->has($profile->getToken())) {
101-
$this->exec($db, 'UPDATE sf_profiler_data SET parent = :parent, data = :data, ip = :ip, method = :method, url = :url, time = :time, created_at = :created_at WHERE token = :token', $args);
102+
$this->exec($db, 'UPDATE sf_profiler_data SET parent = :parent, data = :data, ip = :ip, method = :method, url = :url, time = :time, created_at = :created_at, status_code = :status_code WHERE token = :token', $args);
102103
} else {
103-
$this->exec($db, 'INSERT INTO sf_profiler_data (token, parent, data, ip, method, url, time, created_at) VALUES (:token, :parent, :data, :ip, :method, :url, :time, :created_at)', $args);
104+
$this->exec($db, 'INSERT INTO sf_profiler_data (token, parent, data, ip, method, url, time, created_at, status_code) VALUES (:token, :parent, :data, :ip, :method, :url, :time, :created_at, :status_code)', $args);
104105
}
105106
$this->cleanup();
106107
$status = true;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Profile
3131
private $method;
3232
private $url;
3333
private $time;
34+
private $statusCode;
3435

3536
/**
3637
* @var Profile
@@ -171,6 +172,22 @@ public function setTime($time)
171172
$this->time = $time;
172173
}
173174

175+
/**
176+
* @param int $statusCode
177+
*/
178+
public function setStatusCode($statusCode)
179+
{
180+
$this->statusCode = $statusCode;
181+
}
182+
183+
/**
184+
* @return int
185+
*/
186+
public function getStatusCode()
187+
{
188+
return $this->statusCode;
189+
}
190+
174191
/**
175192
* Finds children profilers.
176193
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public function collect(Request $request, Response $response, \Exception $except
202202
$profile->setUrl($request->getUri());
203203
$profile->setIp($request->getClientIp());
204204
$profile->setMethod($request->getMethod());
205+
$profile->setStatusCode($response->getStatusCode());
205206

206207
$response->headers->set('X-Debug-Token', $profile->getToken());
207208

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
7171
continue;
7272
}
7373

74-
list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = explode("\t", $item, 6);
74+
$values = explode("\t", $item, 7);
75+
list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = $values;
76+
$statusCode = isset($values[6]) ? $values[6] : null;
7577

7678
$itemTime = (int) $itemTime;
7779

@@ -94,6 +96,7 @@ public function find($ip, $url, $limit, $method, $start = null, $end = null)
9496
'url' => $itemUrl,
9597
'time' => $itemTime,
9698
'parent' => $itemParent,
99+
'status_code' => $statusCode,
97100
);
98101
--$limit;
99102
}
@@ -182,6 +185,7 @@ public function write(Profile $profile)
182185
$profile->getUrl(),
183186
$profile->getTime(),
184187
$profile->getParentToken(),
188+
$profile->getStatusCode(),
185189
))."\n";
186190

187191
return $this->appendValue($indexName, $indexRow, $this->lifetime);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function initDb()
4040
}
4141

4242
$db->exec('PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY;');
43-
$db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token STRING, data STRING, ip STRING, method STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER)');
43+
$db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token STRING, data STRING, ip STRING, method STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER, status_code INTEGER)');
4444
$db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON sf_profiler_data (created_at)');
4545
$db->exec('CREATE INDEX IF NOT EXISTS data_ip ON sf_profiler_data (ip)');
4646
$db->exec('CREATE INDEX IF NOT EXISTS data_method ON sf_profiler_data (method)');

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,22 @@ public function testDuplicates()
246246
$this->assertCount(3, $this->getStorage()->find('127.0.0.1', 'http://example.net/', 3, 'GET'), '->find() method returns incorrect number of entries');
247247
}
248248

249+
public function testStatusCode()
250+
{
251+
$profile = new Profile('token1');
252+
$profile->setStatusCode(200);
253+
$this->getStorage()->write($profile);
254+
255+
$profile = new Profile('token2');
256+
$profile->setStatusCode(404);
257+
$this->getStorage()->write($profile);
258+
259+
$tokens = $this->getStorage()->find('', '', 10, '');
260+
$this->assertCount(2, $tokens);
261+
$this->assertContains($tokens[0]['status_code'], array(200, 404));
262+
$this->assertContains($tokens[1]['status_code'], array(200, 404));
263+
}
264+
249265
/**
250266
* @return \Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface
251267
*/

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ public function testCollect()
2626
{
2727
$request = new Request();
2828
$request->query->set('foo', 'bar');
29-
$response = new Response();
29+
$response = new Response('', 204);
3030
$collector = new RequestDataCollector();
3131

3232
$profiler = new Profiler($this->storage);
3333
$profiler->add($collector);
3434
$profile = $profiler->collect($request, $response);
3535

36-
$profile = $profiler->loadProfile($profile->getToken());
36+
$this->assertSame(204, $profile->getStatusCode());
37+
$this->assertSame('GET', $profile->getMethod());
3738
$this->assertEquals(array('foo' => 'bar'), $profiler->get('request')->getRequestQuery()->all());
3839
}
3940

0 commit comments

Comments
 (0)
0