1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-07-19 07:41:39 +02:00

added support for sending the request id in headers and retreiving the data using the open handler

This commit is contained in:
maximebf
2013-09-23 10:41:54 -04:00
parent 821bf57f2d
commit 8972b82a9a
7 changed files with 118 additions and 12 deletions

View File

@@ -1,6 +1,16 @@
# Changelog # Changelog
1.6: 2013-09-23:
- send the request id in headers and use the open handler to retreive the dataset
- !! modified sendDataAsHeaders() to add $useOpenHandler as the first argument
2013-09-19:
- added HttpDriver
- added jQuery.noConflict() managment
2013-09-15 (1.6):
- added sending data through HTTP headers - added sending data through HTTP headers
- added stacked data - added stacked data

View File

@@ -2,6 +2,6 @@
include 'bootstrap.php'; include 'bootstrap.php';
$debugbar['messages']->addMessage('hello from ajax'); $debugbar['messages']->addMessage('hello from ajax');
$debugbar->sendDataInHeaders(); $debugbar->sendDataInHeaders(true);
?> ?>
hello from AJAX hello from AJAX

View File

@@ -15,8 +15,9 @@ $debugbarRenderer = $debugbar->getJavascriptRenderer()
// //
// create a writable profiles folder in the demo directory to uncomment the following lines // create a writable profiles folder in the demo directory to uncomment the following lines
// //
//$debugbar->setStorage(new DebugBar\Storage\FileStorage(__DIR__ . '/profiles')); // $debugbar->setStorage(new DebugBar\Storage\FileStorage(__DIR__ . '/profiles'));
//$debugbarRenderer->setOpenHandlerUrl('open.php'); // $debugbar->setStorage(new DebugBar\Storage\RedisStorage(new Predis\Client()));
// $debugbarRenderer->setOpenHandlerUrl('open.php');
function render_demo_page(Closure $callback = null) function render_demo_page(Closure $callback = null)
{ {

View File

@@ -25,6 +25,24 @@ of `AjaxHandler` is stored in the `ajaxHandler` property of the `DebugBar` objec
debugbar.ajaxHandler.handle(xhr); debugbar.ajaxHandler.handle(xhr);
If you are sending a lot of data through headers, it may cause problems
with your browser. Instead you can use a storage handler (see Storage chapter)
and the open handler (see Open Handler chapter) to load the data after an ajax
request. Use true as the first argument of `sendDataInHeaders()`.
$debugbar = new DebugBar();
// define a storage
$debugbar->setStorage(new DebugBar\Storage\FileStorage('/path/to/storage'));
// define the open handler url
$renderer = $debugbar->getJavascriptRenderer();
$renderer->setOpenHandlerUrl('open.php');
// ...
$debugbar->sendDataInHeaders(true);
## Stacked data ## Stacked data
Some times you need to collect data about a request but the page won't actually Some times you need to collect data about a request but the page won't actually

View File

@@ -28,6 +28,8 @@ use DebugBar\Storage\StorageInterface;
*/ */
class DebugBar implements ArrayAccess class DebugBar implements ArrayAccess
{ {
public static $useOpenHandlerWhenSendingDataHeaders = false;
protected $collectors = array(); protected $collectors = array();
protected $data; protected $data;
@@ -266,13 +268,23 @@ class DebugBar implements ArrayAccess
/** /**
* Sends the data through the HTTP headers * Sends the data through the HTTP headers
* *
* @param bool $useOpenHandler
* @param string $headerName * @param string $headerName
* @param integer $maxHeaderLength * @param integer $maxHeaderLength
*/ */
public function sendDataInHeaders($headerName = 'phpdebugbar', $maxHeaderLength = 4096) public function sendDataInHeaders($useOpenHandler = null, $headerName = 'phpdebugbar', $maxHeaderLength = 4096)
{ {
$headers = $this->getDataAsHeaders($headerName, $maxHeaderLength); if ($useOpenHandler === null) {
$useOpenHandler = self::$useOpenHandlerWhenSendingDataHeaders;
}
if ($useOpenHandler && $this->storage !== null) {
$this->getData();
$headerName .= '-id';
$headers = array($headerName => $this->getCurrentRequestId());
} else {
$headers = $this->getDataAsHeaders($headerName, $maxHeaderLength);
}
$this->getHttpDriver()->setHeaders($headers); $this->getHttpDriver()->setHeaders($headers);
return $this; return $this;
} }

View File

@@ -323,7 +323,7 @@ if (typeof(PhpDebugBar) == 'undefined') {
var nb = getObjectSize(this.debugbar.datasets) + 1; var nb = getObjectSize(this.debugbar.datasets) + 1;
if (typeof(data['__meta']) === 'undefined') { if (typeof(data['__meta']) === 'undefined') {
return "Request #" + nb + suffix; return "#" + nb + suffix;
} }
var filename = data['__meta']['uri'].substr(data['__meta']['uri'].lastIndexOf('/') + 1); var filename = data['__meta']['uri'].substr(data['__meta']['uri'].lastIndexOf('/') + 1);
@@ -740,6 +740,21 @@ if (typeof(PhpDebugBar) == 'undefined') {
return id; return id;
}, },
/**
* Loads a dataset using the open handler
*
* @param {String} id
*/
loadDataSet: function(id, suffix) {
if (!this.openHandler) {
throw new Error('loadDataSet() needs an open handler');
}
var self = this;
this.openHandler.load(id, function(data) {
self.addDataSet(data, id, suffix);
});
},
/** /**
* Returns the data from a dataset * Returns the data from a dataset
* *
@@ -830,15 +845,55 @@ if (typeof(PhpDebugBar) == 'undefined') {
* *
* @this {AjaxHandler} * @this {AjaxHandler}
* @param {XMLHttpRequest} xhr * @param {XMLHttpRequest} xhr
* @return {Bool}
*/ */
handle: function(xhr) { handle: function(xhr) {
if (!this.loadFromId(xhr)) {
return this.loadFromData(xhr);
}
return true;
},
/**
* Checks if the HEADER-id exists and loads the dataset using the open handler
*
* @param {XMLHttpRequest} xhr
* @return {Bool}
*/
loadFromId: function(xhr) {
var id = this.extractIdFromHeaders(xhr);
if (id && this.debugbar.openHandler) {
this.debugbar.loadDataSet(id, "(ajax)");
return true;
}
return false;
},
/**
* Extracts the id from the HEADER-id
*
* @param {XMLHttpRequest} xhr
* @return {String}
*/
extractIdFromHeaders: function(xhr) {
return xhr.getResponseHeader(this.headerName + '-id');
},
/**
* Checks if the HEADER exists and loads the dataset
*
* @param {XMLHttpRequest} xhr
* @return {Bool}
*/
loadFromData: function(xhr) {
var raw = this.extractDataFromHeaders(xhr); var raw = this.extractDataFromHeaders(xhr);
if (!raw) { if (!raw) {
return; return false;
} }
var data = this.parseHeaders(raw); var data = this.parseHeaders(raw);
this.debugbar.addDataSet(data.data, data.id, "(ajax)"); this.debugbar.addDataSet(data.data, data.id, "(ajax)");
return true;
}, },
/** /**

View File

@@ -46,9 +46,8 @@ class DebugBarTest extends DebugBarTestCase
} }
public function testStorage() public function testStorage()
{ {
$s = new MockStorage(); $this->debugbar->setStorage($s = new MockStorage());
$this->debugbar->setStorage($s);
$this->debugbar->addCollector(new MockCollector(array('foo'))); $this->debugbar->addCollector(new MockCollector(array('foo')));
$data = $this->debugbar->collect(); $data = $this->debugbar->collect();
$this->assertEquals($s->data[$this->debugbar->getCurrentRequestId()], $data); $this->assertEquals($s->data[$this->debugbar->getCurrentRequestId()], $data);
@@ -70,6 +69,17 @@ class DebugBarTest extends DebugBarTestCase
$this->assertArrayHasKey('phpdebugbar', $http->headers); $this->assertArrayHasKey('phpdebugbar', $http->headers);
} }
public function testSendDataInHeadersWithOpenHandler()
{
$http = $this->debugbar->getHttpDriver();
$this->debugbar->setStorage($s = new MockStorage());
$this->debugbar->addCollector($c = new MockCollector(array('foo')));
$this->debugbar->sendDataInHeaders(true);
$this->assertArrayHasKey('phpdebugbar-id', $http->headers);
$this->assertEquals($this->debugbar->getCurrentRequestId(), $http->headers['phpdebugbar-id']);
}
public function testStackedData() public function testStackedData()
{ {
$http = $this->debugbar->getHttpDriver(); $http = $this->debugbar->getHttpDriver();