This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/zendframework/zf2
- Loading branch information
50 parents
030ff33
+
f2f20f3
+
a50e133
+
4c554ee
+
dbfb1b8
+
ccab83f
+
00b350f
+
78945d0
+
f0e5f4b
+
ceb7d8c
+
9e124d1
+
3de5912
+
b6a974a
+
10a6438
+
cb6c1e7
+
18afd6c
+
3baf1bd
+
c800904
+
f52dcb8
+
126ccb2
+
e7d6206
+
e2d24ab
+
ec1abfc
+
290ea90
+
9f4ca1b
+
edaa760
+
c4c0c95
+
d21f055
+
5b18029
+
e6b97af
+
010fb36
+
64c7b8d
+
636523e
+
4cc2cd6
+
e34098a
+
16367cd
+
943c77f
+
8226e5b
+
0b47726
+
3cd8a03
+
cc4782c
+
9c653a6
+
656dbe5
+
9bce1ba
+
7dc18ca
+
861130d
+
2d2ffbd
+
4f413a5
+
2e1067a
+
1d082e4
commit 497a2c2
Showing
3 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |