Skip to content

Commit

Permalink
Debugger::enable(): exit() replaced with _exceptionHandler()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 20, 2014
1 parent 5066e54 commit 6fc6846
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 21 deletions.
33 changes: 12 additions & 21 deletions src/Tracy/Debugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ final public function __construct()
*/
public static function enable($mode = NULL, $logDirectory = NULL, $email = NULL)
{
self::$enabled = TRUE;
self::$time = isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(TRUE);
if (isset($_SERVER['REQUEST_URI'])) {
self::$source = (!empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://')
Expand All @@ -168,11 +169,13 @@ public static function enable($mode = NULL, $logDirectory = NULL, $email = NULL)
}

// logging configuration
if ($email !== NULL) {
self::$email = $email;
}
if (is_string($logDirectory)) {
self::$logDirectory = realpath($logDirectory);
if (self::$logDirectory === FALSE) {
echo __METHOD__ . "() error: Log directory is not found or is not directory.\n";
exit(254);
self::_exceptionHandler(new \RuntimeException("Log directory is not found or is not directory."));
}
} elseif ($logDirectory === FALSE || self::$logDirectory === NULL) {
self::$logDirectory = FALSE;
Expand All @@ -188,28 +191,16 @@ public static function enable($mode = NULL, $logDirectory = NULL, $email = NULL)
ini_set('log_errors', FALSE);

} elseif (ini_get('display_errors') != !self::$productionMode && ini_get('display_errors') !== (self::$productionMode ? 'stderr' : 'stdout')) { // intentionally ==
echo __METHOD__ . "() error: Unable to set 'display_errors' because function ini_set() is disabled.\n";
exit(254);
self::_exceptionHandler(new \RuntimeException("Unable to set 'display_errors' because function ini_set() is disabled."));
}

if ($email) {
if (!is_string($email) && !is_array($email)) {
echo __METHOD__ . "() error: Email address must be a string.\n";
exit(254);
}
self::$email = $email;
}

if (!self::$enabled) {
register_shutdown_function(array(__CLASS__, '_shutdownHandler'));
set_exception_handler(array(__CLASS__, '_exceptionHandler'));
set_error_handler(array(__CLASS__, '_errorHandler'));
register_shutdown_function(array(__CLASS__, '_shutdownHandler'));
set_exception_handler(array(__CLASS__, '_exceptionHandler'));
set_error_handler(array(__CLASS__, '_errorHandler'));

foreach (array('Tracy\Bar', 'Tracy\BlueScreen', 'Tracy\DefaultBarPanel', 'Tracy\Dumper',
'Tracy\FireLogger', 'Tracy\Helpers', 'Tracy\Logger', ) as $class) {
class_exists($class);
}
self::$enabled = TRUE;
foreach (array('Tracy\Bar', 'Tracy\BlueScreen', 'Tracy\DefaultBarPanel', 'Tracy\Dumper',
'Tracy\FireLogger', 'Tracy\Helpers', 'Tracy\Logger', ) as $class) {
class_exists($class);
}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Tracy/Debugger.enable().error.development.console.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Test: Tracy\Debugger::enable() error.
* @exitCode 254
* @httpCode 500
* @outputMatch exception 'RuntimeException' with message%A%
*/

use Tracy\Debugger;


require __DIR__ . '/../bootstrap.php';

header('Content-Type: text/plain');

Debugger::enable(Debugger::DEVELOPMENT, 'relative');
19 changes: 19 additions & 0 deletions tests/Tracy/Debugger.enable().error.development.html.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Test: Tracy\Debugger::enable() error.
* @exitCode 254
* @httpCode 500
* @outputMatch %A%<title>RuntimeException</title><!-- Log directory is not found or is not directory. -->%A%
*/

use Tracy\Debugger;


require __DIR__ . '/../bootstrap.php';

if (PHP_SAPI === 'cli') {
Tester\Environment::skip('HTML is not rendered in CLI mode');
}

Debugger::enable(Debugger::DEVELOPMENT, 'relative');
17 changes: 17 additions & 0 deletions tests/Tracy/Debugger.enable().error.production.console.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* Test: Tracy\Debugger::enable() error.
* @exitCode 254
* @httpCode 500
* @outputMatch ERROR: application encountered an error and can not continue.
*/

use Tracy\Debugger;


require __DIR__ . '/../bootstrap.php';

header('Content-Type: text/plain');

Debugger::enable(Debugger::PRODUCTION, 'relative');
19 changes: 19 additions & 0 deletions tests/Tracy/Debugger.enable().error.production.html.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* Test: Tracy\Debugger::enable() error.
* @exitCode 254
* @httpCode 500
* @outputMatch %A%<title>Server Error</title>%A%
*/

use Tracy\Debugger;


require __DIR__ . '/../bootstrap.php';

if (PHP_SAPI === 'cli') {
Tester\Environment::skip('HTML is not rendered in CLI mode');
}

Debugger::enable(Debugger::PRODUCTION, 'relative');
21 changes: 21 additions & 0 deletions tests/Tracy/Debugger.log().error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Test: Tracy\Debugger logging error.
*/

use Tracy\Debugger,
Tester\Assert;


require __DIR__ . '/../bootstrap.php';


// no error
Debugger::$logDirectory = TEMP_DIR;
Debugger::log('Hello');

Debugger::$logDirectory = TEMP_DIR . '/unknown';
Assert::exception(function() {
Debugger::log('Hello');
}, 'RuntimeException', "Directory '%a%' is not found or is not directory.");

0 comments on commit 6fc6846

Please sign in to comment.