Skip to content

Commit

Permalink
use adapter name defined in "log" config when convert doctrine to zen…
Browse files Browse the repository at this point in the history
…d db service
  • Loading branch information
samsonasik committed Jul 3, 2017
1 parent 89b9dce commit 129300e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
62 changes: 60 additions & 2 deletions spec/ModuleSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,36 @@

$configListener = Double::instance(['extends' => ConfigListener::class, 'methods' => '__construct']);
allow($moduleEvent)->toReceive('getConfigListener')->andReturn($configListener);
allow($configListener)->toReceive('getMergedConfig')->andReturn([]);
allow($configListener)->toReceive('getMergedConfig')->andReturn([
'log' => [
'ErrorHeroModuleLogger' => [
'writers' => [

[
'name' => 'db',
'options' => [
'db' => 'Zend\Db\Adapter\Adapter',
'table' => 'error_log',
'column' => [
'timestamp' => 'date',
'priority' => 'type',
'message' => 'event',
'extra' => [
'url' => 'url',
'file' => 'file',
'line' => 'line',
'error_type' => 'error_type',
'trace' => 'trace',
'request_data' => 'request_data',
],
],
],
],

],
],
],
]);

$entityManager = Double::instance(['extends' => EntityManager::class, 'methods' => '__construct']);
$connection = Double::instance(['extends' => Connection::class, 'methods' => '__construct']);
Expand Down Expand Up @@ -129,7 +158,36 @@

$configListener = Double::instance(['extends' => ConfigListener::class, 'methods' => '__construct']);
allow($moduleEvent)->toReceive('getConfigListener')->andReturn($configListener);
allow($configListener)->toReceive('getMergedConfig')->andReturn([]);
allow($configListener)->toReceive('getMergedConfig')->andReturn([
'log' => [
'ErrorHeroModuleLogger' => [
'writers' => [

[
'name' => 'db',
'options' => [
'db' => 'Zend\Db\Adapter\Adapter',
'table' => 'error_log',
'column' => [
'timestamp' => 'date',
'priority' => 'type',
'message' => 'event',
'extra' => [
'url' => 'url',
'file' => 'file',
'line' => 'line',
'error_type' => 'error_type',
'trace' => 'trace',
'request_data' => 'request_data',
],
],
],
],

],
],
],
]);

$entityManager = Double::instance(['extends' => EntityManager::class, 'methods' => '__construct']);
$connection = Double::instance(['extends' => Connection::class, 'methods' => '__construct']);
Expand Down
14 changes: 13 additions & 1 deletion src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function convertDoctrineToZendDbService(ModuleEvent $event)
return;
}

/** @var $configListener \Zend\ModuleManager\Listener\ConfigListener */
$configListener = $event->getConfigListener();
$configuration = $configListener->getMergedConfig(false);

Expand All @@ -58,7 +59,17 @@ public function convertDoctrineToZendDbService(ModuleEvent $event)

$allowOverride = $services->getAllowOverride();
$services->setAllowOverride(true);
$services->setService('Zend\Db\Adapter\Adapter', new Adapter($config));

$adapterName = 'Zend\Db\Adapter\Adapter';
$writers = $configuration['log']['ErrorHeroModuleLogger']['writers'];
foreach ($writers as $key => $writer) {
if ($writer['name'] === 'db') {
$adapterName = $writer['options']['db'];
break;
}
}

$services->setService($adapterName, new Adapter($config));
$services->setAllowOverride($allowOverride);
}

Expand All @@ -70,6 +81,7 @@ public function convertDoctrineToZendDbService(ModuleEvent $event)
public function errorPreviewPageHandler(ModuleEvent $event)
{
$services = $event->getParam('ServiceManager');
/** @var $configListener \Zend\ModuleManager\Listener\ConfigListener */
$configListener = $event->getConfigListener();
$configuration = $configListener->getMergedConfig(false);

Expand Down

0 comments on commit 129300e

Please sign in to comment.