Permalink
Browse files

Added FormatterPluginManager to load Formatters by name

Changed writers according to that
  • Loading branch information...
1 parent 8642b2b commit ed981ec037c55b1c1f78492402e58c99d2de32cf @stefankleff committed Oct 9, 2012
@@ -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;
}
@@ -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');
}
/**
@@ -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__
+ ));
+ }
+}
@@ -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');
}
@@ -22,18 +22,18 @@
/**
* Add a log filter to the writer
*
- * @param int|Filter $filter
+ * @param int|string|Filter $filter
* @return WriterInterface
*/
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
@@ -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()
{

0 comments on commit ed981ec

Please sign in to comment.