Permalink
Browse files

added the ability to use options array in FingersCrossed as well

  • Loading branch information...
1 parent 9358bb8 commit a18574ac4c5e8cd265844445e8cc96f982d710be @stefankleff committed Oct 10, 2012
Showing with 118 additions and 2 deletions.
  1. +102 −2 library/Zend/Log/Writer/FingersCrossed.php
  2. +16 −0 tests/ZendTest/Log/Writer/FingersCrossedTest.php
@@ -9,13 +9,16 @@
*/
namespace Zend\Log\Writer;
+use Traversable;
+use Zend\Stdlib\ArrayUtils;
use Zend\Log\Filter\Priority as PriorityFilter;
use Zend\Log\Filter\FilterInterface;
use Zend\Log\Formatter\FormatterInterface;
use Zend\Log\Exception;
use Zend\Log\Logger;
use Zend\Log\Writer\WriterInterface;
use Zend\Log\Writer\AbstractWriter;
+use Zend\Log\WriterPluginManager;
/**
* Buffers all events until the strategy determines to flush them.
@@ -34,6 +37,13 @@ class FingersCrossed extends AbstractWriter
* @var WriterInterface
*/
protected $writer;
+
+ /**
+ * Writer plugins
+ *
+ * @var WriterPluginManager
+ */
+ protected $writerPlugins;
/**
* Flag if buffering is enabled
@@ -60,23 +70,113 @@ class FingersCrossed extends AbstractWriter
/**
* Constructor
*
- * @param WriterInterface $writer Wrapped writer
+ * @param WriterInterface|string|array|Traversable $writer Wrapped writer or array of configuration options
* @param FilterInterface|int $filterOrPriority Filter or log priority which determines buffering of events
* @param int $bufferSize Maximum buffer size
*/
- public function __construct(WriterInterface $writer, $filterOrPriority = null, $bufferSize = 0)
+ public function __construct($writer, $filterOrPriority = null, $bufferSize = 0)
{
$this->writer = $writer;
+
+ if ($writer instanceof Traversable) {
+ $writer = ArrayUtils::iteratorToArray($writer);
+ }
+
+ if (is_array($writer)) {
+ $filterOrPriority = isset($writer['priority']) ? $writer['priority'] : null;
+ $bufferSize = isset($writer['bufferSize']) ? $writer['bufferSize'] : null;
+ $writer = isset($writer['writer']) ? $writer['writer'] : null;
+ }
if (null === $filterOrPriority) {
$filterOrPriority = new PriorityFilter(Logger::WARN);
} elseif (!$filterOrPriority instanceof FilterInterface) {
$filterOrPriority = new PriorityFilter($filterOrPriority);
}
+ if (is_array($writer) && isset($writer['name'])) {
+ $this->setWriter($writer['name'], $writer['options']);
+ }
+ else {
+ $this->setWriter($writer);
+ }
$this->addFilter($filterOrPriority);
$this->bufferSize = $bufferSize;
}
+
+ /**
+ * Set a new formatter for this writer
+ *
+ * @param string|Formatter\FormatterInterface $formatter
+ * @return self
+ * @throws Exception\InvalidArgumentException
+ */
+ public function setWriter($writer, array $options = null)
+ {
+ if (is_string($writer)) {
+ $writer = $this->writerPlugin($writer, $options);
+ }
+
+ if (!$writer instanceof WriterInterface) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ 'Formatter must implement %s\Formatter\FormatterInterface; received "%s"',
+ __NAMESPACE__,
+ is_object($writer) ? get_class($writer) : gettype($writer)
+ ));
+ }
+
+ $this->writer = $writer;
+ return $this;
+ }
+
+ /**
+ * Get writer plugin manager
+ *
+ * @return WriterPluginManager
+ */
+ public function getWriterPluginManager()
+ {
+ if (null === $this->writerPlugins) {
+ $this->setWriterPluginManager(new WriterPluginManager());
+ }
+ return $this->writerPlugins;
+ }
+
+ /**
+ * Set writer plugin manager
+ *
+ * @param string|WriterPluginManager $plugins
+ * @return Logger
+ * @throws Exception\InvalidArgumentException
+ */
+ public function setWriterPluginManager($plugins)
+ {
+ if (is_string($plugins)) {
+ $plugins = new $plugins;
+ }
+ if (!$plugins instanceof WriterPluginManager) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ 'Writer plugin manager must extend %s\WriterPluginManager; received %s',
+ __NAMESPACE__,
+ is_object($plugins) ? get_class($plugins) : gettype($plugins)
+ ));
+ }
+
+ $this->writerPlugins = $plugins;
+ return $this;
+ }
+
+ /**
+ * Get writer instance
+ *
+ * @param string $name
+ * @param array|null $options
+ * @return Writer\WriterInterface
+ */
+ public function writerPlugin($name, array $options = null)
+ {
+ return $this->getWriterPluginManager()->get($name, $options);
+ }
/**
* Log a message to this writer.
@@ -54,4 +54,20 @@ public function testAfterFlushing()
$this->assertSame(count($wrappedWriter->events), 3);
}
+
+ public function setWriterByName() {
+ $writer = new FingersCrossedWriter('mock');
+ $this->assertAttributeInstanceOf('Zend\Log\Writer\Mock', 'writer', $writer);
+ }
+
+ public function testConstructorOptions() {
+ $options = array('writer' => 'mock', 'priority' => 3);
+ $writer = new FingersCrossedWriter($options);
+ $this->assertAttributeInstanceOf('Zend\Log\Writer\Mock', 'writer', $writer);
+
+ $filters = $this->readAttribute($writer, 'filters');
+ $this->assertCount(1, $filters);
+ $this->assertInstanceOf('Zend\Log\Filter\Priority', $filters[0]);
+ $this->assertAttributeEquals(3, 'priority', $filters[0]);
+ }
}

0 comments on commit a18574a

Please sign in to comment.