Skip to content

Instantly share code, notes, and snippets.

@LeonardoCA
Created July 13, 2012 21:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeonardoCA/3107582 to your computer and use it in GitHub Desktop.
Save LeonardoCA/3107582 to your computer and use it in GitHub Desktop.
AjaxPanelsCompilerExtension
<?php
/**
* This file is part of the AjaxPanels Nette Addon
*
* Copyright (c) 2012 Leonard Odlozilik
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/
namespace Lxo\Ajax;
use Nette;
/**
* AjaxPanels - refreshes content of any debug panel
*
* @author Vojtěch Dobeš
* @author Leonard Odlozilik
*/
class AjaxPanels extends Nette\Object
{
public function __construct()
{
}
/**
* Adds barDump for Ajax Requests
*
* @param \Nette\Application\Application $sender
* @param \Nette\Application\IResponse $response
* @return void
*/
public function barDump(Nette\Application\Application $sender, Nette\Application\IResponse $response)
{
$presenter = $sender->getPresenter();
if ($presenter->isAjax() && Nette\Diagnostics\Debugger::isEnabled() && Nette\Diagnostics\Debugger::$bar) {
$panels = Nette\Reflection\ClassType::from(Nette\Diagnostics\Debugger::$bar)
->getProperty('panels');
$panels->setAccessible(TRUE);
$panels = $panels->getValue(Nette\Diagnostics\Debugger::$bar);
$presenter->payload->netteDumps = $panels['Nette\Diagnostics\DefaultBarPanel-4']->data;
//Nette\Diagnostics\Debugger::barDump($panels);
if (array_key_exists('DibiNettePanel', $panels)) {
$presenter->payload->dibiPanel = $panels['DibiNettePanel']->getPanel();
}
}
}
/**
* @param \Nette\Application\Application $sender
* @return void
*/
/*
public function registerLogFileComponent(Nette\Application\Application $sender)
{
$presenter = $sender->getPresenter();
//$presenter->addComponent(new Lxo\Ajax\ComponentExceptionLogFile);
}
*/
/*
public function createComponentExceptionLogFile() {
return new Lxo\Ajax\ComponentExceptionLogFile;
}
*/
/*
public static function handleGetExceptionLogFile() {
if (Nette\Diagnostics\Debugger::isEnabled() && Nette\Diagnostics\Debugger::$bar) {
echo file_exists($filename = $this->getParam('file')) ? file_get_contents($filename) : '"'.$this->getParam('file').'" not found.';
$this->terminate();
}
}
*/
public static function sendExceptionLogFile(Nette\Application\Application $sender, \Exception $e) {
if (Nette\Diagnostics\Debugger::isEnabled() && Nette\Diagnostics\Debugger::$bar && ($presenter = $sender->getPresenter()) && (preg_match('/exceptionLogFile/', $e->getMessage()))) {
echo file_exists($filename = $presenter->getParam('file')) ? file_get_contents($filename) : '"'.$presenter->getParam('file').'" not found.';
Nette\Diagnostics\Debugger::$bar = FALSE;
die();
}
}
}
<?php
/**
* This file is part of AjaxPanels Extension for Nette
*
* @author LeonardoCA
*
*/
namespace Lxo\Ajax;
use Nette,
Nette\DI\ContainerBuilder,
Nette\Utils\Validators;
class AjaxPanelsCompilerExtension extends Nette\Config\CompilerExtension
{
public function loadConfiguration() {
$builder = $this->getContainerBuilder();
if ($builder->hasDefinition('application')) {
$ajaxPanels = $builder->addDefinition($this->prefix('ajaxpanels'))
->setClass('LXO\Ajax\AjaxPanels');
$builder->getDefinition('application')
->addSetup('$service->onResponse[] = ?;', array(array($ajaxPanels, 'barDump')))
// nasty workaround because I was not able to addComponent to presenter using compiler extension
// file is sent while Presenter can not create component 'exceptionLogFile'
->addSetup('$service->onError[] = ?;', array(array($ajaxPanels, 'sendExceptionLogFile')));
}
}
}
<?php
/**
* This file is part of the AjaxPanels Nette Addon
*
* Copyright (c) 2012 Leonard Odlozilik
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*/
namespace Lxo\Ajax;
use Nette;
/**
* Component sending log file to browser
*
* @author Leonard Odlozilik
*/
class ComponentExceptionLogFile extends Nette\Application\UI\PresenterComponent
{
public function handleGetExceptionLogFile() {
if (Nette\Diagnostics\Debugger::isEnabled() && Nette\Diagnostics\Debugger::$bar) {
Nette\Diagnostics\Debugger::$bar = FALSE;
$presenter = $this->getPresenter(TRUE);
echo file_exists($filename = $presenter->getParam('file')) ? file_get_contents($filename) : '"'.$this->getParam('file').'" not found.';
$presenter->terminate();
}
}
}
(function($, undefined) {
/**
* Shows barDumps from Ajax request
*/
$.nette.ext('diagnostics.dumps', {
success: function (payload) {
if (!payload.netteDumps || payload.netteDumps.length == 0) return;
var $panel = this.getPanel(), $helper = $('<div>');
$(this.getTitle(++this.counter)).appendTo($panel);
$.each(payload.netteDumps, function (i, item) {
if (item.title) $('<h2>', {
text: item.title
}).appendTo($helper);
var $table = $('<table>').appendTo($helper), i = 0;
$.each(item.dump, function (key, dump) {
var $caption = $('<th>')
$table.append($('<tr>', {
'class': i++ % 2 ? 'nette-alt' : '',
html: '<th>' + key + '</th><td>' + dump + '</td>'
}));
});
});
$helper.children().appendTo($panel);
this.notify();
}
}, {
counter: 0,
getTitle: function (count) {
return '<h2>Ajax request #' + count + '</h2>';
},
getTab: function () {
if (!this.tab) this.tab = $('[rel=Nette-Diagnostics-DefaultBarPanel-4]');
return this.tab;
},
getPanel: function () {
if (!this.panel) this.panel = $('#nette-debug-panel-Nette-Diagnostics-DefaultBarPanel-4 .nette-DumpPanel');
return this.panel;
},
notify: function () {
if (this.notified) return;
$('<strong>', {
text: ' [ajax!]',
css: { fontWeight: 'bold' } }
).appendTo(this.getTab());
this.notified = true;
}
});
})(jQuery);
(function($, undefined) {
/**
* Shows db queries for Ajax request in dibi panel
*/
$.nette.ext('diagnostics.dibipanel', {
success: function (payload) {
if (!payload.dibiPanel || payload.dibiPanel.length == 0) return;
var $panel = this.getPanel(), $helper = $('<div>');
$(this.getTitle(++this.counter)).appendTo($panel);
$('<div>', {
html: payload.dibiPanel
}).appendTo($helper);
console.log(payload.dibiPanel);
$helper.children().appendTo($panel);
this.notify();
}
}, {
counter: 0,
getTitle: function (count) {
return '<h2>Ajax request #' + count + '</h2>';
},
getTab: function () {
if (!this.tab) this.tab = $('[rel=DibiNettePanel]');
return this.tab;
},
getPanel: function () {
if (!this.panel) this.panel = $('#nette-debug-panel-DibiNettePanel .nette-DibiProfiler');
return this.panel;
},
notify: function () {
if (this.notified) return;
$('<strong>', {
text: ' [ajax!]',
css: { fontWeight: 'bold' } }
).appendTo(this.getTab());
this.notified = true;
}
});
})(jQuery);
/*
(function($, undefined) {
/**
* Shows HTTP Error Statuses from Ajax request
*//*
$.nette.ext('error', {
error: function (payload) {
if (payload.status != 200) {
$('#snippet--flashMessages').html('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a>'+payload.status+' '+payload.statusText+'</div>');
}
}
});
})(jQuery);
*/
(function($, undefined) {
/**
* Shows HTTP Error Statuses and some FireLogger info from Ajax request
*/
$.nette.ext('httpStatusToFlash', {
start: function (xhr) {
//xhr.setRequestHeader("X-FireLogger", '1.2');
return xhr;
},
error: function (payload) {
var flashMessages = $('#snippet--flashMessages');
var html = "";
if (payload.status != 200) {
html += '<h4>'+payload.status+' '+payload.statusText+'</h4>';
}
var headers = payload.getAllResponseHeaders();
var matches = headers.match(/FireLogger[-a-z0-9]+/);
if (matches != null) {
for (var i=0;i<matches.length;i++)
{
var firelog = jQuery.parseJSON(this.base64decode(payload.getResponseHeader(matches[i])));
//console.log(firelog);
firelog = firelog.logs[0];
html += '<p>'+firelog.template+'</p><p>'+this.editorLink(firelog.pathname,firelog.lineno)+'</p>';
}
}
var fileLog = payload.getResponseHeader('X-Nette-Error-Log');
if (fileLog) {
matches = window.location.href.match(/^[^\?#]*/g);
var getLogUrl = matches[0] + '?do=exceptionLogFile-GetExceptionLogFile&file='+fileLog;
// displays debugging info in iframe
html += '<iframe src="'+getLogUrl+'" width="' + (flashMessages.width() - 30) + '" height="500"></iframe>';
// opens new window with debugging info
//window.open(getLogUrl);
}
flashMessages.html('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a>'+html+'</div>');
}
}, {
editorLink: function (link,lineno) {
return '<b>File:</b> <a title="'+link+':'+lineno+'" href="editor://open/?file='+encodeURIComponent(link)+'&line='+lineno+'">'+link+'</a> <b>Line:</b> '+lineno;
},
/* http://www.webtoolkit.info/javascript-base64.html */
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// base64 decode
base64decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = this._utf8_decode(output);
return output;
},
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment