Skip to content

Commit eb42b07

Browse files
committed
refactoring, isHtmlMode moved to Helpers
1 parent b6da0ff commit eb42b07

3 files changed

Lines changed: 38 additions & 29 deletions

File tree

src/Tracy/Bar.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public function getPanel($id)
5454
public function render()
5555
{
5656
@session_start(); // @ session may be already started or it is not possible to start session
57-
$session = & $_SESSION['__NF']['tracybar-2.3'];
58-
$redirect = preg_match('#^Location:#im', implode("\n", headers_list()));
59-
if ($redirect) {
57+
$previousBars = & $_SESSION['__NF']['tracybar-2.3'];
58+
$isRedirect = preg_match('#^Location:#im', implode("\n", headers_list()));
59+
if ($isRedirect) {
6060
Dumper::fetchLiveData();
61-
Dumper::$livePrefix = count($session) . 'p';
61+
Dumper::$livePrefix = count($previousBars) . 'p';
6262
}
6363

6464
$obLevel = ob_get_level();
@@ -90,14 +90,14 @@ public function render()
9090
}
9191
}
9292

93-
if ($redirect) {
94-
$session[] = ['panels' => $panels, 'liveData' => Dumper::fetchLiveData()];
93+
if ($isRedirect) {
94+
$previousBars[] = ['panels' => $panels, 'liveData' => Dumper::fetchLiveData()];
9595
return;
9696
}
9797

9898
$liveData = Dumper::fetchLiveData();
9999

100-
foreach (array_reverse((array) $session) as $reqId => $info) {
100+
foreach (array_reverse((array) $previousBars) as $reqId => $info) {
101101
$panels[] = [
102102
'tab' => '<span title="Previous request before redirect">previous</span>',
103103
'panel' => NULL,
@@ -109,7 +109,7 @@ public function render()
109109
}
110110
$liveData += $info['liveData'];
111111
}
112-
$session = NULL;
112+
$previousBars = NULL;
113113

114114
require __DIR__ . '/assets/Bar/bar.phtml';
115115
}

src/Tracy/Debugger.php

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,17 @@ public static function enable($mode = NULL, $logDirectory = NULL, $email = NULL)
169169
}
170170
error_reporting(E_ALL);
171171

172-
if (!self::$enabled) {
173-
register_shutdown_function([__CLASS__, 'shutdownHandler']);
174-
set_exception_handler([__CLASS__, 'exceptionHandler']);
175-
set_error_handler([__CLASS__, 'errorHandler']);
172+
if (self::$enabled) {
173+
return;
174+
}
175+
self::$enabled = TRUE;
176176

177-
array_map('class_exists', ['Tracy\Bar', 'Tracy\BlueScreen', 'Tracy\DefaultBarPanel', 'Tracy\Dumper',
178-
'Tracy\FireLogger', 'Tracy\Helpers', 'Tracy\Logger']);
177+
register_shutdown_function([__CLASS__, 'shutdownHandler']);
178+
set_exception_handler([__CLASS__, 'exceptionHandler']);
179+
set_error_handler([__CLASS__, 'errorHandler']);
179180

180-
self::$enabled = TRUE;
181-
}
181+
array_map('class_exists', ['Tracy\Bar', 'Tracy\BlueScreen', 'Tracy\DefaultBarPanel', 'Tracy\Dumper',
182+
'Tracy\FireLogger', 'Tracy\Helpers', 'Tracy\Logger']);
182183
}
183184

184185

@@ -209,7 +210,7 @@ public static function shutdownHandler()
209210
FALSE
210211
);
211212

212-
} elseif (self::$showBar && !connection_aborted() && !self::$productionMode && self::isHtmlMode()) {
213+
} elseif (self::$showBar && !connection_aborted() && !self::$productionMode && Helpers::isHtmlMode()) {
213214
self::$reserved = NULL;
214215
self::removeOutputBuffers(FALSE);
215216
self::getBar()->render();
@@ -234,7 +235,7 @@ public static function exceptionHandler($exception, $exit = TRUE)
234235
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
235236
$code = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE ? 503 : 500;
236237
header("$protocol $code", TRUE, $code);
237-
if (self::isHtmlMode()) {
238+
if (Helpers::isHtmlMode()) {
238239
header('Content-Type: text/html; charset=UTF-8');
239240
}
240241
}
@@ -249,15 +250,15 @@ public static function exceptionHandler($exception, $exit = TRUE)
249250
} catch (\Exception $e) {
250251
}
251252

252-
if (self::isHtmlMode()) {
253+
if (Helpers::isHtmlMode()) {
253254
$logged = empty($e);
254255
require self::$errorTemplate ?: __DIR__ . '/assets/Debugger/error.500.phtml';
255256
} elseif (PHP_SAPI === 'cli') {
256257
fwrite(STDERR, 'ERROR: application encountered an error and can not continue. '
257258
. (isset($e) ? "Unable to log error.\n" : "Error was logged.\n"));
258259
}
259260

260-
} elseif (!connection_aborted() && self::isHtmlMode()) {
261+
} elseif (!connection_aborted() && Helpers::isHtmlMode()) {
261262
self::getBlueScreen()->render($exception);
262263
if (self::$showBar) {
263264
self::getBar()->render();
@@ -368,19 +369,11 @@ public static function errorHandler($severity, $message, $file, $line, $context)
368369

369370
} else {
370371
self::fireLog(new ErrorException($message, 0, $severity, $file, $line));
371-
return self::isHtmlMode() ? NULL : FALSE; // FALSE calls normal error handler
372+
return Helpers::isHtmlMode() ? NULL : FALSE; // FALSE calls normal error handler
372373
}
373374
}
374375

375376

376-
private static function isHtmlMode()
377-
{
378-
return empty($_SERVER['HTTP_X_REQUESTED_WITH'])
379-
&& PHP_SAPI !== 'cli'
380-
&& !preg_match('#^Content-Type: (?!text/html)#im', implode("\n", headers_list()));
381-
}
382-
383-
384377
private static function removeOutputBuffers($errorOccurred)
385378
{
386379
while (ob_get_level() > self::$obLevel) {

src/Tracy/Helpers.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,20 @@ public static function getSuggestion(array $items, $value)
213213
return $best;
214214
}
215215

216+
217+
/** @internal */
218+
public static function isHtmlMode()
219+
{
220+
return !self::isAjax()
221+
&& PHP_SAPI !== 'cli'
222+
&& !preg_match('#^Content-Type: (?!text/html)#im', implode("\n", headers_list()));
223+
}
224+
225+
226+
/** @internal */
227+
public static function isAjax()
228+
{
229+
return isset($_SERVER['HTTP_X_REQUESTED_WITH']);
230+
}
231+
216232
}

0 commit comments

Comments
 (0)