Created
July 17, 2013 09:44
-
-
Save basz/6019200 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;$(document).ready(function(e) { | |
$(document).ajaxSuccess(function(event, xhr, settings) { | |
if (xhr.getResponseHeader("X-ZDT-Payload")) { | |
$('#zend-developer-toolbar').replaceWith($.parseJSON(xhr.getResponseHeader("X-ZDT-Payload"))); | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Tries to injects the toolbar into the view. The toolbar is only injected in well | |
* formed HTML by replacing the closing body tag, leaving ESI untouched. | |
* | |
* @param ProfilerEvent $event | |
*/ | |
protected function injectToolbar(ProfilerEvent $event) | |
{ | |
$entries = $this->renderEntries($event); | |
$request = $event->getApplication()->getRequest(); | |
$response = $event->getApplication()->getResponse(); | |
$toolbarView = new ViewModel(array('entries' => $entries)); | |
$toolbarView->setTemplate('zend-developer-tools/toolbar/toolbar'); | |
$toolbar = $this->renderer->render($toolbarView); | |
$toolbarCss = new ViewModel(array( | |
'position' => $this->options->getToolbarPosition(), | |
)); | |
$toolbarCss->setTemplate('zend-developer-tools/toolbar/style'); | |
if ($request->isXmlHttpRequest()) { | |
$header = new XZDTPayload(); | |
$header->setPayload($toolbar); | |
$response->getHeaders()->addHeader($header); | |
} else { | |
$injected = preg_replace('/<\/body>/i', $toolbar . "\n</body>", $response->getBody(), 1); | |
$style = $this->renderer->render($toolbarCss); | |
$injected = preg_replace('/<\/head>/i', $style . "\n</head>", $injected, 1); | |
$response->setContent($injected); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ZendDeveloperTools\Http\Header; | |
use Zend\Http\Header\HeaderInterface; | |
/** | |
* Provides an X-ZDT-Payload HTTP Header | |
* | |
* @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.14 | |
*/ | |
class XZDTPayload implements HeaderInterface | |
{ | |
/** | |
* json encoded payload | |
* | |
* @var string | |
*/ | |
protected $payload; | |
public static function fromString($headerLine) | |
{ | |
$header = new static(); | |
list($name, $value) = explode(': ', $headerLine, 2); | |
// check to ensure proper header type for this factory | |
if (strtolower($name) !== strtolower($header->getFieldName())) { | |
throw new \InvalidArgumentException('Invalid header line for %s string: "%s"', $header->getFieldName(), $name); | |
} | |
$header->setPayload(json_decode($value)); | |
return $header; | |
} | |
/** | |
* Return header name | |
* | |
* @return string | |
*/ | |
public function getFieldName() | |
{ | |
return 'X-ZDT-Payload'; | |
} | |
public function getFieldValue() { | |
return $this->payload; | |
} | |
/** | |
* @param String $message | |
*/ | |
public function setPayload($payload) | |
{ | |
$this->payload = json_encode($payload); | |
} | |
/** | |
* @return String | |
*/ | |
public function getPayload() | |
{ | |
return json_decode($this->payload); | |
} | |
/** | |
* Output header line | |
* | |
* @return string | |
*/ | |
public function toString() | |
{ | |
return sprintf("%s: %s", $this->getFieldName(), $this->getFieldValue()); | |
} | |
/** | |
* Allow casting to string | |
* | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return $this->toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much!