mirror of
https://github.com/maximebf/php-debugbar.git
synced 2025-05-04 13:57:55 +02:00
added support for sending data through HTTP headers (fixed #18)
This commit is contained in:
parent
c54fb868b6
commit
7fa6b33cc5
@ -36,6 +36,7 @@ examples and [phpdebugbar.com](http://phpdebugbar.com) for a live example.
|
||||
Integrations with other frameworks:
|
||||
|
||||
- [Laravel](https://github.com/barryvdh/laravel-debugbar) (project by barryvdh)
|
||||
- [Atomik](http://atomikframework.com/docs/error-log-debug.html#debug-bar)
|
||||
|
||||
*(drop me a message or submit a PR to add your DebugBar related project here)*
|
||||
|
||||
@ -63,7 +64,6 @@ The easiest way is using the `render()` functions
|
||||
```PHP
|
||||
<?php
|
||||
use DebugBar\StandardDebugBar;
|
||||
use DebugBar\JavascriptRenderer;
|
||||
|
||||
$debugbar = new StandardDebugBar();
|
||||
$debugbarRenderer = $debugbar->getJavascriptRenderer();
|
||||
|
@ -2,9 +2,6 @@
|
||||
|
||||
include 'bootstrap.php';
|
||||
$debugbar['messages']->addMessage('hello from ajax');
|
||||
|
||||
?>
|
||||
hello from AJAX
|
||||
<?php
|
||||
echo $debugbarRenderer->render(false);
|
||||
$debugbar->sendDataInHeaders();
|
||||
?>
|
||||
hello from AJAX
|
24
docs/ajax.md
Normal file
24
docs/ajax.md
Normal file
@ -0,0 +1,24 @@
|
||||
# AJAX
|
||||
|
||||
As mentioned in the previous chapter, if you are performing AJAX requests
|
||||
which return HTML content, you can use `JavascriptRenderer::render(false)`.
|
||||
|
||||
In the case you are sending back non-HTML data (eg: JSON), the DebugBar can
|
||||
send data to the client using HTTP headers using the `sendDataInHeaders()` method
|
||||
(no need to use the `JavascriptRenderer`):
|
||||
|
||||
$debugbar = new DebugBar();
|
||||
// ...
|
||||
$debugbar->sendDataInHeaders();
|
||||
|
||||
On the client side, an instance of `PhpDebugBar.AjaxHandler` will
|
||||
parse the headers and add the dataset to the debugbar.
|
||||
|
||||
The AjaxHandler automatically binds to jQuery's *ajaxComplete* event
|
||||
so if you are using jQuery, you have nothing to configure.
|
||||
|
||||
If you're not using jQuery, you can call `AjaxHandler.handle(xhr)`.
|
||||
If you are using the `JavascriptRenderer` initialization, the instance
|
||||
of `AjaxHandler` is stored in the `ajaxHandler` property of the `DebugBar` object.
|
||||
|
||||
debugbar.ajaxHandler.handle(xhr);
|
@ -6,6 +6,7 @@
|
||||
"../README.md",
|
||||
"data_collectors.md",
|
||||
"rendering.md",
|
||||
"ajax.md",
|
||||
"base_collectors.md",
|
||||
"bridge_collectors.md",
|
||||
"storage.md",
|
||||
|
@ -202,6 +202,34 @@ class DebugBar implements ArrayAccess
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the data through the HTTP headers
|
||||
*
|
||||
* @param string $headerName
|
||||
* @param integer $maxHeaderLength
|
||||
*/
|
||||
public function sendDataInHeaders($headerName = 'phpdebugbar', $maxHeaderLength = 4096)
|
||||
{
|
||||
$data = rawurlencode(json_encode(array(
|
||||
'id' => $this->getCurrentRequestId(),
|
||||
'data' => $this->getData()
|
||||
)));
|
||||
$chunks = array();
|
||||
|
||||
while (strlen($data) > $maxHeaderLength) {
|
||||
$chunks[] = substr($data, 0, $maxHeaderLength);
|
||||
$data = substr($data, $maxHeaderLength);
|
||||
}
|
||||
$chunks[] = $data;
|
||||
|
||||
for ($i = 0, $c = count($chunks); $i < $c; $i++) {
|
||||
$name = $headerName . ($i > 0 ? "-$i" : '');
|
||||
header("$name: {$chunks[$i]}");
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a JavascriptRenderer for this instance
|
||||
*
|
||||
|
@ -49,6 +49,10 @@ class JavascriptRenderer
|
||||
|
||||
protected $ignoredCollectors = array();
|
||||
|
||||
protected $ajaxHandlerClass = 'PhpDebugBar.AjaxHandler';
|
||||
|
||||
protected $ajaxHandlerBindToJquery = true;
|
||||
|
||||
protected $openHandlerClass = 'PhpDebugBar.OpenHandler';
|
||||
|
||||
protected $openHandlerUrl;
|
||||
@ -354,6 +358,50 @@ class JavascriptRenderer
|
||||
), $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the class name of the ajax handler
|
||||
*
|
||||
* Set to false to disable
|
||||
*
|
||||
* @param string $className
|
||||
*/
|
||||
public function setAjaxHandlerClass($className)
|
||||
{
|
||||
$this->ajaxHandlerClass = $className;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the class name of the ajax handler
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getAjaxHandlerClass()
|
||||
{
|
||||
return $this->ajaxHandlerClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether to call bindToJquery() on the ajax handler
|
||||
*
|
||||
* @param boolean $bind
|
||||
*/
|
||||
public function setBindAjaxHandlerToJquery($bind = true)
|
||||
{
|
||||
$this->ajaxHandlerBindToJquery = $bind;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether bindToJquery() will be called on the ajax handler
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isAjaxHandlerBoundToJquery()
|
||||
{
|
||||
return $this->ajaxHandlerBindToJquery;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the class name of the js open handler
|
||||
*
|
||||
@ -598,6 +646,13 @@ class JavascriptRenderer
|
||||
$js .= $this->getJsControlsDefinitionCode($this->variableName);
|
||||
}
|
||||
|
||||
if ($this->ajaxHandlerClass) {
|
||||
$js .= sprintf("%s.ajaxHandler = new %s(%s);\n", $this->variableName, $this->ajaxHandlerClass, $this->variableName);
|
||||
if ($this->ajaxHandlerBindToJquery) {
|
||||
$js .= sprintf("%s.ajaxHandler.bindToJquery();\n", $this->variableName);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->openHandlerUrl !== null) {
|
||||
$js .= sprintf("%s.setOpenHandler(new %s(%s));\n", $this->variableName,
|
||||
$this->openHandlerClass,
|
||||
|
@ -741,4 +741,79 @@ if (typeof(localStorage) == 'undefined') {
|
||||
DebugBar.Tab = Tab;
|
||||
DebugBar.Indicator = Indicator;
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* AjaxHandler
|
||||
*
|
||||
* Extract data from headers of an XMLHttpRequest and adds a new dataset
|
||||
*/
|
||||
var AjaxHandler = PhpDebugBar.AjaxHandler = function(debugbar, headerName) {
|
||||
this.debugbar = debugbar;
|
||||
this.headerName = headerName || 'phpdebugbar';
|
||||
};
|
||||
|
||||
$.extend(AjaxHandler.prototype, {
|
||||
|
||||
/**
|
||||
* Handles an XMLHttpRequest
|
||||
*
|
||||
* @this {AjaxHandler}
|
||||
* @param {XMLHttpRequest} xhr
|
||||
*/
|
||||
handle: function(xhr) {
|
||||
var raw = this.extractDataFromHeaders(xhr);
|
||||
if (!raw) {
|
||||
return;
|
||||
}
|
||||
|
||||
var data = this.parseHeaders(raw);
|
||||
this.debugbar.addDataSet(data.data, data.id);
|
||||
},
|
||||
|
||||
/**
|
||||
* Extract the data as a string from headers of an XMLHttpRequest
|
||||
*
|
||||
* @this {AjaxHandler}
|
||||
* @param {XMLHttpRequest} xhr
|
||||
* @return {string}
|
||||
*/
|
||||
extractDataFromHeaders: function(xhr) {
|
||||
var data = xhr.getResponseHeader(this.headerName);
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
for (var i = 1;; i++) {
|
||||
var header = xhr.getResponseHeader(this.headerName + '-' + i);
|
||||
if (!header) {
|
||||
break;
|
||||
}
|
||||
data += header;
|
||||
}
|
||||
return decodeURIComponent(data);
|
||||
},
|
||||
|
||||
/**
|
||||
* Parses the string data into an object
|
||||
*
|
||||
* @this {AjaxHandler}
|
||||
* @param {string} data
|
||||
* @return {string}
|
||||
*/
|
||||
parseHeaders: function(data) {
|
||||
return JSON.parse(data);
|
||||
},
|
||||
|
||||
/**
|
||||
* Attaches an event listener to jQuery.ajaxComplete()
|
||||
*/
|
||||
bindToJquery: function() {
|
||||
var self = this;
|
||||
$(document).ajaxComplete(function(e, xhr, settings) {
|
||||
self.handle(xhr);
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
})(jQuery);
|
||||
|
Loading…
x
Reference in New Issue
Block a user