Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'feature/log-nested-handlers' into develop

Close #3257
  • Loading branch information...
commit 7b27f0346e3b1ecba593cbd6d6aad844752582bf 2 parents f6da888 + a93c84f
Matthew Weier O'Phinney weierophinney authored
72 library/Zend/Stdlib/ErrorHandler.php
View
@@ -22,46 +22,44 @@
abstract class ErrorHandler
{
/**
- * Flag to mark started
+ * Active stack
*
- * @var boolean
+ * @var array
*/
- protected static $started = false;
+ protected static $stack = array();
/**
- * All errors as one instance of ErrorException
- * using the previous exception support.
+ * Check if this error handler is active
*
- * @var null|ErrorException
+ * @return boolean
*/
- protected static $errorException = null;
+ public static function started()
+ {
+ return (bool) static::getNestedLevel();
+ }
/**
- * If the error handler has been started.
+ * Get the current nested level
*
- * @return boolean
+ * @return int
*/
- public static function started()
+ public static function getNestedLevel()
{
- return static::$started;
+ return count(static::$stack);
}
/**
* Starting the error handler
*
* @param int $errorLevel
- * @throws Exception\LogicException If already started
*/
public static function start($errorLevel = \E_WARNING)
{
- if (static::started() === true) {
- throw new Exception\LogicException('ErrorHandler already started');
+ if (!static::$stack) {
+ set_error_handler(array(get_called_class(), 'addError'), $errorLevel);
}
- static::$started = true;
- static::$errorException = null;
-
- set_error_handler(array(get_called_class(), 'addError'), $errorLevel);
+ static::$stack[] = null;
}
/**
@@ -69,30 +67,43 @@ public static function start($errorLevel = \E_WARNING)
*
* @param boolean $throw Throw the ErrorException if any
* @return null|ErrorException
- * @throws Exception\LogicException If not started before
* @throws ErrorException If an error has been catched and $throw is true
*/
public static function stop($throw = false)
{
- if (static::started() === false) {
- throw new Exception\LogicException('ErrorHandler not started');
- }
+ $errorException = null;
- $errorException = static::$errorException;
+ if (static::$stack) {
+ $errorException = array_pop(static::$stack);
- static::$started = false;
- static::$errorException = null;
- restore_error_handler();
+ if (!static::$stack) {
+ restore_error_handler();
+ }
- if ($errorException && $throw) {
- throw $errorException;
+ if ($errorException && $throw) {
+ throw $errorException;
+ }
}
return $errorException;
}
/**
- * Add an error to the stack.
+ * Stop all active handler
+ *
+ * @return void
+ */
+ public static function clean()
+ {
+ if (static::$stack) {
+ restore_error_handler();
+ }
+
+ static::$stack = array();
+ }
+
+ /**
+ * Add an error to the stack
*
* @param int $errno
* @param string $errstr
@@ -102,6 +113,7 @@ public static function stop($throw = false)
*/
public static function addError($errno, $errstr = '', $errfile = '', $errline = 0)
{
- static::$errorException = new ErrorException($errstr, 0, $errno, $errfile, $errline, static::$errorException);
+ $stack = & static::$stack[ count(static::$stack) - 1 ];
+ $stack = new ErrorException($errstr, 0, $errno, $errfile, $errline, $stack);
}
}
37 tests/ZendTest/Stdlib/ErrorHandlerTest.php
View
@@ -17,34 +17,49 @@ class ErrorHandlerTest extends TestCase
{
public function tearDown()
{
- if (ErrorHandler::started()) {
- ErrorHandler::stop();
+ if (ErrorHandler::getNestedLevel()) {
+ ErrorHandler::clean();
}
}
- public function testStarted()
+ public function testNestedLevel()
{
- $this->assertFalse(ErrorHandler::started());
+ $this->assertSame(0, ErrorHandler::getNestedLevel());
ErrorHandler::start();
- $this->assertTrue(ErrorHandler::started());
+ $this->assertSame(1, ErrorHandler::getNestedLevel());
+
+ ErrorHandler::start();
+ $this->assertSame(2, ErrorHandler::getNestedLevel());
ErrorHandler::stop();
- $this->assertFalse(ErrorHandler::started());
+ $this->assertSame(1, ErrorHandler::getNestedLevel());
+
+ ErrorHandler::stop();
+ $this->assertSame(0, ErrorHandler::getNestedLevel());
}
- public function testStartThrowsLogicException()
+ public function testClean()
{
ErrorHandler::start();
+ $this->assertSame(1, ErrorHandler::getNestedLevel());
- $this->setExpectedException('Zend\Stdlib\Exception\LogicException');
ErrorHandler::start();
+ $this->assertSame(2, ErrorHandler::getNestedLevel());
+
+ ErrorHandler::clean();
+ $this->assertSame(0, ErrorHandler::getNestedLevel());
}
- public function testStopThrowsLogicException()
+ public function testStarted()
{
- $this->setExpectedException('Zend\Stdlib\Exception\LogicException');
+ $this->assertFalse(ErrorHandler::started());
+
+ ErrorHandler::start();
+ $this->assertTrue(ErrorHandler::started());
+
ErrorHandler::stop();
+ $this->assertFalse(ErrorHandler::started());
}
public function testReturnCatchedError()
@@ -65,7 +80,7 @@ public function testThrowCatchedError()
ErrorHandler::stop(true);
}
- public function testAddErrors()
+ public function testAddError()
{
ErrorHandler::start();
ErrorHandler::addError(1, 'test-msg1', 'test-file1', 100);
Please sign in to comment.
Something went wrong with that request. Please try again.