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

Commit

Permalink
Merge branch 'hotfix/zf2-541'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 10 deletions.
51 changes: 49 additions & 2 deletions src/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Zend\Log\Exception;
use Zend\Log\Filter;
use Zend\Log\Formatter\FormatterInterface as Formatter;
use Zend\Stdlib\ErrorHandler;

/**
* @category Zend
Expand Down Expand Up @@ -42,6 +43,20 @@ abstract class AbstractWriter implements WriterInterface
*/
protected $formatter;

/**
* Use Zend\Stdlib\ErrorHandler to report errors during calls to write
*
* @var bool
*/
protected $convertWriteErrorsToExceptions = true;

/**
* Error level passed to Zend\Stdlib\ErrorHandler::start for errors reported during calls to write
*
* @var bool
*/
protected $errorsToExceptionsConversionLevel = E_WARNING;

/**
* Add a filter specific to this writer.
*
Expand Down Expand Up @@ -134,8 +149,30 @@ public function write(array $event)
}
}

// exception occurs on error
$this->doWrite($event);
$errorHandlerStarted = false;

if ($this->convertWriteErrorsToExceptions && !ErrorHandler::started()) {
ErrorHandler::start($this->errorsToExceptionsConversionLevel);
$errorHandlerStarted = true;
}

try {
$this->doWrite($event);
} catch (\Exception $e) {
if ($errorHandlerStarted) {
ErrorHandler::stop();
$errorHandlerStarted = false;
}
throw $e;
}

if ($errorHandlerStarted) {
$error = ErrorHandler::stop();
$errorHandlerStarted = false;
if ($error) {
throw new Exception\RuntimeException("Unable to write", 0, $error);
}
}
}

/**
Expand All @@ -150,6 +187,16 @@ public function setFormatter(Formatter $formatter)
return $this;
}

/**
* Set convert write errors to exception flag
*
* @param bool $ignoreWriteErrors
*/
public function setConvertWriteErrorsToExceptions($convertErrors)
{
$this->convertWriteErrorsToExceptions = $convertErrors;
}

/**
* Perform shutdown activities such as closing open resources
*
Expand Down
8 changes: 1 addition & 7 deletions src/Writer/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,7 @@ public function __construct($streamOrUrl, $mode = null, $logSeparator = null)
protected function doWrite(array $event)
{
$line = $this->formatter->format($event) . $this->logSeparator;

ErrorHandler::start(E_WARNING);
$result = fwrite($this->stream, $line);
$error = ErrorHandler::stop();
if (false === $result) {
throw new Exception\RuntimeException("Unable to write to stream", 0, $error);
}
fwrite($this->stream, $line);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions test/TestAsset/ErrorGeneratingWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Log
*/

namespace ZendTest\Log\TestAsset;

use Zend\Log\Writer\AbstractWriter;

class ErrorGeneratingWriter extends AbstractWriter
{
protected function doWrite(array $event)
{
$stream = fopen("php://memory", "r");
fclose($stream);
fwrite($stream, "test");
}
}
14 changes: 13 additions & 1 deletion test/Writer/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace ZendTest\Log\Writer;

use ZendTest\Log\TestAsset\ConcreteWriter;
use ZendTest\Log\TestAsset\ErrorGeneratingWriter;
use Zend\Log\Formatter\Simple as SimpleFormatter;
use Zend\Log\Filter\Regex as RegexFilter;

Expand Down Expand Up @@ -69,4 +70,15 @@ public function testFluentInterface()

$this->assertTrue($instance instanceof ConcreteWriter);
}
}

public function testConvertErrorsToException()
{
$writer = new ErrorGeneratingWriter();
$this->setExpectedException('Zend\Log\Exception\RuntimeException');
$writer->write(array('message' => 'test'));

$writer->setConvertWriteErrorsToExceptions(false);
$this->setExpectedException('PHPUnit_Framework_Error_Warning');
$writer->write(array('message' => 'test'));
}
}

0 comments on commit 8af24aa

Please sign in to comment.