Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions dev/ajax.bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

require __DIR__ . '/../src/tracy.php';


class Panel implements Tracy\IBarPanel, Tracy\IAsyncHandler
{
/** @var string */
private $id;

public function getTab()
{
return 'AJAX';
}


public function getPanel()
{
$parameters = array('id' => $this->id);
$json = htmlSpecialChars(json_encode($parameters), ENT_QUOTES);
$id = htmlSpecialChars($this->id);

ob_start();
include __DIR__ . '/ajax.bar.phtml';
return ob_get_clean();
}


public function handleAsyncCall($parameters)
{
return '<strong>' . microtime(TRUE) . '</strong>'
. '<br><xmp>' . var_export($parameters, TRUE) . '</xmp>';
}


public function setHandlerId($id)
{
$this->id = $id;
}

}

Tracy\Debugger::enable();

Tracy\Debugger::getBar()->addPanel($panel = new Panel);
Tracy\Debugger::addAsyncHandler($panel);

Tracy\Debugger::getBar()->addPanel($panel = new Panel);
Tracy\Debugger::addAsyncHandler($panel);

Tracy\Debugger::getBar()->addPanel($panel = new Panel);
Tracy\Debugger::addAsyncHandler($panel);
40 changes: 40 additions & 0 deletions dev/ajax.bar.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* @var string $id
* @var string $json
*/
?>
<script>

var Milo = Milo || {};

(function() {
if (typeof Milo.AjaxPanel === 'undefined') {
Milo.AjaxPanel = {};

var $ = Tracy.Query.factory;

$('div.milo-ajax-panel .ajax').bind('click', function() {
var el = $(this.parentNode).find('.payload')[0];
Tracy.Async.get(
this.getAttribute('data-id'),
this.getAttribute('data-parameters'),
function (responseText) {
el.innerHTML = responseText;
},
function (errorMessage) {
console.log(errorMessage);
}
);

return false;
});
}
})();

</script>
<h1>AJAX Panel</h1>
<div class="milo-ajax-panel">
<a class="ajax" href="#" data-id="<?php echo $id ?>" data-parameters="<?php echo $json ?>">Refresh</a><br>
<div class="payload">(payload)</div>
</div>
43 changes: 43 additions & 0 deletions src/Tracy/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ class Debugger
/** @var ILogger */
private static $fireLogger;

/********************* asynchronous calls ************/

/** @var IAsyncHandler[] */
private static $asyncHandlers = array();


/**
* Static class - cannot be instantiated.
Expand Down Expand Up @@ -403,6 +408,44 @@ public static function getFireLogger()
}


/**
* @param IAsyncHandler
* @param string
*/
public static function addAsyncHandler(IAsyncHandler $handler, $id = NULL)
{
if ($id === NULL) {
$c = 0;
do {
$id = get_class($handler) . ($c++ ? "-$c" : '');
} while (isset(self::$asyncHandlers[$id]));
}

if (self::$enabled && !self::$productionMode && isset($_SERVER['HTTP_X_TRACY_ASYNC']) && $_SERVER['HTTP_X_TRACY_ASYNC'] === "$id") {
try {
set_error_handler(function($severity, $message, $file, $line){
restore_error_handler();
throw new \ErrorException($message, 0, $severity, $file, $line);
});
$result = json_encode($handler->handleAsyncCall(json_decode($_SERVER['HTTP_X_TRACY_ASYNC_PARAMETERS'], TRUE)));
restore_error_handler();

header('Content-Type: application/json; charset=utf-8');
echo $result;

} catch (\Exception $e) {
header('HTTP/1.1 500 Internal Server Error');
header('Content-Type: text/plain; charset=utf-8');
echo $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine();
}
die();
}

self::$asyncHandlers["$id"] = $handler;
$handler->setHandlerId($id);
}


/********************* useful tools ****************d*g**/


Expand Down
34 changes: 34 additions & 0 deletions src/Tracy/IAsyncHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* This file is part of the Tracy (http://tracy.nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/

namespace Tracy;

use Tracy;


/**
* Asynchronous request handler.
*
* @author Miloslav Hůla
*/
interface IAsyncHandler
{

/**
* Handles asynchronous call.
* @param mixed
* @return mixed
*/
function handleAsyncCall($parameters);


/**
* @param string
*/
function setHandlerId($id);

}
31 changes: 31 additions & 0 deletions src/Tracy/templates/bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,35 @@
});
};


var Async = function() {
this.requests = {};
};

Async.prototype.get = function(id, json, onSuccess, onError) {
onSuccess = onSuccess || function() {};
onError = onError || function() {};

if (typeof this.requests[id] !== 'undefined') {
this.requests[id].abort();
}

var request = new XMLHttpRequest(), _this = this;
request.open('GET', document.URL, true);
request.setRequestHeader('X-Tracy-Async', id);
request.setRequestHeader('X-Tracy-Async-Parameters', json);
request.onreadystatechange = function() {
if (request.readyState === XMLHttpRequest.DONE && request.status !== 0) { // 0 = aborted
delete _this.requests[id];
request.status === 200
? onSuccess(JSON.parse(request.responseText))
: onError(request.responseText + request.status);
}
};
request.send();
this.requests[id] = request;
};

Tracy.Async = new Async();

})();
1 change: 1 addition & 0 deletions src/tracy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Copyright (c) 2004, 2014 David Grudl (http://davidgrudl.com)
*/

require __DIR__ . '/Tracy/IAsyncHandler.php';
require __DIR__ . '/Tracy/IBarPanel.php';
require __DIR__ . '/Tracy/Bar.php';
require __DIR__ . '/Tracy/BlueScreen.php';
Expand Down