Improve how script termination is handled

This commit is contained in:
filp
2013-03-15 18:29:08 +00:00
parent 9deb3799df
commit f4bee324e6
4 changed files with 22 additions and 16 deletions
+1
View File
@@ -22,6 +22,7 @@ class Handler implements HandlerInterface
* to message the handler walker.
*/
const LAST_HANDLER = 0x10;
const QUIT = 0x20;
/**
* @var Damnit\Run
+3 -2
View File
@@ -41,7 +41,7 @@ class PrettyPageHandler extends Handler
// Prepare the $v global variable that will pass relevant
// information to the template
$inspector = $this->getInspector();
$frames = $inspector->getFrames();
$frames = $inspector->getFrames();
$v = (object) array(
'name' => explode('\\', $inspector->getExceptionName()),
@@ -79,7 +79,8 @@ class PrettyPageHandler extends Handler
require $templateFile;
});
return Handler::LAST_HANDLER;
return Handler::QUIT;
}
/**
+17 -13
View File
@@ -20,7 +20,7 @@ class Run
const SHUTDOWN_HANDLER = 'handleShutdown';
protected $isRegistered;
protected $exitWhenDone = true;
protected $allowQuit = true;
/**
* @var DarnIt\Handler\HandlerInterface[]
@@ -123,14 +123,12 @@ class Run
}
/**
* Should Damnit quit the script once all handlers have executed?
* Mosty useful for unit testing.
*
* Should Damnit allow Handlers to force the script to quit?
* @param bool $exit
*/
public function exitWhenDone($exit = true)
public function allowQuit($exit = true)
{
$this->exitWhenDone = (bool) $exit;
$this->allowQuit = (bool) $exit;
}
/**
@@ -155,15 +153,21 @@ class Run
$handlerResponse = $handler->handle($exception);
if($handlerResponse === Handler::LAST_HANDLER) {
// The Handler has handled the exception in some way,
// or signals that no further handlers should be queried,
// but the script execution will continue
break;
} elseif($handlerResponse === Handler::QUIT) {
// The Handler has handled the exception in some way,
// and script execution should terminate, unless specifically
// disallowed, in which case the behavior is the same as
// Handler::LAST_HANDLER
if($this->allowQuit) {
exit;
} else {
break;
}
}
}
// And we're done!
$this->unregister();
if($this->exitWhenDone) {
exit;
}
}
+1 -1
View File
@@ -20,7 +20,7 @@ class RunTest extends TestCase
protected function getRunInstance()
{
$run = new Run;
$run->exitWhenDone(false);
$run->allowQuit(false);
return $run;
}