-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
134 lines (120 loc) · 4.27 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace YcheukfMigration;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\ConsoleUsageProviderInterface;
use Zend\Console\Adapter\AdapterInterface as Console;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Stdlib\Parameters;
use Zend\EventManager\StaticEventManager;
use Zend\Db\Adapter\Adapter;
class Module implements
AutoloaderProviderInterface,
ConfigProviderInterface,
ConsoleUsageProviderInterface,
ServiceProviderInterface,
BootstrapListenerInterface
{
/**
* @param EventInterface|\Zend\Mvc\MvcEvent $e
* @return array|void
*/
public function onBootstrap(EventInterface $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
/**
* attach an event to setDbConfigsFromEvent.
add this code to other module if you need to add some dynamic db config.
*/
$events = StaticEventManager::getInstance();
$events->attach("YcheukfMigration\Controller\MigrateController", array('setDbConfigsFromEvent'), function($e) {
// var_dump($e->getParams());
$aReturn = array();
$aReturn['db'] = array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2test2;host=localhost',
'username' => 'root',
'password' => 'root',
'driver_options' => array(
'SET NAMES \'UTF8\''
)
);
$aReturn['db2'] = array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2test3;host=localhost',
'username' => 'root',
'password' => 'root',
'driver_options' => array(
'SET NAMES \'UTF8\''
)
);
return ($aReturn);
});
// */
}
public function init(){
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/migration',
),
),
);
}
public function getServiceConfig()
{
return array(
'factories' => array(
'YcheukfMigration\Model\MigrationVersionTable' => function (ServiceLocatorInterface $serviceLocator) {
/** @var $tableGateway TableGateway */
$tableGateway = $serviceLocator->get('YcheukfMigrationVersionTableGateway');
$table = new Model\MigrationVersionTable($tableGateway);
return $table;
},
'YcheukfMigrationVersionTableGateway' => function (ServiceLocatorInterface $serviceLocator) {
/** @var $dbAdapter \Zend\Db\Adapter\Adapter */
$dbAdapter = $serviceLocator->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\MigrationVersion());
return new TableGateway(Library\Migration::MIGRATION_TABLE, $dbAdapter, null, $resultSetPrototype);
},
),
);
}
public function getConsoleUsage(Console $console)
{
return array(
'migration [--all]' ,
' List available migrations',
'migration generate [--dir=]',
' Generate new migration ',
'migration up [<dbkey>] [--dir=] [--dbsfromevent]',
' apply up-grade all migration. ',
'migration down [<dbkey>] [--dir=] [--dbsfromevent]',
' apply down-grade all migration',
' ',
array('--all', 'show all applied migrations'),
array('<dbkey>', 'the key of db-config-array. mostly is "db"'),
array('--dir', 'migration dir. default is "default"'),
array(
'--dbsfromevent',
'set db-config-array by event "setDbConfigsFromEvent"'
),
);
}
}