diff --git a/CHANGELOG.md b/CHANGELOG.md index 12c3b3a..fa47857 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,16 @@ # 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 stacked data diff --git a/demo/ajax.php b/demo/ajax.php index 10548bd..1250e4c 100644 --- a/demo/ajax.php +++ b/demo/ajax.php @@ -2,6 +2,6 @@ include 'bootstrap.php'; $debugbar['messages']->addMessage('hello from ajax'); -$debugbar->sendDataInHeaders(); +$debugbar->sendDataInHeaders(true); ?> hello from AJAX \ No newline at end of file diff --git a/demo/bootstrap.php b/demo/bootstrap.php index 59bb30d..090a78b 100644 --- a/demo/bootstrap.php +++ b/demo/bootstrap.php @@ -15,8 +15,9 @@ $debugbarRenderer = $debugbar->getJavascriptRenderer() // // create a writable profiles folder in the demo directory to uncomment the following lines // -//$debugbar->setStorage(new DebugBar\Storage\FileStorage(__DIR__ . '/profiles')); -//$debugbarRenderer->setOpenHandlerUrl('open.php'); +// $debugbar->setStorage(new DebugBar\Storage\FileStorage(__DIR__ . '/profiles')); +// $debugbar->setStorage(new DebugBar\Storage\RedisStorage(new Predis\Client())); +// $debugbarRenderer->setOpenHandlerUrl('open.php'); function render_demo_page(Closure $callback = null) { diff --git a/docs/ajax_and_stack.md b/docs/ajax_and_stack.md index a1cac4a..134c0c1 100644 --- a/docs/ajax_and_stack.md +++ b/docs/ajax_and_stack.md @@ -25,6 +25,24 @@ of `AjaxHandler` is stored in the `ajaxHandler` property of the `DebugBar` objec 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 Some times you need to collect data about a request but the page won't actually diff --git a/src/DebugBar/DebugBar.php b/src/DebugBar/DebugBar.php index bdd886f..e80d34a 100644 --- a/src/DebugBar/DebugBar.php +++ b/src/DebugBar/DebugBar.php @@ -28,6 +28,8 @@ use DebugBar\Storage\StorageInterface; */ class DebugBar implements ArrayAccess { + public static $useOpenHandlerWhenSendingDataHeaders = false; + protected $collectors = array(); protected $data; @@ -266,13 +268,23 @@ class DebugBar implements ArrayAccess /** * Sends the data through the HTTP headers - * + * + * @param bool $useOpenHandler * @param string $headerName * @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); return $this; } diff --git a/src/DebugBar/Resources/debugbar.js b/src/DebugBar/Resources/debugbar.js index edfca9d..c9a5a52 100644 --- a/src/DebugBar/Resources/debugbar.js +++ b/src/DebugBar/Resources/debugbar.js @@ -323,7 +323,7 @@ if (typeof(PhpDebugBar) == 'undefined') { var nb = getObjectSize(this.debugbar.datasets) + 1; if (typeof(data['__meta']) === 'undefined') { - return "Request #" + nb + suffix; + return "#" + nb + suffix; } var filename = data['__meta']['uri'].substr(data['__meta']['uri'].lastIndexOf('/') + 1); @@ -740,6 +740,21 @@ if (typeof(PhpDebugBar) == 'undefined') { 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 * @@ -830,15 +845,55 @@ if (typeof(PhpDebugBar) == 'undefined') { * * @this {AjaxHandler} * @param {XMLHttpRequest} xhr + * @return {Bool} */ 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); if (!raw) { - return; + return false; } var data = this.parseHeaders(raw); this.debugbar.addDataSet(data.data, data.id, "(ajax)"); + return true; }, /** diff --git a/tests/DebugBar/Tests/DebugBarTest.php b/tests/DebugBar/Tests/DebugBarTest.php index f01808e..7d22f11 100644 --- a/tests/DebugBar/Tests/DebugBarTest.php +++ b/tests/DebugBar/Tests/DebugBarTest.php @@ -46,9 +46,8 @@ class DebugBarTest extends DebugBarTestCase } public function testStorage() - { - $s = new MockStorage(); - $this->debugbar->setStorage($s); + { + $this->debugbar->setStorage($s = new MockStorage()); $this->debugbar->addCollector(new MockCollector(array('foo'))); $data = $this->debugbar->collect(); $this->assertEquals($s->data[$this->debugbar->getCurrentRequestId()], $data); @@ -70,6 +69,17 @@ class DebugBarTest extends DebugBarTestCase $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() { $http = $this->debugbar->getHttpDriver();