Skip to content

Commit

Permalink
Debugger Bar: added AJAX support (PROOF OF CONCEPT)
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 30, 2013
1 parent 7f98d09 commit 1ba4499
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 51 deletions.
40 changes: 39 additions & 1 deletion Nette/Diagnostics/Bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,35 @@ class Bar extends Nette\Object
/** @var array */
private $panels = array();

private $handle;



public function init()
{
if (isset($_GET['_nette_debug']) && preg_match('#^\w+$#', $_GET['_nette_debug'])) {
$file = $this->getFileName($_GET['_nette_debug']);
header('Content-Type: text/javascript');
$html = @file_get_contents($file);
if ($html) {
echo 'Nette.Debug.newContent(' . json_encode(Nette\Utils\Strings::fixEncoding($html)) . ');';
unlink($file);
}
exit;
}
$id = uniqid();
$this->handle = fopen($this->getFileName($id), 'w');
flock($this->handle, LOCK_EX);
setCookie('ajax', $id, 0, '/');

This comment has been minimized.

Copy link
@Majkl578

Majkl578 May 1, 2013

Contributor

This cookie name is too generic. Also the path is wrong, imagine multiple instances (e.g. localhost/*).

This comment has been minimized.

Copy link
@dg

dg Aug 3, 2013

Author Member

This is just proof of concept.

}



public function getFileName($id)
{
return Debugger::$logDirectory . "/data.$id.dat";

This comment has been minimized.

Copy link
@Majkl578

Majkl578 May 1, 2013

Contributor

Would not work when logDirectory is not set. (Actually, it will try to create the file in filesystem root.)

}



/**
Expand Down Expand Up @@ -65,6 +94,9 @@ public function getPanel($id)
*/
public function render()
{
if (!$this->handle) {
return;
}
$obLevel = ob_get_level();
$panels = array();
foreach ($this->panels as $id => $panel) {
Expand Down Expand Up @@ -105,8 +137,14 @@ public function render()
}
}
$session = NULL;
ob_start();
require __DIR__ . '/templates/bar.content.phtml';
fwrite($this->handle, ob_get_clean());
fclose($this->handle);

require __DIR__ . '/templates/bar.phtml';
if (preg_match('#^Content-Type: text/html#im', implode("\n", headers_list())) && !isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
require __DIR__ . '/templates/bar.phtml';
}
}

}
6 changes: 5 additions & 1 deletion Nette/Diagnostics/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ public static function enable($mode = NULL, $logDirectory = NULL, $email = NULL)
set_error_handler(array(__CLASS__, '_errorHandler'));
self::$enabled = TRUE;
}

if (!self::$productionMode) {
self::$bar->init();
}
}


Expand Down Expand Up @@ -385,7 +389,7 @@ public static function _shutdownHandler()
}

// debug bar (require HTML & development mode)
if (!connection_aborted() && self::$bar && !self::$productionMode && self::isHtmlMode()) {
if (!connection_aborted() && self::$bar && !self::$productionMode) {
self::$bar->render();
}
}
Expand Down
40 changes: 40 additions & 0 deletions Nette/Diagnostics/templates/bar.content.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* Debug Bar screen template.
*
* It uses Silk Icons created by Mark James and released under Creative Commons Attribution 2.5 License.
*
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*
* @param array $panels
* @return void
*/

namespace Nette\Diagnostics;

use Nette;

foreach ($panels as $id => $panel): if (!$panel['panel']) continue; ?>
<div class="nette-panel" id="nette-debug-panel-<?php echo $panel['id'] ?>">
<?php echo $panel['panel'] ?>
<div class="nette-icons">
<a href="#" title="open in window">&curren;</a>
<a href="#" rel="close" title="close window">&times;</a>
</div>
</div>
<?php endforeach ?>

<div id="nette-debug-bar">
<ul>
<li id="nette-debug-logo" title="PHP <?php echo htmlSpecialChars(PHP_VERSION . " |\n"
. (isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] . " |\n" : '')
. (class_exists('Nette\Framework') ? 'Nette Framework ' . Nette\Framework::VERSION . ' (' . substr(Nette\Framework::REVISION, 8) . ')' : '')) ?>">&nbsp;<span>Nette Framework</span></li>
<?php foreach ($panels as $panel): if (!$panel['tab']) continue; ?>
<?php if (!empty($panel['previous'])) echo '</ul><ul class="nette-previous">'; ?>
<li><?php if ($panel['panel']): ?><a href="#" rel="<?php echo $panel['id'] ?>"><?php echo trim($panel['tab']) ?></a><?php else: echo '<span>', trim($panel['tab']), '</span>'; endif ?></li>
<?php endforeach ?>
<li><a href="#" rel="close" title="close debug bar">&times;</a></li>
</ul>
</div>
51 changes: 47 additions & 4 deletions Nette/Diagnostics/templates/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,11 @@

Debug.init = function() {
Debug.initResize();
(new Bar).init();
$('.nette-panel').each(function() {
Debug.getPanel(this.id).init();
});
var place = document.body.appendChild(document.createElement('div'));
place.id = 'nette-debug';

Debug.tryLoad();
setInterval(Debug.tryLoad, 40);
};

Debug.getPanel = function(id) {
Expand All @@ -284,4 +285,46 @@
});
};

Debug.loading = false;

Debug.tryLoad = function() {
var m;
if (!Debug.loading && (m = document.cookie.match(/ajax=(\w+)/))) {
Debug.loading = true;
document.cookie = 'ajax=; path=/';
Debug.load('?_nette_debug=' + m[1], function() {
Debug.loading = false;
});
}
};

Debug.newContent = function(data) {
var place = document.getElementById('nette-debug');
place.innerHTML = data;
for (var i = 0, scripts = place.getElementsByTagName('script'); i < scripts.length; i++) eval(scripts[i].innerHTML);
(new Bar).init();
$('.nette-panel').each(function() {
Debug.getPanel(this.id).init();
});
place.style.display = 'block';
};

Debug.load = function(url, callback) {
// reuse <script> by ID?
var head = document.getElementsByTagName('head')[0] || document.documentElement,
script = document.createElement('script'),
done = false;

script.src = url;
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete')) {
done = true;
callback && callback.apply();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
};

})();
49 changes: 4 additions & 45 deletions Nette/Diagnostics/templates/bar.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use Nette;

<!-- Nette Debug Bar -->

<?php ob_start() ?>
&nbsp;

<style id="nette-debug-style" class="nette-debug">
<?php require __DIR__ . '/bar.css' ?>
Expand All @@ -41,50 +39,11 @@ use Nette;
<script id="nette-debug-script">
/* <![CDATA[ */
<?php require __DIR__ . '/netteQ.js' ?>
<?php require __DIR__ . '/bar.js' ?>
<?php require __DIR__ . '/dumper.js' ?>
/* ]]> */
</script>


<?php foreach ($panels as $id => $panel): if (!$panel['panel']) continue; ?>
<div class="nette-panel" id="nette-debug-panel-<?php echo $panel['id'] ?>">
<?php echo $panel['panel'] ?>
<div class="nette-icons">
<a href="#" title="open in window">&curren;</a>
<a href="#" rel="close" title="close window">&times;</a>
</div>
</div>
<?php endforeach ?>

<div id="nette-debug-bar">
<ul>
<li id="nette-debug-logo" title="PHP <?php echo htmlSpecialChars(PHP_VERSION . " |\n"
. (isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] . " |\n" : '')
. (class_exists('Nette\Framework') ? 'Nette Framework ' . Nette\Framework::VERSION . ' (' . substr(Nette\Framework::REVISION, 8) . ')' : '')) ?>">&nbsp;<span>Nette Framework</span></li>
<?php foreach ($panels as $panel): if (!$panel['tab']) continue; ?>
<?php if (!empty($panel['previous'])) echo '</ul><ul class="nette-previous">'; ?>
<li><?php if ($panel['panel']): ?><a href="#" rel="<?php echo $panel['id'] ?>"><?php echo trim($panel['tab']) ?></a><?php else: echo '<span>', trim($panel['tab']), '</span>'; endif ?></li>
<?php endforeach ?>
<li><a href="#" rel="close" title="close debug bar">&times;</a></li>
</ul>
</div>
<?php $output = ob_get_clean(); ?>

<?php require __DIR__ . '/bar.js' ?>

<script>
(function(onloadOrig) {
window.onload = function() {
if (typeof onloadOrig === 'function') onloadOrig();
var debug = document.body.appendChild(document.createElement('div'));
debug.id = 'nette-debug';
debug.innerHTML = <?php echo json_encode(Nette\Utils\Strings::fixEncoding($output)) ?>;
for (var i = 0, scripts = debug.getElementsByTagName('script'); i < scripts.length; i++) eval(scripts[i].innerHTML);
Nette.Dumper.init();
Nette.Debug.init();
debug.style.display = 'block';
};
})(window.onload);
Nette.Dumper.init();
Nette.Debug.init();
/* ]]> */
</script>

<!-- /Nette Debug Bar -->

3 comments on commit 1ba4499

@Majkl578
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's a status of this? Will it make it to 2.1?

@fprochazka
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dg are there any problems in this we can help with? Is there any chance this will make it's way to master?

@enumag
Copy link
Contributor

@enumag enumag commented on 1ba4499 Dec 1, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd also like to see this in master asap.

Please sign in to comment.