Skip to content

Commit e06764f

Browse files
committed
DoctrineORMModule support
1 parent 4239037 commit e06764f

File tree

4 files changed

+174
-4
lines changed

4 files changed

+174
-4
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ CREATE TABLE `log` (
4545
```
4646
> If you use other RDBMS, you may follow the `log` table structure above.
4747
48-
**2. Setup your Zend\Db\Adapter\Adapter service**
48+
**2. Setup your Zend\Db\Adapter\Adapter service or your DoctrineORMModule Config**
4949

50-
This module uses `Zend\Log\Writer\Db` so, we need `Zend\Db\Adapter\Adapter` service.
50+
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`.
5151

5252
```php
5353
// config/autoload/local.php
@@ -63,6 +63,29 @@ return [
6363
],
6464
];
6565
```
66+
67+
OR
68+
69+
```php
70+
// config/autoload/local.php
71+
return [
72+
'doctrine' => [
73+
'connection' => [
74+
'orm_default' => [
75+
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
76+
'params' => [
77+
'user' => 'root',
78+
'password' => '',
79+
'dbname' => 'mysqldbname',
80+
'host' => 'localhost',
81+
'port' => '3306',
82+
],
83+
],
84+
],
85+
]
86+
];
87+
```
88+
6689
> If you use other RDBMS, you may configure your own `db` config.
6790
6891
**3. Require this module uses [composer](https://getcomposer.org/).**

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
"zendframework/zend-json": "^2.5|^3.0",
2222
"zendframework/zend-log": "^2.5",
2323
"zendframework/zend-mail": "^2.5",
24-
"zendframework/zend-mvc": "^2.5|^3.0",
2524
"zendframework/zend-servicemanager": "^2.5|^3.0",
2625
"zendframework/zend-text": "^2.5"
2726
},
2827
"require-dev": {
28+
"doctrine/doctrine-orm-module": "^1.1",
2929
"kahlan/kahlan": "^3.0.0",
30-
"satooshi/php-coveralls": "^1.0"
30+
"satooshi/php-coveralls": "^1.0",
31+
"zendframework/zend-mvc": "^2.5|^3.0"
3132
},
3233
"autoload": {
3334
"psr-4": {

spec/ModuleSpec.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33
namespace ErrorHeroModule\Spec;
44

5+
use Doctrine\DBAL\Connection;
6+
use Doctrine\DBAL\Driver\PDOMySql\Driver;
7+
use Doctrine\ORM\EntityManager;
58
use ErrorHeroModule\Listener\Mvc;
69
use ErrorHeroModule\Module;
710
use Kahlan\Plugin\Double;
811
use Zend\EventManager\EventManagerInterface;
912
use Zend\Mvc\Application;
1013
use Zend\Mvc\MvcEvent;
1114
use Zend\ServiceManager\ServiceLocatorInterface;
15+
use Zend\ModuleManager\ModuleEvent;
16+
use Zend\ModuleManager\ModuleManager;
17+
use Zend\ModuleManager\Listener\ConfigListener;
1218

1319
describe('Module', function () {
1420

@@ -29,6 +35,94 @@
2935

3036
});
3137

38+
describe('->init()', function () {
39+
40+
it('receive ModuleManager that get Eventmanager that attach ModuleEvent::EVENT_LOAD_MODULES_POST', function () {
41+
42+
$moduleManager = Double::instance(['extends' => ModuleManager::class, 'methods' => '__construct']);
43+
$eventManager = Double::instance(['implements' => EventManagerInterface::class]);
44+
45+
allow($moduleManager)->toReceive('getEventManager')->andReturn($eventManager);
46+
expect($eventManager)->toReceive('attach')->with(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this->module, 'convertDoctrineToZendDbConfig']);
47+
48+
$this->module->init($moduleManager);
49+
50+
});
51+
52+
});
53+
54+
describe('->convertDoctrineToZendDbConfig()', function () {
55+
56+
it('does not has EntityManager service', function () {
57+
58+
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
59+
$serviceManager = Double::instance(['implements' => ServiceLocatorInterface::class]);
60+
allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager);
61+
allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(false);
62+
63+
$this->module->convertDoctrineToZendDbConfig($moduleEvent);
64+
expect($moduleEvent)->not->toReceive('getConfigListener');
65+
66+
});
67+
68+
it('has EntityManager service but already has db config', function () {
69+
70+
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
71+
$serviceManager = Double::instance(['implements' => ServiceLocatorInterface::class]);
72+
allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager);
73+
allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(true);
74+
75+
$configListener = Double::instance(['extends' => ConfigListener::class, 'methods' => '__construct']);
76+
allow($moduleEvent)->toReceive('getConfigListener')->andReturn($configListener);
77+
allow($configListener)->toReceive('getMergedConfig')->andReturn([
78+
'db' => [
79+
'username' => 'root',
80+
'password' => '',
81+
'driver' => 'pdo_mysql',
82+
'database' => 'mydb',
83+
'host' => 'localhost',
84+
],
85+
]);
86+
87+
$this->module->convertDoctrineToZendDbConfig($moduleEvent);
88+
expect($serviceManager)->not->toReceive('get')->with(EntityManager::class);
89+
90+
});
91+
92+
it('has EntityManager service but already does not has db config', function () {
93+
94+
$moduleEvent = Double::instance(['extends' => ModuleEvent::class, 'methods' => '__construct']);
95+
$serviceManager = Double::instance(['implements' => ServiceLocatorInterface::class]);
96+
allow($moduleEvent)->toReceive('getParam')->with('ServiceManager')->andReturn($serviceManager);
97+
allow($serviceManager)->toReceive('has')->with(EntityManager::class)->andReturn(true);
98+
99+
$configListener = Double::instance(['extends' => ConfigListener::class, 'methods' => '__construct']);
100+
allow($moduleEvent)->toReceive('getConfigListener')->andReturn($configListener);
101+
allow($configListener)->toReceive('getMergedConfig')->andReturn([]);
102+
103+
$entityManager = Double::instance(['extends' => EntityManager::class, 'methods' => '__construct']);
104+
$connection = Double::instance(['extends' => Connection::class, 'methods' => '__construct']);
105+
106+
$driver = Double::instance(['extends' => Driver::class, 'methods' => '__construct']);
107+
allow($driver)->toReceive('getName')->andReturn('pdo_mysql');
108+
109+
allow($connection)->toReceive('getUsername')->andReturn('root');
110+
allow($connection)->toReceive('getPassword')->andReturn('');
111+
allow($connection)->toReceive('getDriver')->andReturn($driver);
112+
allow($connection)->toReceive('getDatabase')->andReturn('mydb');
113+
allow($connection)->toReceive('getHost')->andReturn('localhost');
114+
allow($connection)->toReceive('getPort')->andReturn('3306');
115+
116+
allow($entityManager)->toReceive('getConnection')->andReturn($connection);
117+
allow($serviceManager)->toReceive('get')->with(EntityManager::class)->andReturn($entityManager);
118+
119+
$this->module->convertDoctrineToZendDbConfig($moduleEvent);
120+
expect($serviceManager)->toReceive('get')->with(EntityManager::class);
121+
122+
});
123+
124+
});
125+
32126
describe('->onBootstrap()', function () {
33127

34128
it('pull Mvc Listener and use it to attach with EventManager', function () {

src/Module.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,62 @@
22

33
namespace ErrorHeroModule;
44

5+
use Doctrine\ORM\EntityManager;
56
use Zend\Mvc\MvcEvent;
7+
use Zend\ModuleManager\ModuleEvent;
8+
use Zend\ModuleManager\ModuleManager;
69

710
class Module
811
{
12+
/**
13+
* @param ModuleManager $moduleManager
14+
* @return void
15+
*/
16+
public function init(ModuleManager $moduleManager)
17+
{
18+
$eventManager = $moduleManager->getEventManager();
19+
$eventManager->attach(ModuleEvent::EVENT_LOAD_MODULES_POST, [$this, 'convertDoctrineToZendDbConfig']);
20+
}
21+
22+
/**
23+
* @param ModuleEvent $event
24+
* @return void
25+
*/
26+
public function convertDoctrineToZendDbConfig(ModuleEvent $event)
27+
{
28+
$services = $event->getParam('ServiceManager');
29+
if (! $services->has(EntityManager::class)) {
30+
return;
31+
}
32+
33+
$configListener = $event->getConfigListener();
34+
$configuration = $configListener->getMergedConfig(false);
35+
36+
if (isset($configuration['db'])) {
37+
return;
38+
}
39+
40+
$entityManager = $services->get(EntityManager::class);
41+
$doctrineDBALConnection = $entityManager->getConnection();
42+
43+
$configuration['db'] = [
44+
'username' => $doctrineDBALConnection->getUsername(),
45+
'password' => $doctrineDBALConnection->getPassword(),
46+
'driver' => $doctrineDBALConnection->getDriver()->getName(),
47+
'database' => $doctrineDBALConnection->getDatabase(),
48+
'host' => $doctrineDBALConnection->getHost(),
49+
'port' => $doctrineDBALConnection->getPort()
50+
];
51+
52+
$configListener->setMergedConfig($configuration);
53+
$event->setConfigListener($configListener);
54+
55+
$allowOverride = $services->getAllowOverride();
56+
$services->setAllowOverride(true);
57+
$services->setService('config', $configuration);
58+
$services->setAllowOverride($allowOverride);
59+
}
60+
961
public function onBootstrap(MvcEvent $e)
1062
{
1163
$app = $e->getApplication();

0 commit comments

Comments
 (0)