diff --git a/src/Logger.php b/src/Logger.php index 546d86eb..9feca3d5 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -12,6 +12,7 @@ use DateTime; use ErrorException; use Traversable; +use Zend\ServiceManager\AbstractPluginManager; use Zend\Stdlib\ArrayUtils; use Zend\Stdlib\SplPriorityQueue; @@ -147,6 +148,20 @@ public function __construct($options = null) throw new Exception\InvalidArgumentException('Options must be an array or an object implementing \Traversable '); } + // Inject writer plugin manager, if available + if (isset($options['writer_plugin_manager']) + && $options['writer_plugin_manager'] instanceof AbstractPluginManager + ) { + $this->setWriterPluginManager($options['writer_plugin_manager']); + } + + // Inject processor plugin manager, if available + if (isset($options['processor_plugin_manager']) + && $options['processor_plugin_manager'] instanceof AbstractPluginManager + ) { + $this->setProcessorPluginManager($options['processor_plugin_manager']); + } + if (isset($options['writers']) && is_array($options['writers'])) { foreach ($options['writers'] as $writer) { if (!isset($writer['name'])) { diff --git a/src/LoggerAbstractServiceFactory.php b/src/LoggerAbstractServiceFactory.php index ce2eea3b..b4611e15 100644 --- a/src/LoggerAbstractServiceFactory.php +++ b/src/LoggerAbstractServiceFactory.php @@ -10,6 +10,7 @@ namespace Zend\Log; use Zend\ServiceManager\AbstractFactoryInterface; +use Zend\ServiceManager\AbstractPluginManager; use Zend\ServiceManager\ServiceLocatorInterface; /** @@ -90,6 +91,34 @@ protected function getConfig(ServiceLocatorInterface $services) protected function processConfig(&$config, ServiceLocatorInterface $services) { + if (isset($config['writer_plugin_manager']) + && is_string($config['writer_plugin_manager']) + && $services->has($config['writer_plugin_manager']) + ) { + $config['writer_plugin_manager'] = $services->get($config['writer_plugin_manager']); + } + + if ((!isset($config['writer_plugin_manager']) + || ! $config['writer_plugin_manager'] instanceof AbstractPluginManager) + && $services->has('LogWriterManager') + ) { + $config['writer_plugin_manager'] = $services->get('LogWriterManager'); + } + + if (isset($config['processor_plugin_manager']) + && is_string($config['processor_plugin_manager']) + && $services->has($config['processor_plugin_manager']) + ) { + $config['processor_plugin_manager'] = $services->get($config['processor_plugin_manager']); + } + + if ((!isset($config['processor_plugin_manager']) + || ! $config['processor_plugin_manager'] instanceof AbstractPluginManager) + && $services->has('LogProcessorManager') + ) { + $config['processor_plugin_manager'] = $services->get('LogProcessorManager'); + } + if (!isset($config['writers'])) { return; } diff --git a/test/LoggerAbstractServiceFactoryTest.php b/test/LoggerAbstractServiceFactoryTest.php index 4ae27340..6c96da00 100644 --- a/test/LoggerAbstractServiceFactoryTest.php +++ b/test/LoggerAbstractServiceFactoryTest.php @@ -9,6 +9,8 @@ namespace ZendTest\Log; +use Zend\Log\ProcessorPluginManager; +use Zend\Log\WriterPluginManager; use Zend\Log\Writer\Db as DbWriter; use Zend\Mvc\Service\ServiceManagerConfig; use Zend\ServiceManager\ServiceManager; @@ -128,4 +130,61 @@ public function testRetrievesDatabaseServiceFromServiceManagerWhenEncounteringDb $this->assertTrue($found, 'Did not find expected DB writer'); $this->assertAttributeSame($db, 'db', $writer); } + + /** + * @group 4455 + */ + public function testWillInjectWriterPluginManagerIfAvailable() + { + $writers = new WriterPluginManager(); + $mockWriter = $this->getMock('Zend\Log\Writer\WriterInterface'); + $writers->setService('CustomWriter', $mockWriter); + + $services = new ServiceManager(new ServiceManagerConfig(array( + 'abstract_factories' => array('Zend\Log\LoggerAbstractServiceFactory'), + ))); + $services->setService('LogWriterManager', $writers); + $services->setService('Config', array( + 'log' => array( + 'Application\Frontend' => array( + 'writers' => array(array('name' => 'CustomWriter')), + ), + ), + )); + + $log = $services->get('Application\Frontend'); + $logWriters = $log->getWriters(); + $this->assertEquals(1, count($logWriters)); + $writer = $logWriters->current(); + $this->assertSame($mockWriter, $writer); + } + + /** + * @group 4455 + */ + public function testWillInjectProcessorPluginManagerIfAvailable() + { + $processors = new ProcessorPluginManager(); + $mockProcessor = $this->getMock('Zend\Log\Processor\ProcessorInterface'); + $processors->setService('CustomProcessor', $mockProcessor); + + $services = new ServiceManager(new ServiceManagerConfig(array( + 'abstract_factories' => array('Zend\Log\LoggerAbstractServiceFactory'), + ))); + $services->setService('LogProcessorManager', $processors); + $services->setService('Config', array( + 'log' => array( + 'Application\Frontend' => array( + 'writers' => array(array('name' => 'Null')), + 'processors' => array(array('name' => 'CustomProcessor')), + ), + ), + )); + + $log = $services->get('Application\Frontend'); + $logProcessors = $log->getProcessors(); + $this->assertEquals(1, count($logProcessors)); + $processor = $logProcessors->current(); + $this->assertSame($mockProcessor, $processor); + } }