mirror of
https://github.com/maximebf/php-debugbar.git
synced 2025-05-11 09:25:16 +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:
|
Integrations with other frameworks:
|
||||||
|
|
||||||
- [Laravel](https://github.com/barryvdh/laravel-debugbar) (project by barryvdh)
|
- [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)*
|
*(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
|
||||||
<?php
|
<?php
|
||||||
use DebugBar\StandardDebugBar;
|
use DebugBar\StandardDebugBar;
|
||||||
use DebugBar\JavascriptRenderer;
|
|
||||||
|
|
||||||
$debugbar = new StandardDebugBar();
|
$debugbar = new StandardDebugBar();
|
||||||
$debugbarRenderer = $debugbar->getJavascriptRenderer();
|
$debugbarRenderer = $debugbar->getJavascriptRenderer();
|
||||||
|
@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
include 'bootstrap.php';
|
include 'bootstrap.php';
|
||||||
$debugbar['messages']->addMessage('hello from ajax');
|
$debugbar['messages']->addMessage('hello from ajax');
|
||||||
|
$debugbar->sendDataInHeaders();
|
||||||
?>
|
?>
|
||||||
hello from AJAX
|
hello from AJAX
|
||||||
<?php
|
|
||||||
echo $debugbarRenderer->render(false);
|
|
||||||
?>
|
|
||||||
|
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",
|
"../README.md",
|
||||||
"data_collectors.md",
|
"data_collectors.md",
|
||||||
"rendering.md",
|
"rendering.md",
|
||||||
|
"ajax.md",
|
||||||
"base_collectors.md",
|
"base_collectors.md",
|
||||||
"bridge_collectors.md",
|
"bridge_collectors.md",
|
||||||
"storage.md",
|
"storage.md",
|
||||||
|
@ -202,6 +202,34 @@ class DebugBar implements ArrayAccess
|
|||||||
return $this->data;
|
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
|
* Returns a JavascriptRenderer for this instance
|
||||||
*
|
*
|
||||||
|
@ -49,6 +49,10 @@ class JavascriptRenderer
|
|||||||
|
|
||||||
protected $ignoredCollectors = array();
|
protected $ignoredCollectors = array();
|
||||||
|
|
||||||
|
protected $ajaxHandlerClass = 'PhpDebugBar.AjaxHandler';
|
||||||
|
|
||||||
|
protected $ajaxHandlerBindToJquery = true;
|
||||||
|
|
||||||
protected $openHandlerClass = 'PhpDebugBar.OpenHandler';
|
protected $openHandlerClass = 'PhpDebugBar.OpenHandler';
|
||||||
|
|
||||||
protected $openHandlerUrl;
|
protected $openHandlerUrl;
|
||||||
@ -354,6 +358,50 @@ class JavascriptRenderer
|
|||||||
), $type);
|
), $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
|
* Sets the class name of the js open handler
|
||||||
*
|
*
|
||||||
@ -598,6 +646,13 @@ class JavascriptRenderer
|
|||||||
$js .= $this->getJsControlsDefinitionCode($this->variableName);
|
$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) {
|
if ($this->openHandlerUrl !== null) {
|
||||||
$js .= sprintf("%s.setOpenHandler(new %s(%s));\n", $this->variableName,
|
$js .= sprintf("%s.setOpenHandler(new %s(%s));\n", $this->variableName,
|
||||||
$this->openHandlerClass,
|
$this->openHandlerClass,
|
||||||
|
@ -741,4 +741,79 @@ if (typeof(localStorage) == 'undefined') {
|
|||||||
DebugBar.Tab = Tab;
|
DebugBar.Tab = Tab;
|
||||||
DebugBar.Indicator = Indicator;
|
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);
|
})(jQuery);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user