Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 0 deletions.
119 changes: 119 additions & 0 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Stdlib
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Stdlib;

use ErrorException;

/**
* ErrorHandler that can be used to catch internal PHP errors
* and convert to a ErrorException instance.
*
* @category Zend
* @package Zend_Stdlib
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class ErrorHandler
{
/**
* Flag to mark started
*
* @var boolean
*/
protected static $started = false;

/**
* All errors as one instance of ErrorException
* using the previous exception support.
*
* @var null|ErrorException
*/
protected static $errorException = null;

/**
* If the error handler has been started.
*
* @return boolean
*/
public static function started()
{
return static::$started;
}

/**
* 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');
}

static::$started = true;
static::$errorException = null;

set_error_handler(array(get_called_class(), 'addError'), $errorLevel);
}

/**
* Stopping the error handler
*
* @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 = static::$errorException;

static::$started = false;
static::$errorException = null;
restore_error_handler();

if ($errorException && $throw) {
throw $errorException;
}

return $errorException;
}

/**
* Add an error to the stack.
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return void
*/
public static function addError($errno, $errstr = '', $errfile = '', $errline = 0)
{
static::$errorException = new ErrorException($errstr, 0, $errno, $errfile, $errline, static::$errorException);
}
}
38 changes: 38 additions & 0 deletions src/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Stdlib
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\Stdlib\Exception;

use Zend\Stdlib\Exception;

/**
* logic exception
*
* @category Zend
* @package Zend_Stdlib
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class LogicException extends \LogicException implements Exception
{
}
82 changes: 82 additions & 0 deletions test/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace ZendTest\Stdlib;

use PHPUnit_Framework_TestCase as TestCase,
Zend\Stdlib\ErrorHandler;

class ErrorHandlerTest extends TestCase
{
public function tearDown()
{
if (ErrorHandler::started()) {
ErrorHandler::stop();
}
}

public function testStarted()
{
$this->assertFalse(ErrorHandler::started());

ErrorHandler::start();
$this->assertTrue(ErrorHandler::started());

ErrorHandler::stop();
$this->assertFalse(ErrorHandler::started());
}

public function testStartThrowsLogicException()
{
ErrorHandler::start();

$this->setExpectedException('Zend\Stdlib\Exception\LogicException');
ErrorHandler::start();
}

public function testStopThrowsLogicException()
{
$this->setExpectedException('Zend\Stdlib\Exception\LogicException');
ErrorHandler::stop();
}

public function testReturnCatchedError()
{
ErrorHandler::start();
strpos(); // Invalid argument list
$err = ErrorHandler::stop();

$this->assertInstanceOf('ErrorException', $err);
}

public function testThrowCatchedError()
{
ErrorHandler::start();
strpos(); // Invalid argument list

$this->setExpectedException('ErrorException');
ErrorHandler::stop(true);
}

public function testAddErrors()
{
ErrorHandler::start();
ErrorHandler::addError(1, 'test-msg1', 'test-file1', 100);
ErrorHandler::addError(2, 'test-msg2', 'test-file2', 200);
$err = ErrorHandler::stop();

$this->assertInstanceOf('ErrorException', $err);
$this->assertEquals('test-file2', $err->getFile());
$this->assertEquals('test-msg2', $err->getMessage());
$this->assertEquals(200, $err->getLine());
$this->assertEquals(0, $err->getCode());
$this->assertEquals(2, $err->getSeverity());

$previous = $err->getPrevious();
$this->assertInstanceOf('ErrorException', $previous);
$this->assertEquals('test-file1', $previous->getFile());
$this->assertEquals('test-msg1', $previous->getMessage());
$this->assertEquals(100, $previous->getLine());
$this->assertEquals(0, $previous->getCode());
$this->assertEquals(1, $previous->getSeverity());
}
}

0 comments on commit 497a2c2

Please sign in to comment.