Skip to content

Commit

Permalink
debug: use property editor and editor_basepath (#3526)
Browse files Browse the repository at this point in the history
  • Loading branch information
bloep committed May 2, 2020
1 parent 8d4784d commit 26f096b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
45 changes: 39 additions & 6 deletions redaxo/src/addons/debug/boot.php
Expand Up @@ -8,6 +8,44 @@
if (rex::isBackend() && 'debug' === rex_request::get('page')) {
$index = file_get_contents(rex_addon::get('debug')->getAssetsPath('clockwork/index.html'));

$editor = rex_editor::factory();
$curEditor = $editor->getName();
$editorBasepath = $editor->getBasepath();

$siteKey = rex_debug::getFullClockworkApiUrl();
$localPath = null;
$realPath = null;

if ($editorBasepath) {
$localPath = rex_escape($editorBasepath, 'js');
$realPath = rex_escape(rex_path::base(), 'js');
}

$injectedScript = <<<EOF
<script>
let store;
try {
store = JSON.parse(localStorage.getItem('clockwork'));
} catch (e) {
store = {};
}
if (!store) store = {};
if (!store.settings) store.settings = {};
if (!store.settings.global) store.settings.global = {};
store.settings.global.editor = '$curEditor';
store.settings.global.seenReleaseNotesVersion = "4.1";
if (!store.settings.site) store.settings.site = {};
store.settings.site['$siteKey'] = {localPathMap: {local: "$localPath", real: "$realPath"}};
localStorage.setItem('clockwork', JSON.stringify(store))
</script>
EOF;

$index = str_replace('<body>', '<body>'.$injectedScript, $index);
rex_response::sendPage($index);
exit;
}
Expand All @@ -21,19 +59,14 @@
rex_response::setHeader('X-Clockwork-Id', rex_debug::getInstance()->getRequest()->id);
rex_response::setHeader('X-Clockwork-Version', \Clockwork\Clockwork::VERSION);

rex_response::setHeader('X-Clockwork-Path', rex_url::backendController(['page' => 'structure'] + rex_api_debug::getUrlParams(), false));
rex_response::setHeader('X-Clockwork-Path', rex_debug::getClockworkApiUrl());

register_shutdown_function(static function () {
$clockwork = rex_debug::getInstance();

$clockwork->getTimeline()->endEvent('total');

foreach (rex_timer::$serverTimings as $label => $timings) {
if (!isset($timings['timings'])) {
// compat for redaxo < 5.11
continue;
}

foreach ($timings['timings'] as $i => $timing) {
if ($timing['end'] - $timing['start'] >= 0.001) {
$clockwork->getTimeline()->addEvent($label.'_'.$i, $label, $timing['start'], $timing['end']);
Expand Down
18 changes: 18 additions & 0 deletions redaxo/src/addons/debug/lib/debug.php
Expand Up @@ -65,4 +65,22 @@ public static function getTrace(array $ignoredClasses = []): array
'trace' => array_slice($trace, $start),
];
}

public static function getFullClockworkApiUrl(): string
{
$https = isset($_SERVER['HTTPS']) && 'on' == $_SERVER['HTTPS'];
$host = $_SERVER['HTTP_HOST'];
$port = $_SERVER['SERVER_PORT'] ?? null;
$uri = dirname($_SERVER['REQUEST_URI']).'/'.self::getClockworkApiUrl();

$scheme = $https ? 'https' : 'http';
$port = (!$https && 80 != $port || $https && 443 != $port) ? ":{$port}" : '';

return "{$scheme}://{$host}{$port}{$uri}";
}

public static function getClockworkApiUrl(): string
{
return rex_url::backendPage('structure', rex_api_debug::getUrlParams(), false);
}
}
5 changes: 4 additions & 1 deletion redaxo/src/addons/debug/package.yml
Expand Up @@ -11,4 +11,7 @@ page:
block: system
pjax: false
icon: rex-icon rex-icon-heartbeat
linkAttr: { target: _blank }
linkAttr: { target: _blank }

requires:
redaxo: ^5.11-dev
13 changes: 13 additions & 0 deletions redaxo/src/core/lib/util/editor.php
Expand Up @@ -91,4 +91,17 @@ public function getSupportedEditors()
'xdebug' => 'Xdebug via xdebug.file_link_format (php.ini)',
];
}

/**
* Returns the editor name, e.g. „atom“.
*/
public function getName(): ?string
{
return rex::getProperty('editor');
}

public function getBasepath(): ?string
{
return rex::getProperty('editor_basepath');
}
}

0 comments on commit 26f096b

Please sign in to comment.