Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion library/Zend/Log/LoggerAbstractServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
}
}
47 changes: 46 additions & 1 deletion tests/ZendTest/Log/LoggerAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}