Skip to content

Commit

Permalink
Webprofiler add status code to search form
Browse files Browse the repository at this point in the history
  • Loading branch information
oktapodia authored and fabpot committed Feb 9, 2016
1 parent 80f3410 commit 7d3700a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 16 deletions.
Expand Up @@ -216,6 +216,7 @@ public function searchBarAction(Request $request)
if (null === $session = $request->getSession()) {
$ip =
$method =
$statusCode =
$url =
$start =
$end =
Expand All @@ -224,6 +225,7 @@ public function searchBarAction(Request $request)
} else {
$ip = $request->query->get('ip', $session->get('_profiler_search_ip'));
$method = $request->query->get('method', $session->get('_profiler_search_method'));
$statusCode = $request->query->get('status_code', $session->get('_profiler_search_status_code'));
$url = $request->query->get('url', $session->get('_profiler_search_url'));
$start = $request->query->get('start', $session->get('_profiler_search_start'));
$end = $request->query->get('end', $session->get('_profiler_search_end'));
Expand All @@ -236,6 +238,7 @@ public function searchBarAction(Request $request)
'token' => $token,
'ip' => $ip,
'method' => $method,
'status_code' => $statusCode,
'url' => $url,
'start' => $start,
'end' => $end,
Expand Down Expand Up @@ -269,6 +272,7 @@ public function searchResultsAction(Request $request, $token)

$ip = $request->query->get('ip');
$method = $request->query->get('method');
$statusCode = $request->query->get('status_code');
$url = $request->query->get('url');
$start = $request->query->get('start', null);
$end = $request->query->get('end', null);
Expand All @@ -278,9 +282,10 @@ public function searchResultsAction(Request $request, $token)
'request' => $request,
'token' => $token,
'profile' => $profile,
'tokens' => $this->profiler->find($ip, $url, $limit, $method, $start, $end),
'tokens' => $this->profiler->find($ip, $url, $limit, $method, $start, $end, $statusCode),
'ip' => $ip,
'method' => $method,
'status_code' => $statusCode,
'url' => $url,
'start' => $start,
'end' => $end,
Expand Down Expand Up @@ -308,6 +313,7 @@ public function searchAction(Request $request)

$ip = preg_replace('/[^:\d\.]/', '', $request->query->get('ip'));
$method = $request->query->get('method');
$statusCode = $request->query->get('status_code');
$url = $request->query->get('url');
$start = $request->query->get('start', null);
$end = $request->query->get('end', null);
Expand All @@ -317,6 +323,7 @@ public function searchAction(Request $request)
if (null !== $session = $request->getSession()) {
$session->set('_profiler_search_ip', $ip);
$session->set('_profiler_search_method', $method);
$session->set('_profiler_search_status_code', $statusCode);
$session->set('_profiler_search_url', $url);
$session->set('_profiler_search_start', $start);
$session->set('_profiler_search_end', $end);
Expand All @@ -328,12 +335,13 @@ public function searchAction(Request $request)
return new RedirectResponse($this->generator->generate('_profiler', array('token' => $token)), 302, array('Content-Type' => 'text/html'));
}

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

return new RedirectResponse($this->generator->generate('_profiler_search_results', array(
'token' => $tokens ? $tokens[0]['token'] : 'empty',
'ip' => $ip,
'method' => $method,
'status_code' => $statusCode,
'url' => $url,
'start' => $start,
'end' => $end,
Expand Down
Expand Up @@ -15,6 +15,11 @@
</select>
</div>

<div class="form-group">
<label for="status_code">Status</label>
<input type="number" name="status_code" id="status_code" value="{{ status_code }}">
</div>

<div class="form-group">
<label for="url">URL</label>
<input type="text" name="url" id="url" value="{{ url }}">
Expand Down
Expand Up @@ -123,6 +123,7 @@ public function testSearchResult()
'tokens' => $tokens,
'ip' => '127.0.0.1',
'method' => 'GET',
'status_code' => null,
'url' => 'http://example.com/',
'start' => null,
'end' => null,
Expand Down
Expand Up @@ -49,7 +49,7 @@ public function __construct($dsn)
/**
* {@inheritdoc}
*/
public function find($ip, $url, $limit, $method, $start = null, $end = null)
public function find($ip, $url, $limit, $method, $start = null, $end = null, $statusCode = null)
{
$file = $this->getIndexFilename();

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

list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent, $csvStatusCode) = $values;
$csvTime = (int) $csvTime;

if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method)) {
if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method) || $statusCode && false === strpos($csvStatusCode, $statusCode)) {
continue;
}

Expand Down Expand Up @@ -154,6 +152,7 @@ public function write(Profile $profile)
'method' => $profile->getMethod(),
'url' => $profile->getUrl(),
'time' => $profile->getTime(),
'status_code' => $profile->getStatusCode(),
);

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

if (!$parent && $data['parent']) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/Profiler/Profile.php
Expand Up @@ -287,6 +287,6 @@ public function hasCollector($name)

public function __sleep()
{
return array('token', 'parent', 'children', 'collectors', 'ip', 'method', 'url', 'time');
return array('token', 'parent', 'children', 'collectors', 'ip', 'method', 'url', 'time', 'statusCode');
}
}
17 changes: 9 additions & 8 deletions src/Symfony/Component/HttpKernel/Profiler/Profiler.php
Expand Up @@ -134,20 +134,21 @@ public function purge()
/**
* Finds profiler tokens for the given criteria.
*
* @param string $ip The IP
* @param string $url The URL
* @param string $limit The maximum number of tokens to return
* @param string $method The request method
* @param string $start The start date to search from
* @param string $end The end date to search to
* @param string $ip The IP
* @param string $url The URL
* @param string $limit The maximum number of tokens to return
* @param string $method The request method
* @param string $start The start date to search from
* @param string $end The end date to search to
* @param string $statusCode The request status code
*
* @return array An array of tokens
*
* @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
*/
public function find($ip, $url, $limit, $method, $start, $end)
public function find($ip, $url, $limit, $method, $start, $end, $statusCode = null)
{
return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end));
return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end), $statusCode);
}

/**
Expand Down
Expand Up @@ -127,6 +127,20 @@ public function testRetrieveByIp()
$this->assertCount(0, $this->storage->find('127.0._.1', '', 10, 'GET'), '->find() does not interpret a "_" as a wildcard in the IP');
}

public function testRetrieveByStatusCode()
{
$profile200 = new Profile('statuscode200');
$profile200->setStatusCode(200);
$this->storage->write($profile200);

$profile404 = new Profile('statuscode404');
$profile404->setStatusCode(404);
$this->storage->write($profile404);

$this->assertCount(1, $this->storage->find(null, null, 10, null, null, null, '200'), '->find() retrieve a record by Status code 200');
$this->assertCount(1, $this->storage->find(null, null, 10, null, null, null, '404'), '->find() retrieve a record by Status code 404');
}

public function testRetrieveByUrl()
{
$profile = new Profile('simple_quote');
Expand Down
Expand Up @@ -59,6 +59,13 @@ public function testFindWorksWithInvalidDates()
$this->assertCount(0, $profiler->find(null, null, null, null, 'some string', ''));
}

public function testFindWorksWithStatusCode()
{
$profiler = new Profiler($this->storage);

$this->assertCount(0, $profiler->find(null, null, null, null, null, null, '204'));
}

protected function setUp()
{
$this->tmp = tempnam(sys_get_temp_dir(), 'sf2_profiler');
Expand Down

0 comments on commit 7d3700a

Please sign in to comment.