From 8fbbf44d3eb91edd5280663e0fe8fee8a1f2cfac Mon Sep 17 00:00:00 2001 From: Robin Cafolla Date: Fri, 4 Oct 2013 17:20:34 +0100 Subject: [PATCH 1/5] Fixed NULL value throwing InvalidArgumentException in Zend\Log\Logger::__construct() --- src/Logger.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Logger.php b/src/Logger.php index e5763d5e..ed94c05c 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -131,7 +131,10 @@ public function __construct($options = null) $options = ArrayUtils::iteratorToArray($options); } - if (is_array($options)) { + if ($options && !is_array($options)) { + throw new Exception\InvalidArgumentException('Options must be an array or an object implementing \Traversable '); + } + else if (is_array($options)) { if (isset($options['writers']) && is_array($options['writers'])) { foreach ($options['writers'] as $writer) { @@ -155,9 +158,6 @@ public function __construct($options = null) } } - else { - throw new Exception\InvalidArgumentException('Options must be an array or an object implementing \Traversable '); - } $this->processors = new SplPriorityQueue(); } From 6283f9732f846a2a3dbe58c3de2ec0d0d22d54a6 Mon Sep 17 00:00:00 2001 From: Robin Cafolla Date: Mon, 7 Oct 2013 09:11:51 +0100 Subject: [PATCH 2/5] Changed conditional to PSR-2 format. Refactored to remove duplicate array check. --- src/Logger.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Logger.php b/src/Logger.php index ed94c05c..779d4e5e 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -131,10 +131,7 @@ public function __construct($options = null) $options = ArrayUtils::iteratorToArray($options); } - if ($options && !is_array($options)) { - throw new Exception\InvalidArgumentException('Options must be an array or an object implementing \Traversable '); - } - else if (is_array($options)) { + if (is_array($options)) { if (isset($options['writers']) && is_array($options['writers'])) { foreach ($options['writers'] as $writer) { @@ -157,7 +154,9 @@ public function __construct($options = null) static::registerErrorHandler($this); } - } + } elseif ($options) { + throw new Exception\InvalidArgumentException('Options must be an array or an object implementing \Traversable '); + } $this->processors = new SplPriorityQueue(); } From b9a27bc62e838152902250815650027f5e65111d Mon Sep 17 00:00:00 2001 From: Robin Cafolla Date: Tue, 8 Oct 2013 21:04:48 +0200 Subject: [PATCH 3/5] Removed trailing spaces with php-cs-fixer --- src/Logger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Logger.php b/src/Logger.php index 779d4e5e..012a6edd 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -156,7 +156,7 @@ public function __construct($options = null) } elseif ($options) { throw new Exception\InvalidArgumentException('Options must be an array or an object implementing \Traversable '); - } + } $this->processors = new SplPriorityQueue(); } From 3184175ce0aa22f2125aa94d745d4f0163974faf Mon Sep 17 00:00:00 2001 From: Maks3w Date: Wed, 16 Oct 2013 19:04:41 +0200 Subject: [PATCH 4/5] [psr-2] Anonymous functions must have the brace at the same line --- src/Logger.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Logger.php b/src/Logger.php index ebd5e6a6..91488819 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -521,8 +521,7 @@ public static function registerErrorHandler(Logger $logger, $continueNativeHandl $errorPriorityMap = static::$errorPriorityMap; - $previous = set_error_handler(function ($level, $message, $file, $line) use ($logger, $errorPriorityMap, $continueNativeHandler) - { + $previous = set_error_handler(function ($level, $message, $file, $line) use ($logger, $errorPriorityMap, $continueNativeHandler) { $iniLevel = error_reporting(); if ($iniLevel & $level) { From 4f2a7d86911fa17c54c55915a46af9d66e9d1154 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 30 Oct 2013 16:28:06 -0500 Subject: [PATCH 5/5] [zendframework/zf2#5254] Pre-process configuration to inject DB adapter instance - Looks for a named DB adapter in the writer configuration, and, if found, retrieves it from the service locator passed to the factory, re-setting the db configuration to use the discovered adapter. --- src/LoggerAbstractServiceFactory.php | 35 ++++++++++++++++- test/LoggerAbstractServiceFactoryTest.php | 47 ++++++++++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/LoggerAbstractServiceFactory.php b/src/LoggerAbstractServiceFactory.php index ccb4c648..e392877b 100644 --- a/src/LoggerAbstractServiceFactory.php +++ b/src/LoggerAbstractServiceFactory.php @@ -56,7 +56,9 @@ public function canCreateServiceWithName(ServiceLocatorInterface $services, $nam public function createServiceWithName(ServiceLocatorInterface $services, $name, $requestedName) { $config = $this->getConfig($services); - return new Logger($config[$requestedName]); + $config = $config[$requestedName]; + $this->processConfig($config, $services); + return new Logger($config); } /** @@ -85,4 +87,35 @@ protected function getConfig(ServiceLocatorInterface $services) $this->config = $config[$this->configKey]; return $this->config; } + + protected function processConfig(&$config, ServiceLocatorInterface $services) + { + if (!isset($config['writers'])) { + return; + } + + foreach ($config['writers'] as $index => $writerConfig) { + if (!isset($writerConfig['name']) + || strtolower($writerConfig['name']) != 'db' + ) { + continue; + } + if (!isset($writerConfig['options']) + || !isset($writerConfig['options']['db']) + ) { + continue; + } + if (!is_string($writerConfig['options']['db'])) { + continue; + } + if (!$services->has($writerConfig['options']['db'])) { + continue; + } + + // Retrieve the DB service from the service locator, and + // inject it into the configuration. + $db = $services->get($writerConfig['options']['db']); + $config['writers'][$index]['options']['db'] = $db; + } + } } diff --git a/test/LoggerAbstractServiceFactoryTest.php b/test/LoggerAbstractServiceFactoryTest.php index 2aadfcf2..4b4a016f 100644 --- a/test/LoggerAbstractServiceFactoryTest.php +++ b/test/LoggerAbstractServiceFactoryTest.php @@ -9,8 +9,9 @@ namespace ZendTest\Log; -use Zend\ServiceManager\ServiceManager; +use Zend\Log\Writer\Db as DbWriter; use Zend\Mvc\Service\ServiceManagerConfig; +use Zend\ServiceManager\ServiceManager; /** * @group Zend_Log @@ -83,4 +84,48 @@ public function testInvalidLoggerService($service) { $actual = $this->serviceManager->get($service); } + + /** + * @group 5254 + */ + public function testRetrievesDatabaseServiceFromServiceManagerWhenEncounteringDbWriter() + { + $db = $this->getMockBuilder('Zend\Db\Adapter\Adapter') + ->disableOriginalConstructor() + ->getMock(); + $serviceManager = new ServiceManager(new ServiceManagerConfig(array( + 'abstract_factories' => array('Zend\Log\LoggerAbstractServiceFactory'), + ))); + $serviceManager->setService('Db\Logger', $db); + $serviceManager->setService('Config', array( + 'log' => array( + 'Application\Log' => array( + 'writers' => array( + array( + 'name' => 'db', + 'priority' => 1, + 'options' => array( + 'separator' => '_', + 'column' => array(), + 'table' => 'applicationlog', + 'db' => 'Db\Logger', + ), + ), + ), + ), + ), + )); + $logger = $serviceManager->get('Application\Log'); + $this->assertInstanceOf('Zend\Log\Logger', $logger); + $writers = $logger->getWriters(); + $found = false; + foreach ($writers as $writer) { + if ($writer instanceof DbWriter) { + $found = true; + break; + } + } + $this->assertTrue($found, 'Did not find expected DB writer'); + $this->assertAttributeSame($db, 'db', $writer); + } }