forked from speckcommerce/SpeckInstall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
173 lines (152 loc) · 6.95 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?
namespace SpeckInstall;
use Zend\Mvc\MvcEvent;
class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getControllerConfig()
{
return array(
'invokables' => array(
'speck_install' => '\SpeckInstall\Controller\InstallController',
),
);
}
public function getServiceConfig()
{
return array(
'factories' => array(
'SpeckInstall\Mapper\Install' => function ($sm) {
$mapper = new \SpeckInstall\Mapper\Install;
$config = $sm->get('config');
if(isset($config['db'])) {
$mapper->setDbAdapter($sm->get('Zend\Db\Adapter\Adapter'));
}
return $mapper;
},
),
);
}
public function getConfig()
{
return array(
'router' => array(
'routes' => array(
'install' => array(
'type' => 'Literal',
'options' => array(
'route' => '/install',
'defaults' => array(
'controller' => 'speck_install',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'schema' => array(
'type' => 'Literal',
'options' => array(
'route' => '/schema',
'defaults' => array(
'action' => 'schema',
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/view'
),
),
);
}
public function onBootstrap($e)
{
if($e->getRequest() instanceof \Zend\Console\Request){
return;
}
$app = $e->getParam('application');
$em = $app->getEventManager();
$sm = $em->getSharedManager();
$em->attach(MvcEvent::EVENT_DISPATCH, array($this , 'install'), 100);
//install event listener
$sm->attach('SpeckInstall\Controller\InstallController', 'install.create_tables', function ($e) { return $this->createTables($e, 'SpeckCatalog' ); });
$sm->attach('SpeckInstall\Controller\InstallController', 'install.create_tables', function ($e) { return $this->createTables($e, 'SpeckContact' ); });
$sm->attach('SpeckInstall\Controller\InstallController', 'install.create_tables', function ($e) { return $this->createTables($e, 'ZfcUser' ); });
$sm->attach('SpeckInstall\Controller\InstallController', 'install.create_tables', function ($e) { return $this->createTables($e, 'SpeckAddress' ); });
$sm->attach('SpeckInstall\Controller\InstallController', 'install.create_tables', function ($e) { return $this->createTables($e, 'SpeckCart' ); });
$sm->attach('SpeckInstall\Controller\InstallController', 'install.create_tables', function ($e) { return $this->createTables($e, 'SpeckUserAddress' ); });
$sm->attach('SpeckInstall\Controller\InstallController', 'install.create_tables', function ($e) { return $this->createTables($e, 'SpeckRandomProducts' ); });
//$sm->attach('SpeckInstall\Controller\InstallController', 'install.create_tables', function ($e) { return $this->createTables($e, 'SpeckOrder' ); });
$sm->attach('SpeckInstall\Controller\InstallController', 'install.setup_multisite', array($this, 'setupMultiSite'));
$sm->attach('SpeckInstall\Controller\InstallController', 'install.setup_multisite', function ($e) { return $this->createTables($e, 'SpeckMultisite' ); });
$sm->attach('SpeckInstall\Controller\InstallController', 'install.setup_multisite', function ($e) {
$name = $e->getParam('website_name');
$query = "insert into `website`(`website_id`, `name`)VALUES(1, '{$name}')";
$e->getParam('mapper')->query($query);
return array('true', 'SpeckInstall inserted site name - "' . $name . '"');
});
}
public function createTables($e, $moduleName)
{
$mm = $e->getTarget()->getServiceLocator()->get('modulemanager');
$module = $mm->getModule($moduleName);
if(null === $module) {
return array(false, 'Missing module - ' . $moduleName . ' - Did you run composer install?');
}
$reflection = new \ReflectionClass($module);
$path = dirname($reflection->getFileName());
try {
$create = file_get_contents($path .'/data/schema.sql');
if(!$create) { throw new \Exception('cannot find schema file'); }
$mapper = $e->getParam('mapper');
$mapper->query($create);
} catch (\Exception $e) {
return array(false, "SpeckInstaller was unable to complete 'createTables' for {$moduleName} - " . $e->getMessage());
}
return array(true, "SpeckInstaller ran 'createTables' for {$moduleName}");
}
public function setupMultisite($e)
{
$multisite = $e->getParam('multi_site');
$mm = $e->getTarget()->getServiceLocator()->get('modulemanager');
$module = $mm->getModule('SpeckMultisite');
if(null === $module) {
return array(false, 'setup multisite - Missing module - SpeckMultisite - Did you run composer install?');
}
$reflection = new \ReflectionClass($module);
$path = dirname($reflection->getFileName());
try {
$config = include($path .'/config/module.SpeckMultisite.dist.php');
$content = "<?php\nreturn " . var_export($config, 1) . ';';
file_put_contents('config/autoload/multisite.global.php', $content);
} catch (\Exception $e) {
return array(false, "SpeckInstaller was unable to complete 'setupMultisite' for {$moduleName} - " . $e->getMessage());
}
return array(true, "stored SpeckMultisite config");
}
public function createZfcUserTables($e)
{
$this->createTables('ZfcUser');
}
public function install($e)
{
if($e->getRouteMatch()->getParam('controller') === 'speck_install') {
return;
}
$response = $e->getResponse();
$response->setStatusCode(307)->getHeaders()->addHeaderLine('Location', '/install');
}
}