Adds collecting info about request method and allowing searching by it

This commit is contained in:
stloyd 2011-07-04 13:53:57 +02:00 committed by Joseph Bielawski
parent 76ba2bc7ac
commit 1aef4e806b
9 changed files with 117 additions and 54 deletions

View File

@ -191,22 +191,25 @@ class ProfilerController extends ContainerAware
$profiler->disable();
if (null === $session = $this->container->get('request')->getSession()) {
$ip =
$url =
$limit =
$token = null;
$ip =
$method =
$url =
$limit =
$token = null;
} else {
$ip = $session->get('_profiler_search_ip');
$url = $session->get('_profiler_search_url');
$limit = $session->get('_profiler_search_limit');
$token = $session->get('_profiler_search_token');
$ip = $session->get('_profiler_search_ip');
$method = $session->get('_profiler_search_method');
$url = $session->get('_profiler_search_url');
$limit = $session->get('_profiler_search_limit');
$token = $session->get('_profiler_search_token');
}
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:search.html.twig', array(
'token' => $token,
'ip' => $ip,
'url' => $url,
'limit' => $limit,
'token' => $token,
'ip' => $ip,
'method' => $method,
'url' => $url,
'limit' => $limit,
));
}
@ -223,15 +226,17 @@ class ProfilerController extends ContainerAware
$profile = $profiler->loadProfile($token);
$ip = $this->container->get('request')->query->get('ip');
$url = $this->container->get('request')->query->get('url');
$limit = $this->container->get('request')->query->get('limit');
$ip = $this->container->get('request')->query->get('ip');
$method = $this->container->get('request')->query->get('method');
$url = $this->container->get('request')->query->get('url');
$limit = $this->container->get('request')->query->get('limit');
return $this->container->get('templating')->renderResponse('WebProfilerBundle:Profiler:results.html.twig', array(
'token' => $token,
'profile' => $profile,
'tokens' => $profiler->find($ip, $url, $limit),
'tokens' => $profiler->find($ip, $url, $limit, $method),
'ip' => $ip,
'method' => $method,
'url' => $url,
'limit' => $limit,
'panel' => null,
@ -250,13 +255,15 @@ class ProfilerController extends ContainerAware
$request = $this->container->get('request');
$ip = preg_replace('/[^\d\.]/', '', $request->query->get('ip'));
$url = $request->query->get('url');
$limit = $request->query->get('limit');
$token = $request->query->get('token');
$ip = preg_replace('/[^\d\.]/', '', $request->query->get('ip'));
$method = $request->query->get('method');
$url = $request->query->get('url');
$limit = $request->query->get('limit');
$token = $request->query->get('token');
if (null !== $session = $request->getSession()) {
$session->set('_profiler_search_ip', $ip);
$session->set('_profiler_search_method', $method);
$session->set('_profiler_search_url', $url);
$session->set('_profiler_search_limit', $limit);
$session->set('_profiler_search_token', $token);
@ -266,13 +273,14 @@ class ProfilerController extends ContainerAware
return new RedirectResponse($this->container->get('router')->generate('_profiler', array('token' => $token)));
}
$tokens = $profiler->find($ip, $url, $limit);
$tokens = $profiler->find($ip, $url, $limit, $method);
return new RedirectResponse($this->container->get('router')->generate('_profiler_search_results', array(
'token' => $tokens ? $tokens[0]['token'] : 'empty',
'ip' => $ip,
'url' => $url,
'limit' => $limit,
'token' => $tokens ? $tokens[0]['token'] : 'empty',
'ip' => $ip,
'method' => $method,
'url' => $url,
'limit' => $limit,
)));
}

View File

@ -8,6 +8,7 @@
<tr>
<th>Token</th>
<th>IP</th>
<th>Method</th>
<th>URL</th>
<th>Time</th>
</tr>
@ -15,6 +16,7 @@
<tr>
<td><a href="{{ path('_profiler', { 'token': elements.token }) }}">{{ elements.token }}</a></td>
<td>{{ elements.ip }}</td>
<td>{{ elements.method }}</td>
<td>{{ elements.url }}</td>
<td>{{ elements.time|date('r') }}</td>
</tr>

View File

@ -7,6 +7,13 @@
<label for="ip">IP</label>
<input type="text" name="ip" id="ip" value="{{ ip }}" />
<div class="clear_fix"></div>
<label for="method">Method</label>
<select name="method" id="method">
{% for m in ['', 'DELETE', 'GET', 'HEAD', 'POST', 'PUT'] %}
<option{{ m == method ? ' selected="selected"' : '' }}>{{ m }}</option>
{% endfor %}
</select>
<div class="clear_fix"></div>
<label for="url">URL</label>
<input type="text" name="url" id="url" value="{{ url }}" />
<div class="clear_fix"></div>

View File

@ -33,7 +33,7 @@ class MysqlProfilerStorage extends PdoProfilerStorage
}
$db = new \PDO($this->dsn, $this->username, $this->password);
$db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token VARCHAR(255) PRIMARY KEY, data LONGTEXT, ip VARCHAR(64), url VARCHAR(255), time INTEGER UNSIGNED, parent VARCHAR(255), created_at INTEGER UNSIGNED, KEY (created_at), KEY (ip), KEY (url), KEY (parent))');
$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))');
$this->db = $db;
}
@ -44,7 +44,7 @@ class MysqlProfilerStorage extends PdoProfilerStorage
/**
* {@inheritdoc}
*/
protected function buildCriteria($ip, $url, $limit)
protected function buildCriteria($ip, $url, $limit, $method)
{
$criteria = array();
$args = array();
@ -59,6 +59,11 @@ class MysqlProfilerStorage extends PdoProfilerStorage
$args[':url'] = '%'.addcslashes($url, '%_\\').'%';
}
if ($method) {
$criteria[] = 'method = :method';
$args[':method'] = $method;
}
return array($criteria, $args);
}
}

View File

@ -45,14 +45,14 @@ abstract class PdoProfilerStorage implements ProfilerStorageInterface
/**
* {@inheritdoc}
*/
public function find($ip, $url, $limit)
public function find($ip, $url, $limit, $method)
{
list($criteria, $args) = $this->buildCriteria($ip, $url, $limit);
list($criteria, $args) = $this->buildCriteria($ip, $url, $limit, $method);
$criteria = $criteria ? 'WHERE '.implode(' AND ', $criteria) : '';
$db = $this->initDb();
$tokens = $this->fetch($db, 'SELECT token, ip, url, time, parent FROM sf_profiler_data '.$criteria.' ORDER BY time DESC LIMIT '.((integer) $limit), $args);
$tokens = $this->fetch($db, 'SELECT token, ip, method, url, time, parent FROM sf_profiler_data '.$criteria.' ORDER BY time DESC LIMIT '.((integer) $limit), $args);
$this->close($db);
return $tokens;
@ -65,7 +65,7 @@ abstract class PdoProfilerStorage implements ProfilerStorageInterface
{
$db = $this->initDb();
$args = array(':token' => $token);
$data = $this->fetch($db, 'SELECT data, parent, ip, url, time FROM sf_profiler_data WHERE token = :token LIMIT 1', $args);
$data = $this->fetch($db, 'SELECT data, parent, ip, method, url, time FROM sf_profiler_data WHERE token = :token LIMIT 1', $args);
$this->close($db);
if (isset($data[0]['data'])) {
return $this->createProfileFromData($token, $data[0]);
@ -85,6 +85,7 @@ abstract class PdoProfilerStorage implements ProfilerStorageInterface
':parent' => $profile->getParent() ? $profile->getParent()->getToken() : '',
':data' => base64_encode(serialize($profile->getCollectors())),
':ip' => $profile->getIp(),
':method' => $profile->getMethod(),
':url' => $profile->getUrl(),
':time' => $profile->getTime(),
':created_at' => time(),
@ -92,7 +93,7 @@ abstract class PdoProfilerStorage implements ProfilerStorageInterface
if ($this->read($profile->getToken())) {
try {
$this->exec($db, 'UPDATE sf_profiler_data SET parent = :parent, data = :data, ip = :ip, url = :url, time = :time, created_at = :created_at WHERE token = :token', $args);
$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);
$this->cleanup();
$status = true;
} catch (\Exception $e) {
@ -100,7 +101,7 @@ abstract class PdoProfilerStorage implements ProfilerStorageInterface
}
} else {
try {
$this->exec($db, 'INSERT INTO sf_profiler_data (token, parent, data, ip, url, time, created_at) VALUES (:token, :parent, :data, :ip, :url, :time, :created_at)', $args);
$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);
$this->cleanup();
$status = true;
} catch (\Exception $e) {
@ -129,10 +130,11 @@ abstract class PdoProfilerStorage implements ProfilerStorageInterface
* @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
*
* @return array An array with (criteria, args)
*/
abstract protected function buildCriteria($ip, $url, $limit);
abstract protected function buildCriteria($ip, $url, $limit, $method);
/**
* Initializes the database
@ -198,10 +200,11 @@ abstract class PdoProfilerStorage implements ProfilerStorageInterface
$profile = new Profile($token);
$profile->setIp($data['ip']);
$profile->setUrl($data['url']);
$profile->setMethod($data['method']);
$profile->setTime($data['time']);
$profile->setCollectors(unserialize(base64_decode($data['data'])));
if (!$parent && isset($data['parent']) && $data['parent']) {
if (!$parent && !empty($data['parent'])) {
$parent = $this->read($data['parent']);
}
@ -218,13 +221,14 @@ abstract class PdoProfilerStorage implements ProfilerStorageInterface
* Reads the child profiles for the given token.
*
* @param string $token The parent token
* @param string $parent The parent instance
*
* @return array An array of Profile instance
*/
protected function readChildren($token, $parent)
{
$db = $this->initDb();
$data = $this->fetch($db, 'SELECT token, data, ip, url, time FROM sf_profiler_data WHERE parent = :token', array(':token' => $token));
$data = $this->fetch($db, 'SELECT token, data, ip, method, url, time FROM sf_profiler_data WHERE parent = :token', array(':token' => $token));
$this->close($db);
if (!$data) {

View File

@ -23,11 +23,17 @@ class Profile implements \Serializable
private $token;
private $collectors;
private $ip;
private $method;
private $url;
private $time;
private $parent;
private $children;
/**
* Constructor.
*
* @param string $token The token
*/
public function __construct($token)
{
$this->token = $token;
@ -90,6 +96,21 @@ class Profile implements \Serializable
$this->ip = $ip;
}
/**
* Returns the request method.
*
* @return string The request method
*/
public function getMethod()
{
return $this->method;
}
public function setMethod($method)
{
$this->method = $method;
}
/**
* Returns the URL.
*
@ -138,6 +159,11 @@ class Profile implements \Serializable
}
}
/**
* Adds the child token
*
* @param Profile $child The child Profile
*/
public function addChild(Profile $child)
{
$this->children[] = $child;
@ -178,11 +204,11 @@ class Profile implements \Serializable
public function serialize()
{
return serialize(array($this->token, $this->parent, $this->children, $this->collectors, $this->ip, $this->url, $this->time));
return serialize(array($this->token, $this->parent, $this->children, $this->collectors, $this->ip, $this->method, $this->url, $this->time));
}
public function unserialize($data)
{
list($this->token, $this->parent, $this->children, $this->collectors, $this->ip, $this->url, $this->time) = unserialize($data);
list($this->token, $this->parent, $this->children, $this->collectors, $this->ip, $this->method, $this->url, $this->time) = unserialize($data);
}
}

View File

@ -82,7 +82,7 @@ class Profiler
/**
* Saves a Profile.
*
* @param Profile A Profile instance
* @param Profile $profile A Profile instance
*
* @return Boolean
*/
@ -106,6 +106,8 @@ class Profiler
/**
* Exports the current profiler data.
*
* @param Profile $profile A Profile instance
*
* @return string The exported data
*/
public function export(Profile $profile)
@ -136,15 +138,16 @@ class Profiler
/**
* 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 $ip The IP
* @param string $url The URL
* @param string $limit The maximum number of tokens to return
* @param string $method The request method
*
* @return array An array of tokens
*/
public function find($ip, $url, $limit)
public function find($ip, $url, $limit, $method)
{
return $this->storage->find($ip, $url, $limit);
return $this->storage->find($ip, $url, $limit, $method);
}
/**
@ -166,11 +169,11 @@ class Profiler
$profile->setTime(time());
$profile->setUrl($request->getUri());
$profile->setIp($request->server->get('REMOTE_ADDR'));
$profile->setMethod($request->getMethod());
$response->headers->set('X-Debug-Token', $profile->getToken());
$collectors = array();
foreach ($this->collectors as $name => $collector) {
foreach ($this->collectors as $collector) {
$collector->collect($request, $response, $exception);
// forces collectors to become "read/only" (they loose their object dependencies)
@ -217,6 +220,8 @@ class Profiler
* Returns true if a Collector for the given name exists.
*
* @param string $name A collector name
*
* @return Boolean
*/
public function has($name)
{

View File

@ -21,13 +21,14 @@ interface ProfilerStorageInterface
/**
* 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 $ip The IP
* @param string $url The URL
* @param string $limit The maximum number of tokens to return
* @param string $method The request method
*
* @return array An array of tokens
*/
function find($ip, $url, $limit);
function find($ip, $url, $limit, $method);
/**
* Reads data associated with the given token.

View File

@ -41,9 +41,9 @@ class SqliteProfilerStorage extends PdoProfilerStorage
}
$db->exec('PRAGMA temp_store=MEMORY; PRAGMA journal_mode=MEMORY;');
$db->exec('CREATE TABLE IF NOT EXISTS sf_profiler_data (token STRING, data STRING, ip STRING, url STRING, time INTEGER, parent STRING, created_at INTEGER)');
$db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON sf_profiler_data (created_at)');
$db->exec('CREATE INDEX IF NOT EXISTS data_ip ON sf_profiler_data (ip)');
$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)');
$db->exec('CREATE INDEX IF NOT EXISTS data_created_at ON sf_profiler_data (created_at)'); $db->exec('CREATE INDEX IF NOT EXISTS data_ip ON sf_profiler_data (ip)');
$db->exec('CREATE INDEX IF NOT EXISTS data_method ON sf_profiler_data (method)');
$db->exec('CREATE INDEX IF NOT EXISTS data_url ON sf_profiler_data (url)');
$db->exec('CREATE INDEX IF NOT EXISTS data_parent ON sf_profiler_data (parent)');
$db->exec('CREATE UNIQUE INDEX IF NOT EXISTS data_token ON sf_profiler_data (token)');
@ -97,7 +97,7 @@ class SqliteProfilerStorage extends PdoProfilerStorage
/**
* {@inheritdoc}
*/
protected function buildCriteria($ip, $url, $limit)
protected function buildCriteria($ip, $url, $limit, $method)
{
$criteria = array();
$args = array();
@ -112,6 +112,11 @@ class SqliteProfilerStorage extends PdoProfilerStorage
$args[':url'] = '%'.addcslashes($url, '%_\\').'%';
}
if ($method) {
$criteria[] = 'method = :method';
$args[':method'] = $method;
}
return array($criteria, $args);
}