Skip to content

Commit

Permalink
refactor Doctrine transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed May 1, 2018
1 parent 9bdae95 commit edca996
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -56,7 +56,7 @@ CREATE TABLE `log` (
**2. Setup your Zend\Db\Adapter\Adapter service or your Doctrine\ORM\EntityManager service config**

You can use 'db' (with _Zend\Db_) config or 'doctrine' (with _DoctrineORMModule_) config that will be converted to be usable with `Zend\Log\Writer\Db`.
You can use 'db' (with _Zend\Db_) config or 'doctrine' (with _DoctrineORMModule_) config that will be transformed to be usable with `Zend\Log\Writer\Db`.

```php
<?php
Expand Down
Expand Up @@ -2,14 +2,12 @@

namespace ErrorHeroModule\Spec;

use Doctrine\ORM\EntityManager;
use ErrorHeroModule;
use ErrorHeroModule\Controller\ErrorPreviewConsoleController;
use Kahlan\Plugin\Quit;
use Kahlan\QuitException;
use Zend\Console\Console;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Mvc\Application;

describe('Integration via ErrorPreviewConsoleController with doctrine', function () {
Expand Down Expand Up @@ -38,9 +36,9 @@
$serviceManager->get('SendResponseListener')
->detach($events);

$db = $serviceManager->get(Adapter::class);
$tableGateway = new TableGateway('log', $db, null, new ResultSet());
$tableGateway->delete([]);
$entityManager = $serviceManager->get(EntityManager::class);
$stmt = $entityManager->getConnection()->prepare('DELETE FROM log');
$stmt->execute();

return $application;

Expand Down
12 changes: 6 additions & 6 deletions spec/ModuleSpec.php
Expand Up @@ -41,7 +41,7 @@
$eventManager = Double::instance(['implements' => EventManagerInterface::class]);

allow($moduleManager)->toReceive('getEventManager')->andReturn($eventManager);
expect($eventManager)->toReceive('attach')->with(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this->module, 'convertDoctrineToZendDbService']);
expect($eventManager)->toReceive('attach')->with(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this->module, 'doctrineTransform']);
expect($eventManager)->toReceive('attach')->with(ModuleEvent::EVENT_MERGE_CONFIG, [$this->module, 'errorPreviewPageHandler'], 101);

$this->module->init($moduleManager);
Expand All @@ -50,7 +50,7 @@

});

describe('->convertDoctrineToZendDbService()', function () {
describe('->doctrineTransform()', function () {

it('does not has EntityManager service', function () {

Expand All @@ -59,7 +59,7 @@
allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager);
allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(false);

$this->module->convertDoctrineToZendDbService($moduleEvent);
$this->module->doctrineTransform($moduleEvent);
expect($moduleEvent)->not->toReceive('getConfigListener');

});
Expand All @@ -80,7 +80,7 @@
],
]);

$this->module->convertDoctrineToZendDbService($moduleEvent);
$this->module->doctrineTransform($moduleEvent);
expect($serviceManager)->not->toReceive('get')->with(EntityManager::class);

});
Expand Down Expand Up @@ -139,7 +139,7 @@
allow($entityManager)->toReceive('getConnection')->andReturn($connection);
allow($serviceManager)->toReceive('get')->with(EntityManager::class)->andReturn($entityManager);

$this->module->convertDoctrineToZendDbService($moduleEvent);
$this->module->doctrineTransform($moduleEvent);
expect($serviceManager)->toReceive('get')->with(EntityManager::class);

});
Expand Down Expand Up @@ -202,7 +202,7 @@
allow($entityManager)->toReceive('getConnection')->andReturn($connection);
allow($serviceManager)->toReceive('get')->with(EntityManager::class)->andReturn($entityManager);

$this->module->convertDoctrineToZendDbService($moduleEvent);
$this->module->doctrineTransform($moduleEvent);
expect($serviceManager)->toReceive('get')->with(EntityManager::class);

});
Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/ExpressiveFactory.php
Expand Up @@ -6,7 +6,7 @@

use Doctrine\ORM\EntityManager;
use ErrorHeroModule\Handler\Logging;
use ErrorHeroModule\Transformer\DoctrineToZendDb;
use ErrorHeroModule\Transformer\Doctrine;
use ErrorHeroModule\Transformer\SymfonyService;
use Psr\Container\ContainerInterface;
use RuntimeException;
Expand All @@ -20,7 +20,7 @@ public function __invoke(ContainerInterface $container) : Expressive
$configuration = $container->get('config');

if ($container->has(EntityManager::class) && ! isset($configuration['db'])) {
$container = DoctrineToZendDb::transform($container, $configuration);
$container = Doctrine::transform($container, $configuration);
}

if ($container instanceof SymfonyContainerBuilder) {
Expand Down
6 changes: 3 additions & 3 deletions src/Module.php
Expand Up @@ -13,19 +13,19 @@ class Module
public function init(ModuleManager $moduleManager) : void
{
$eventManager = $moduleManager->getEventManager();
$eventManager->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'convertDoctrineToZendDbService']);
$eventManager->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'doctrineTransform']);
$eventManager->attach(ModuleEvent::EVENT_MERGE_CONFIG, [$this, 'errorPreviewPageHandler'], 101);
}

public function convertDoctrineToZendDbService(ModuleEvent $event) : void
public function doctrineTransform(ModuleEvent $event) : void
{
$container = $event->getParam('ServiceManager');
if (! $container->has(EntityManager::class)) {
return;
}

$configuration = $container->get('config');
$configuration['db'] ?? Transformer\DoctrineToZendDb::transform($container, $configuration);
$configuration['db'] ?? Transformer\Doctrine::transform($container, $configuration);
}

public function errorPreviewPageHandler(ModuleEvent $event) : void
Expand Down
Expand Up @@ -7,9 +7,10 @@
use Doctrine\ORM\EntityManager;
use Psr\Container\ContainerInterface;
use Zend\Db\Adapter\Adapter;
use Zend\Log\Logger;
use Zend\ServiceManager\ServiceManager;

class DoctrineToZendDb implements TransformerInterface
class Doctrine implements TransformerInterface
{
public static function transform(ContainerInterface $container, array $configuration) : ContainerInterface
{
Expand All @@ -30,19 +31,15 @@ public static function transform(ContainerInterface $container, array $configura
'driver_options' => $driverOptions,
];

$adapterName = Adapter::class;
$writers = $configuration['log']['ErrorHeroModuleLogger']['writers'];
foreach ($writers as $key => $writer) {
if ($writer['name'] === 'db') {
$adapterName = $writer['options']['db'];
$writers[$key]['options']['db'] = new Adapter($config);
break;
}
}

$allowOverride = $container->getAllowOverride();
$container->setAllowOverride(true);
$container->setService($adapterName, new Adapter($config));
$container->setAllowOverride($allowOverride);
$container->setService('ErrorHeroModuleLogger', new Logger(['writers' => $writers]));
}

return $container;
Expand Down

0 comments on commit edca996

Please sign in to comment.