Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Updated constructors to accept options array according to AbstractWriter... #3086

Closed
wants to merge 1 commit into from

2 participants

Stefan Kleff Matthew Weier O'Phinney
Stefan Kleff

....php to work with logger factory. See #2725 (comment)

Matthew Weier O'Phinney

Technically a new feature, so merged to develop branch for 2.1.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 28, 2012
  1. Stefan Kleff

    Updated constructors to accept options array according to AbstractWri…

    stefankleff authored
    …ter.php to work with logger factory
This page is out of date. Refresh to see the latest.
21 library/Zend/Log/Writer/AbstractWriter.php
View
@@ -84,13 +84,22 @@ public function __construct($options = null)
if (is_array($options)) {
- if(isset($options['filters']) && is_array($options['filters'])) {
- foreach($options['filters'] as $filter) {
- if(!isset($filter['name'])) {
- throw new Exception\InvalidArgumentException('Options must contain a name for the filter');
+ if(isset($options['filters'])) {
+ $filters = $options['filters'];
+ if(is_string($filters) || $filters instanceof Filter\FilterInterface) {
+ $this->addFilter($filters);
+ } elseif(is_array($filters)) {
+ foreach($filters as $filter) {
+ if(is_string($filter) || $filter instanceof Filter\FilterInterface) {
+ $this->addFilter($filter);
+ } elseif(is_array($filter)) {
+ if(!isset($filter['name'])) {
+ throw new Exception\InvalidArgumentException('Options must contain a name for the filter');
+ }
+ $filterOptions = (isset($filter['options'])) ? $filter['options'] : null;
+ $this->addFilter($filter['name'], $filterOptions);
+ }
}
- $filterOptions = (isset($filter['options'])) ? $filter['options'] : null;
- $this->addFilter($filter['name'], $filterOptions);
}
}
19 library/Zend/Log/Writer/ChromePhp.php
View
@@ -10,10 +10,12 @@
namespace Zend\Log\Writer;
+use Traversable;
use Zend\Log\Writer\ChromePhp\ChromePhpBridge;
use Zend\Log\Writer\ChromePhp\ChromePhpInterface;
use Zend\Log\Formatter\ChromePhp as ChromePhpFormatter;
use Zend\Log\Logger;
+use Zend\Log\Exception;
/**
* @category Zend
@@ -32,11 +34,24 @@ class ChromePhp extends AbstractWriter
/**
* Initializes a new instance of this class.
*
- * @param null|ChromePhpInterface $instance An instance of ChromePhpInterface
+ * @param null|ChromePhpInterface|array|Traversable $instance An instance of ChromePhpInterface
* that should be used for logging
*/
- public function __construct(ChromePhpInterface $instance = null)
+ public function __construct($instance = null)
{
+ if ($instance instanceof Traversable) {
+ $inatce = iterator_to_array($instance);
+ }
+
+ if (is_array($instance)) {
+ parent::__construct($instance);
+ $instance = isset($instance['instance']) ? $instance['instance'] : null;
+ }
+
+ if(!($instance instanceof ChromePhpInterface || $instance === null)) {
+ throw new Exception\InvalidArgumentException('You must pass a valid Zend\Log\Writer\ChromePhp\ChromePhpInterface');
+ }
+
$this->chromephp = $instance === null ? $this->getChromePhp() : $instance;
$this->formatter = new ChromePhpFormatter();
}
1  library/Zend/Log/Writer/Db.php
View
@@ -69,6 +69,7 @@ public function __construct($db, $tableName = null, array $columnMap = null, $se
}
if (is_array($db)) {
+ parent::__construct($db);
$separator = isset($db['separator']) ? $db['separator'] : null;
$columnMap = isset($db['column']) ? $db['column'] : null;
$tableName = isset($db['table']) ? $db['table'] : null;
20 library/Zend/Log/Writer/FirePhp.php
View
@@ -10,10 +10,12 @@
namespace Zend\Log\Writer;
+use Traversable;
use FirePHP as FirePHPService;
use Zend\Log\Formatter\FirePhp as FirePhpFormatter;
use Zend\Log\Logger;
use Zend\Log\Exception;
+use FirePhp\FirePhpInterface;
/**
* @category Zend
@@ -32,11 +34,25 @@ class FirePhp extends AbstractWriter
/**
* Initializes a new instance of this class.
*
- * @param null|FirePhp\FirePhpInterface $instance An instance of FirePhpInterface
+ * @param null|FirePhp\FirePhpInterface|array|Traversable $instance An instance of FirePhpInterface
* that should be used for logging
*/
- public function __construct(FirePhp\FirePhpInterface $instance = null)
+ public function __construct($instance = null)
{
+ if ($instance instanceof Traversable) {
+ $instance = iterator_to_array($instance);
+ }
+
+ if (is_array($instance)) {
+ parent::__construct($instance);
+ $instance = isset($instance['instance']) ? $instance['instance'] : null;
+ }
+
+ if ($instance instanceof FirePhpInterface) {
+ throw new Exception\InvalidArgumentException('You must pass a valid FirePhp\FirePhpInterface');
+ }
+
+
$this->firephp = $instance;
$this->formatter = new FirePhpFormatter();
}
5 library/Zend/Log/Writer/Mail.php
View
@@ -85,6 +85,7 @@ public function __construct($mail, Transport\TransportInterface $transport = nul
}
if (is_array($mail)) {
+ parent::__construct($mail);
$transport = isset($mail['transport']) ? $mail['transport'] : null;
$mail = isset($mail['mail']) ? $mail['mail'] : null;
}
@@ -110,7 +111,9 @@ public function __construct($mail, Transport\TransportInterface $transport = nul
}
$this->setTransport($transport);
- $this->formatter = new SimpleFormatter();
+ if($this->formatter === null) {
+ $this->formatter = new SimpleFormatter();
+ }
}
/**
28 library/Zend/Log/Writer/MongoDB.php
View
@@ -15,8 +15,7 @@
use Mongo;
use MongoDate;
use Traversable;
-use Zend\Log\Exception\InvalidArgumentException;
-use Zend\Log\Exception\RuntimeException;
+use Zend\Log\Exception;
use Zend\Log\Formatter\FormatterInterface;
use Zend\Stdlib\ArrayUtils;
@@ -52,27 +51,30 @@ class MongoDB extends AbstractWriter
* @param array $saveOptions
* @throws Exception\InvalidArgumentException
*/
- public function __construct($mongo, $database, $collection, array $saveOptions = array())
+ public function __construct($mongo, $database = null, $collection = null, array $saveOptions = array())
{
if ($mongo instanceof Traversable) {
// Configuration may be multi-dimensional due to save options
$mongo = ArrayUtils::iteratorToArray($mongo);
}
if (is_array($mongo)) {
+ parent::__construct($mongo);
$saveOptions = isset($mongo['save_options']) ? $mongo['save_options'] : null;
$collection = isset($mongo['collection']) ? $mongo['collection'] : null;
- if (null === $collection) {
- throw new Exception\InvalidArgumentException(
+ $database = isset($mongo['database']) ? $mongo['database'] : null;
+ $mongo = isset($mongo['mongo']) ? $mongo['mongo'] : null;
+ }
+
+ if (null === $collection) {
+ throw new Exception\InvalidArgumentException(
'The collection parameter cannot be empty'
- );
- }
- $database = isset($mongo['database']) ? $mongo['database'] : null;
- if (null === $database) {
- throw new Exception\InvalidArgumentException(
+ );
+ }
+
+ if (null === $database) {
+ throw new Exception\InvalidArgumentException(
'The database parameter cannot be empty'
- );
- }
- $mongo = isset($mongo['mongo']) ? $mongo['mongo'] : null;
+ );
}
if (!($mongo instanceof Mongo)) {
5 library/Zend/Log/Writer/Stream.php
View
@@ -53,6 +53,7 @@ public function __construct($streamOrUrl, $mode = null, $logSeparator = null)
}
if (is_array($streamOrUrl)) {
+ parent::__construct($streamOrUrl);
$mode = isset($streamOrUrl['mode']) ? $streamOrUrl['mode'] : null;
$logSeparator = isset($streamOrUrl['log_separator']) ? $streamOrUrl['log_separator'] : null;
$streamOrUrl = isset($streamOrUrl['stream']) ? $streamOrUrl['stream'] : null;
@@ -96,7 +97,9 @@ public function __construct($streamOrUrl, $mode = null, $logSeparator = null)
$this->setLogSeparator($logSeparator);
}
- $this->formatter = new SimpleFormatter();
+ if($this->formatter === null) {
+ $this->formatter = new SimpleFormatter();
+ }
}
/**
26 library/Zend/Log/Writer/Syslog.php
View
@@ -10,6 +10,7 @@
namespace Zend\Log\Writer;
+use Traversable;
use Zend\Log\Exception;
use Zend\Log\Logger;
use Zend\Log\Formatter\Simple as SimpleFormatter;
@@ -87,23 +88,34 @@ class Syslog extends AbstractWriter
* @param array $params Array of options; may include "application" and "facility" keys
* @return Syslog
*/
- public function __construct(array $params = array())
+ public function __construct($params = null)
{
- if (isset($params['application'])) {
- $this->appName = $params['application'];
+ if ($params instanceof Traversable) {
+ $params = iterator_to_array($params);
}
$runInitializeSyslog = true;
- if (isset($params['facility'])) {
- $this->setFacility($params['facility']);
- $runInitializeSyslog = false;
+
+ if (is_array($params)) {
+ parent::__construct($params);
+
+ if (isset($params['application'])) {
+ $this->appName = $params['application'];
+ }
+
+ if (isset($params['facility'])) {
+ $this->setFacility($params['facility']);
+ $runInitializeSyslog = false;
+ }
}
if ($runInitializeSyslog) {
$this->initializeSyslog();
}
- $this->setFormatter(new SimpleFormatter('%message%'));
+ if($this->formatter === null) {
+ $this->setFormatter(new SimpleFormatter('%message%'));
+ }
}
/**
4 library/Zend/Log/Writer/ZendMonitor.php
View
@@ -36,8 +36,10 @@ class ZendMonitor extends AbstractWriter
*
* @return ZendMonitor
*/
- public function __construct()
+ public function __construct($options = null)
{
+ parent::__construct($options);
+
if (!function_exists('monitor_custom_event')) {
$this->isEnabled = false;
}
17 tests/ZendTest/Log/Writer/ChromePhpTest.php
View
@@ -80,4 +80,21 @@ public function testWriteDisabled()
));
$this->assertTrue(empty($this->chromephp->calls));
}
+
+ public function testConstructWithOptions()
+ {
+ $formatter = new \Zend\Log\Formatter\Simple();
+ $filter = new \Zend\Log\Filter\Mock();
+ $writer = new ChromePhp(array(
+ 'filters' => $filter,
+ 'formatter' => $formatter,
+ 'instance' => $this->chromephp,
+ ));
+ $this->assertTrue($writer->getChromePhp() instanceof ChromePhpInterface);
+ $this->assertAttributeInstanceOf('Zend\Log\Formatter\ChromePhp', 'formatter', $writer);
+
+ $filters = self::readAttribute($writer, 'filters');
+ $this->assertCount(1, $filters);
+ $this->assertEquals($filter, $filters[0]);
+ }
}
19 tests/ZendTest/Log/Writer/DbTest.php
View
@@ -228,4 +228,23 @@ public function testWriteDateTimeAsExtraValue()
'extra_request_time' => $date->format(FormatterInterface::DEFAULT_DATETIME_FORMAT)
)), $this->db->calls['execute'][0]);
}
+
+ public function testConstructWithOptions()
+ {
+ $formatter = new \Zend\Log\Formatter\Simple();
+ $filter = new \Zend\Log\Filter\Mock();
+ $writer = new DbWriter(array(
+ 'filters' => $filter,
+ 'formatter' => $formatter,
+ 'table' => $this->tableName,
+ 'db' => $this->db,
+
+ ));
+ $this->assertInstanceOf('Zend\Log\Writer\Db', $writer);
+ $this->assertAttributeEquals($this->tableName, 'tableName', $writer);
+
+ $filters = self::readAttribute($writer, 'filters');
+ $this->assertCount(1, $filters);
+ $this->assertEquals($filter, $filters[0]);
+ }
}
17 tests/ZendTest/Log/Writer/FirePhpTest.php
View
@@ -88,4 +88,21 @@ public function testWriteDisabled()
));
$this->assertTrue(empty($this->firephp->calls));
}
+
+ public function testConstructWithOptions()
+ {
+ $formatter = new \Zend\Log\Formatter\Simple();
+ $filter = new \Zend\Log\Filter\Mock();
+ $writer = new FirePhp(array(
+ 'filters' => $filter,
+ 'formatter' => $formatter,
+ 'instance' => $this->firephp,
+ ));
+ $this->assertTrue($writer->getFirePhp() instanceof FirePhpInterface);
+ $this->assertAttributeInstanceOf('Zend\Log\Formatter\FirePhp', 'formatter', $writer);
+
+ $filters = self::readAttribute($writer, 'filters');
+ $this->assertCount(1, $filters);
+ $this->assertEquals($filter, $filters[0]);
+ }
}
30 tests/ZendTest/Log/Writer/MailTest.php
View
@@ -83,4 +83,34 @@ public function testSetSubjectPrependText()
$this->assertContains('an info message', $contents);
$this->assertContains('Subject: test', $contents);
}
+
+ public function testConstructWithOptions()
+ {
+ $message = new MailMessage();
+ $transport = new Transport\File();
+ $options = new Transport\FileOptions(array(
+ 'path' => __DIR__,
+ 'callback' => function (Transport\File $transport) {
+ return MailTest::FILENAME;
+ },
+ ));
+ $transport->setOptions($options);
+
+ $formatter = new \Zend\Log\Formatter\Simple();
+ $filter = new \Zend\Log\Filter\Mock();
+ $writer = new MailWriter(array(
+ 'filters' => $filter,
+ 'formatter' => $formatter,
+ 'mail' => $message,
+ 'transport' => $transport,
+ ));
+
+ $this->assertAttributeEquals($message, 'mail', $writer);
+ $this->assertAttributeEquals($transport, 'transport', $writer);
+ $this->assertAttributeEquals($formatter, 'formatter', $writer);
+
+ $filters = self::readAttribute($writer, 'filters');
+ $this->assertCount(1, $filters);
+ $this->assertEquals($filter, $filters[0]);
+ }
}
26 tests/ZendTest/Log/Writer/StreamTest.php
View
@@ -146,4 +146,30 @@ public function testAllowsSpecifyingLogSeparatorWithinArrayPassedToConstructor()
$writer = new StreamWriter($options);
$this->assertEquals('::', $writer->getLogSeparator());
}
+
+ public function testConstructWithOptions()
+ {
+ $formatter = new \Zend\Log\Formatter\Simple();
+ $filter = new \Zend\Log\Filter\Mock();
+ $writer = new StreamWriter(array(
+ 'filters' => $filter,
+ 'formatter' => $formatter,
+ 'stream' => 'php://memory',
+ 'mode' => 'w+',
+ 'log_separator' => '::',
+
+ ));
+ $this->assertEquals('::', $writer->getLogSeparator());
+ $this->assertAttributeEquals($formatter, 'formatter', $writer);
+
+ $filters = self::readAttribute($writer, 'filters');
+ $this->assertCount(1, $filters);
+ $this->assertEquals($filter, $filters[0]);
+ }
+
+ public function testDefaultFormatter()
+ {
+ $writer = new StreamWriter('php://memory');
+ $this->assertAttributeInstanceOf('Zend\Log\Formatter\Simple', 'formatter', $writer);
+ }
}
23 tests/ZendTest/Log/Writer/SyslogTest.php
View
@@ -102,4 +102,27 @@ public function testPassApplicationNameViaConstructor()
$writer = new CustomSyslogWriter(array('application' => 'test_app'));
$this->assertEquals('test_app', $writer->getApplicationName());
}
+
+ public function testConstructWithOptions()
+ {
+ $formatter = new \Zend\Log\Formatter\Simple();
+ $filter = new \Zend\Log\Filter\Mock();
+ $writer = new CustomSyslogWriter(array(
+ 'filters' => $filter,
+ 'formatter' => $formatter,
+ 'application' => 'test_app',
+ ));
+ $this->assertEquals('test_app', $writer->getApplicationName());
+ $this->assertAttributeEquals($formatter, 'formatter', $writer);
+
+ $filters = self::readAttribute($writer, 'filters');
+ $this->assertCount(1, $filters);
+ $this->assertEquals($filter, $filters[0]);
+ }
+
+ public function testDefaultFormatter()
+ {
+ $writer = new CustomSyslogWriter(array('application' => 'test_app'));
+ $this->assertAttributeInstanceOf('Zend\Log\Formatter\Simple', 'formatter', $writer);
+ }
}
Something went wrong with that request. Please try again.