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

Commit

Permalink
Browse files Browse the repository at this point in the history
- Removed most usage of ArrayUtils -- most arrays were single
  dimensional, and did not require more than iterator_to_array
- Alphabetized all imports
- Some logic cleanup for readability
- Marked filter and writer plugin managers to NOT share by default
  (allowing multiple instances of the same writer and filter types)
  • Loading branch information
weierophinney committed Jul 24, 2012
1 parent ca6ea07 commit b7a33bb
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 37 deletions.
5 changes: 2 additions & 3 deletions src/Filter/Priority.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

namespace Zend\Log\Filter;

use Zend\Log\Exception;
use Traversable;
use Zend\Stdlib\ArrayUtils;
use Zend\Log\Exception;

/**
* @category Zend
Expand Down Expand Up @@ -43,7 +42,7 @@ class Priority implements FilterInterface
public function __construct($priority, $operator = null)
{
if ($priority instanceof Traversable) {
$priority = ArrayUtils::iteratorToArray($priority);
$priority = iterator_to_array($priority);
}
if (is_array($priority)) {
$operator = isset($priority['operator']) ? $priority['operator'] : null;
Expand Down
5 changes: 2 additions & 3 deletions src/Filter/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

namespace Zend\Log\Filter;

use Traversable;
use Zend\Log\Exception;
use Zend\Stdlib\ErrorHandler;
use Traversable;
use Zend\Stdlib\ArrayUtils;

/**
* @category Zend
Expand All @@ -39,7 +38,7 @@ class Regex implements FilterInterface
public function __construct($regex)
{
if ($regex instanceof Traversable) {
$regex = ArrayUtils::iteratorToArray($regex);
$regex = iterator_to_array($regex);
}
if (is_array($regex)) {
$regex = isset($regex['regex']) ? $regex['regex'] : null;
Expand Down
5 changes: 2 additions & 3 deletions src/Filter/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

namespace Zend\Log\Filter;

use Traversable;
use Zend\Log\Exception;
use Zend\Validator\ValidatorInterface as ZendValidator;
use Traversable;
use Zend\Stdlib\ArrayUtils;

/**
* @category Zend
Expand All @@ -38,7 +37,7 @@ class Validator implements FilterInterface
public function __construct($validator)
{
if ($validator instanceof Traversable) {
$validator = ArrayUtils::iteratorToArray($validator);
$validator = iterator_to_array($validator);
}
if (is_array($validator)) {
$validator = isset($validator['validator']) ? $validator['validator'] : null;
Expand Down
8 changes: 6 additions & 2 deletions src/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ public function addFilter($filter, array $options = null)
{
if (is_int($filter)) {
$filter = new Filter\Priority($filter);
} elseif (is_string($filter)) {
}

if (is_string($filter)) {
$filter = $this->filterPlugin($filter, $options);
} elseif (!$filter instanceof Filter\FilterInterface) {
}

if (!$filter instanceof Filter\FilterInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'Writer must implement Zend\Log\Filter\FilterInterface; received "%s"',
is_object($filter) ? get_class($filter) : gettype($filter)
Expand Down
9 changes: 5 additions & 4 deletions src/Writer/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

namespace Zend\Log\Writer;

use Traversable;
use Zend\Db\Adapter\Adapter;
use Zend\Log\Exception;
use Zend\Log\Formatter;
use Traversable;
use Zend\Stdlib\ArrayUtils;

/**
* @category Zend
Expand Down Expand Up @@ -66,15 +65,17 @@ class Db extends AbstractWriter
public function __construct($db, $tableName, array $columnMap = null, $separator = null)
{
if ($db instanceof Traversable) {
$db = ArrayUtils::iteratorToArray($db);
$db = iterator_to_array($db);
}

if (is_array($db)) {
$separator = isset($db['separator']) ? $db['separator'] : null;
$columnMap = isset($db['column']) ? $db['column'] : null;
$tableName = isset($db['table']) ? $db['table'] : null;
$db = isset($db['db']) ? $db['db'] : null;
}
if ($db === null || !($db instanceof Adapter)) {

if (!$db instanceof Adapter) {
throw new Exception\InvalidArgumentException('You must pass a valid Zend\Db\Adapter\Adapter');
}

Expand Down
7 changes: 7 additions & 0 deletions src/Writer/FilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class FilterPluginManager extends AbstractPluginManager
'validator' => 'Zend\Log\Filter\Validator',
);

/**
* Allow many filters of the same type
*
* @var bool
*/
protected $shareByDefault = false;

/**
* Validate the plugin
*
Expand Down
35 changes: 19 additions & 16 deletions src/Writer/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@

namespace Zend\Log\Writer;

use Traversable;
use Zend\Log\Exception;
use Zend\Log\Formatter\Simple as SimpleFormatter;
use Zend\Mail\Message as MailMessage;
use Zend\Mail\Transport;
use Zend\Mail\Transport\Exception as TransportException;
use Traversable;
use Zend\Stdlib\ArrayUtils;

/**
* Class used for writing log messages to email via Zend\Mail.
Expand Down Expand Up @@ -82,31 +81,35 @@ class Mail extends AbstractWriter
public function __construct($mail, Transport\TransportInterface $transport = null)
{
if ($mail instanceof Traversable) {
$mail = ArrayUtils::iteratorToArray($mail);
$mail = iterator_to_array($mail);
}

if (is_array($mail)) {
$transport = isset($mail['transport']) ? $mail['transport'] : null;
$mail = isset($mail['mail']) ? $mail['mail'] : null;
if (!($transport instanceof Transport\TransportInterface)) {
throw new Exception\InvalidArgumentException(
'Parameter of type %s is invalid; must be Zend\Mail\Transport\TransportInterface',
(is_object($mail[1]) ? get_class($mail[1]) : gettype($mail[1]))
);
}
}
if (!($mail instanceof MailMessage)) {

// Ensure we have a valid mail message
if (!$mail instanceof MailMessage) {
throw new Exception\InvalidArgumentException(
'Parameter of type %s is invalid; must be Zend\Mail\Message',
'Mail parameter of type %s is invalid; must be of type Zend\Mail\Message',
(is_object($mail) ? get_class($mail) : gettype($mail))
);
}
$this->mail = $mail;
if (null !== $transport) {
$this->setTransport($transport);
} else {
// default transport
$this->setTransport(new Transport\Sendmail());

// Ensure we have a valid mail transport
if (null === $transport) {
$transport = new Transport\Sendmail();
}
if (!$transport instanceof Transport\TransportInterface) {
throw new Exception\InvalidArgumentException(
'Transport parameter of type %s is invalid; must be of type Zend\Mail\Transport\TransportInterface',
(is_object($transport) ? get_class($transport) : gettype($transport))
);
}
$this->setTransport($transport);

$this->formatter = new SimpleFormatter();
}

Expand Down
7 changes: 5 additions & 2 deletions src/Writer/MongoDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
namespace Zend\Log\Writer;

use Mongo;
use Traversable;
use Zend\Log\Exception\InvalidArgumentException;
use Zend\Log\Exception\RuntimeException;
use Zend\Log\Formatter\FormatterInterface;
use Traversable;
use Zend\Stdlib\ArrayUtils;

/**
Expand Down Expand Up @@ -53,6 +53,7 @@ class MongoDB extends AbstractWriter
public function __construct($mongo, $database, $collection, array $saveOptions = array())
{
if ($mongo instanceof Traversable) {
// Configuration may be multi-dimensional due to save options
$mongo = ArrayUtils::iteratorToArray($mongo);
}
if (is_array($mongo)) {
Expand All @@ -71,14 +72,16 @@ public function __construct($mongo, $database, $collection, array $saveOptions =
}
$mongo = isset($mongo['mongo']) ? $mongo['mongo'] : null;
}

if (!($mongo instanceof Mongo)) {
throw new Exception\InvalidArgumentException(
'Parameter of type %s is invalid; must be Mongo',
(is_object($mongo) ? get_class($mongo) : gettype($mongo))
);
}

$this->mongoCollection = $mongo->selectCollection($database, $collection);
$this->saveOptions = $saveOptions;
$this->saveOptions = $saveOptions;
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/Writer/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@

namespace Zend\Log\Writer;

use Traversable;
use Zend\Log\Exception;
use Zend\Log\Formatter\Simple as SimpleFormatter;
use Zend\Stdlib\ErrorHandler;
use Traversable;
use Zend\Stdlib\ArrayUtils;

/**
* @category Zend
Expand Down Expand Up @@ -42,12 +41,14 @@ class Stream extends AbstractWriter
public function __construct($streamOrUrl, $mode = null)
{
if ($streamOrUrl instanceof Traversable) {
$streamOrUrl = ArrayUtils::iteratorToArray($streamOrUrl);
$streamOrUrl = iterator_to_array($streamOrUrl);
}

if (is_array($streamOrUrl)) {
$mode = isset($streamOrUrl['mode']) ? $streamOrUrl['mode'] : null;
$streamOrUrl = isset($streamOrUrl['stream']) ? $streamOrUrl['stream'] : null;
}

// Setting the default mode
if (null === $mode) {
$mode = 'a';
Expand All @@ -70,7 +71,7 @@ public function __construct($streamOrUrl, $mode = null)

$this->stream = $streamOrUrl;
} else {
if (! $this->stream = @fopen($streamOrUrl, $mode, false)) {
if (!$this->stream = @fopen($streamOrUrl, $mode, false)) {
throw new Exception\RuntimeException(sprintf(
'"%s" cannot be opened with mode "%s"',
$streamOrUrl,
Expand Down
7 changes: 7 additions & 0 deletions src/WriterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class WriterPluginManager extends AbstractPluginManager
'zendmonitor' => 'Zend\Log\Writer\ZendMonitor',
);

/**
* Allow many writers of the same type
*
* @var bool
*/
protected $shareByDefault = false;

/**
* Validate the plugin
*
Expand Down

0 comments on commit b7a33bb

Please sign in to comment.