Created
May 29, 2013 18:33
-
-
Save carnage/5672599 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* ProfilerEvent::EVENT_COLLECTED event callback. | |
* | |
* @param ProfilerEvent $event | |
*/ | |
public function onCollected(ProfilerEvent $event) | |
{ | |
$application = $event->getApplication(); | |
$request = $application->getRequest(); | |
if ($request->isXmlHttpRequest()) { | |
return; | |
} | |
$response = $application->getResponse(); | |
$headers = $response->getHeaders(); | |
if ($headers->has('Content-Type')) { | |
if (true === strpos($headers->get('Content-Type')->getFieldValue(), 'html')) { | |
$this->injectToolbar($event); | |
} elseif (true === strpos($headers->get('Content-Type')->getFieldValue(), 'json')) { | |
$this->injectJsonToolbar($event); | |
} | |
} | |
// todo: X-Debug-Token logic? | |
// todo: redirect logic | |
} | |
/** | |
* Tries to injects the toolbar into the view. The toolbar is only injected in well | |
* formed Json by replacing the closing }, leaving ESI untouched. | |
* | |
* @param ProfilerEvent $event | |
*/ | |
protected function injectJsonToolbar(ProfilerEvent $event) | |
{ | |
$entries = $this->renderEntries($event); | |
$response = $event->getApplication()->getResponse();; | |
$toolbarView = new ViewModel(array('entries' => $entries)); | |
$toolbarView->setTemplate('zend-developer-tools/toolbar/toolbar'); | |
$toolbar = $this->renderer->render($toolbarView); | |
$toolbar = str_replace("\n", '', $toolbar); | |
$injected = preg_replace('/}$/im', ', toolbar:"' . $toolbar . '"}', $response->getBody(), 1); | |
$response->setContent($injected); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment