Skip to content

Commit

Permalink
[zendframework#4455] Allow passing ProcessorPluginManager to logger
Browse files Browse the repository at this point in the history
- Allows passing the ProcessorPluginManager to the Logger constructor
- Adaptes the LoggerAbstractServiceFactory to inject the
  ProcessorPluginManager into the configuration prior to instantiating
  the logger.
  • Loading branch information
weierophinney committed Mar 5, 2014
1 parent 125c9ae commit da24228
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions library/Zend/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ public function __construct($options = null)
$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'])) {
Expand Down
14 changes: 14 additions & 0 deletions library/Zend/Log/LoggerAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ protected function processConfig(&$config, ServiceLocatorInterface $services)
$config['writer_plugin_manager'] = $services->get('Zend\Log\WriterPluginManager');
}

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('Zend\Log\ProcessorPluginManager')
) {
$config['processor_plugin_manager'] = $services->get('Zend\Log\ProcessorPluginManager');
}

if (!isset($config['writers'])) {
return;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/ZendTest/Log/LoggerAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace ZendTest\Log;

use Zend\Log\ProcessorPluginManager;
use Zend\Log\WriterPluginManager;
use Zend\Log\Writer\Db as DbWriter;
use Zend\Mvc\Service\ServiceManagerConfig;
Expand Down Expand Up @@ -154,4 +155,30 @@ public function testWillInjectWriterPluginManagerIfAvailable()
$writer = $logWriters->current();
$this->assertSame($mockWriter, $writer);
}

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('Zend\Log\ProcessorPluginManager', $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);
}
}

0 comments on commit da24228

Please sign in to comment.