Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added FormatterPluginManager to load Formatters by name

Changed writers according to that
  • Loading branch information...
commit ed981ec037c55b1c1f78492402e58c99d2de32cf 1 parent 8642b2b
@stefankleff authored
View
87 library/Zend/Log/Writer/AbstractWriter.php
@@ -12,7 +12,7 @@
use Zend\Log\Exception;
use Zend\Log\Filter;
-use Zend\Log\Formatter\FormatterInterface as Formatter;
+use Zend\Log\Formatter;
use Zend\Stdlib\ErrorHandler;
/**
@@ -28,6 +28,13 @@
* @var FilterPluginManager
*/
protected $filterPlugins;
+
+ /**
+ * Formatter plugins
+ *
+ * @var FormatterPluginManager
+ */
+ protected $formatterPlugins;
/**
* Filter chain
@@ -77,7 +84,8 @@ public function addFilter($filter, array $options = null)
if (!$filter instanceof Filter\FilterInterface) {
throw new Exception\InvalidArgumentException(sprintf(
- 'Writer must implement Zend\Log\Filter\FilterInterface; received "%s"',
+ 'Writer must implement %s\Filter\FilterInterface; received "%s"',
+ __NAMESPACE__,
is_object($filter) ? get_class($filter) : gettype($filter)
));
}
@@ -134,6 +142,56 @@ public function filterPlugin($name, array $options = null)
{
return $this->getFilterPluginManager()->get($name, $options);
}
+
+ /**
+ * Get formatter plugin manager
+ *
+ * @return FormatterPluginManager
+ */
+ public function getFormatterPluginManager()
+ {
+ if (null === $this->formatterPlugins) {
+ $this->setFormatterPluginManager(new FormatterPluginManager());
+ }
+ return $this->formatterPlugins;
+ }
+
+ /**
+ * Set formatter plugin manager
+ *
+ * @param string|FormatterPluginManager $plugins
+ * @return self
+ * @throws Exception\InvalidArgumentException
+ */
+ public function setFormatterPluginManager($plugins)
+ {
+ if (is_string($plugins)) {
+ $plugins = new $plugins;
+ }
+ if (!$plugins instanceof FormatterPluginManager) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ 'Writer plugin manager must extend %s\FormatterPluginManager; received %s',
+ __NAMESPACE__,
+ is_object($plugins) ? get_class($plugins) : gettype($plugins)
+ ));
+ }
+
+ $this->formatterPlugins = $plugins;
+ return $this;
+ }
+
+
+ /**
+ * Get formatter instance
+ *
+ * @param string $name
+ * @param array|null $options
+ * @return Formatter\FormatterInterface
+ */
+ public function formatterPlugin($name, array $options = null)
+ {
+ return $this->getFormatterPluginManager()->get($name, $options);
+ }
/**
* Log a message to this writer.
@@ -178,12 +236,31 @@ public function write(array $event)
/**
* Set a new formatter for this writer
*
- * @param Formatter $formatter
+ * @param string|Formatter\FormatterInterface $formatter
* @return self
+ * @throws Exception\InvalidArgumentException
*/
- public function setFormatter(Formatter $formatter)
+ public function setFormatter($formatter, array $options = null)
{
- $this->formatter = $formatter;
+ if (is_string($formatter)) {
+ $formatter = $this->formatterPlugin($formatter, $options);
+ }
+
+ if (!$formatter instanceof Formatter\FormatterInterface) {
+ // This should be used instead of triggering an error, but this will require to change tests
+ //throw new Exception\InvalidArgumentException(sprintf(
+ // 'Formatter must implement %s\Formatter\FormatterInterface; received "%s"',
+ // __NAMESPACE__,
+ // is_object($formatter) ? get_class($formatter) : gettype($formatter)
+ //));
+ trigger_error(sprintf(
+ 'Formatter must implement %s\Formatter\FormatterInterface; received "%s"',
+ __NAMESPACE__,
+ is_object($formatter) ? get_class($formatter) : gettype($formatter)
+ ));
+ }
+
+ $this->formatter= $formatter;
return $this;
}
View
6 library/Zend/Log/Writer/FingersCrossed.php
@@ -148,13 +148,13 @@ public function reset()
* Stub in accordance to parent method signature.
* Fomatters must be set on the wrapped writer.
*
- * @param Zend\Log\Formatter\FormatterInterface $formatter
+ * @param string|Formatter\FormatterInterface $formatter
* @return void
* @throws Zend\Log\Exception\InvalidArgumentException
*/
- public function setFormatter(FormatterInterface $formatter)
+ public function setFormatter($formatter)
{
- throw new InvalidArgumentException(get_class() . ' does not support formatting');
+ throw new Exception\InvalidArgumentException(get_class() . ' does not support formatting');
}
/**
View
66 library/Zend/Log/Writer/FormatterPluginManager.php
@@ -0,0 +1,66 @@
+<?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 Zend\Log\Writer;
+
+use Zend\ServiceManager\AbstractPluginManager;
+use Zend\Log\Formatter;
+use Zend\Log\Exception;
+
+/**
+ * @category Zend
+ * @package Zend_Log
+ */
+class FormatterPluginManager extends AbstractPluginManager
+{
+ /**
+ * Default set of formatters
+ *
+ * @var array
+ */
+ protected $invokableClasses = array(
+ 'base' => 'Zend\Log\Formatter\Base',
+ 'simple' => 'Zend\Log\Formatter\Simple',
+ 'xml' => 'Zend\Log\Formatter\Xml',
+ 'db' => 'Zend\Log\Formatter\Db',
+ 'errorhandler' => 'Zend\Log\Formatter\ErrorHandler',
+ 'exceptionhandler' => 'Zend\Log\Formatter\ExceptionHandler',
+ );
+
+ /**
+ * Allow many filters of the same type
+ *
+ * @var bool
+ */
+ protected $shareByDefault = false;
+
+ /**
+ * Validate the plugin
+ *
+ * Checks that the formatter loaded is an instance of Formatter\FormatterInterface.
+ *
+ * @param mixed $plugin
+ * @return void
+ * @throws Exception\InvalidArgumentException if invalid
+ */
+ public function validatePlugin($plugin)
+ {
+ if ($plugin instanceof Formatter\FormatterInterface) {
+ // we're okay
+ return;
+ }
+
+ throw new Exception\InvalidArgumentException(sprintf(
+ 'Plugin of type %s is invalid; must implement %s\Formatter\FormatterInterface',
+ (is_object($plugin) ? get_class($plugin) : gettype($plugin)),
+ __NAMESPACE__
+ ));
+ }
+}
View
4 library/Zend/Log/Writer/MongoDB.php
@@ -89,11 +89,11 @@ public function __construct($mongo, $database, $collection, array $saveOptions =
/**
* This writer does not support formatting.
*
- * @param Zend\Log\Formatter\FormatterInterface $formatter
+ * @param string|Zend\Log\Formatter\FormatterInterface $formatter
* @return void
* @throws Zend\Log\Exception\InvalidArgumentException
*/
- public function setFormatter(FormatterInterface $formatter)
+ public function setFormatter($formatter)
{
throw new InvalidArgumentException(get_class() . ' does not support formatting');
}
View
6 library/Zend/Log/Writer/WriterInterface.php
@@ -22,7 +22,7 @@
/**
* Add a log filter to the writer
*
- * @param int|Filter $filter
+ * @param int|string|Filter $filter
* @return WriterInterface
*/
public function addFilter($filter);
@@ -30,10 +30,10 @@ public function addFilter($filter);
/**
* Set a message formatter for the writer
*
- * @param Formatter $formatter
+ * @param string|Formatter $formatter
* @return WriterInterface
*/
- public function setFormatter(Formatter $formatter);
+ public function setFormatter($formatter);
/**
* Write a log message
View
5 tests/ZendTest/Log/Writer/AbstractTest.php
@@ -39,6 +39,11 @@ public function testSetFormatter()
$this->setExpectedException('PHPUnit_Framework_Error');
$this->_writer->setFormatter(new \StdClass());
}
+
+ public function testSetSimpleFormatterByName()
+ {
+ $this->_writer->setFormatter('simple');
+ }
public function testAddFilter()
{
Please sign in to comment.
Something went wrong with that request. Please try again.