Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'weierophinney-hotfix/5254' into develop

* weierophinney-hotfix/5254:
  [zendframework/zendframework#5254] Pre-process configuration to inject DB adapter instance
  • Loading branch information
Ralph Schindler committed Oct 31, 2013
2 parents a65de03 + 4f2a7d8 commit 81c3d1e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
35 changes: 34 additions & 1 deletion src/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 test/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);
}
}

0 comments on commit 81c3d1e

Please sign in to comment.