forked from aimeos/aimeos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.php
237 lines (186 loc) · 5.35 KB
/
setup.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* @copyright Metaways Infosystems GmbH, 2011
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2015
*/
if( php_sapi_name() != 'cli' ) {
exit( 'Setup can only be started via command line for security reasons' );
}
set_error_handler( function( $severity, $message, $file, $line ) {
throw new ErrorException( $message, 0, $severity, $file, $line );
});
ini_set( 'display_errors', 1 );
date_default_timezone_set( 'UTC' );
/**
* Returns the command options given by the user
*
* @param array &$params List of parameters
* @return array Associative list of option name and value(s)
*/
function getOptions( array &$params )
{
$options = array();
foreach( $params as $key => $option )
{
if( $option === '--help' ) {
usage();
}
if( strncmp( $option, '--', 2 ) === 0 && ( $pos = strpos( $option, '=', 2 ) ) !== false )
{
if( ( $name = substr( $option, 2, $pos - 2 ) ) !== false )
{
if( isset( $options[$name] ) )
{
$options[$name] = (array) $options[$name];
$options[$name][] = substr( $option, $pos + 1 );
}
else
{
$options[$name] = substr( $option, $pos + 1 );
}
unset( $params[$key] );
}
else
{
printf( "Invalid option \"%1\$s\"\n", $option );
usage();
}
}
}
return $options;
}
/**
* Returns a new configuration object
*
* @param array $confPaths List of configuration paths from the bootstrap object
* @param array $options Associative list of configuration options as key/value pairs
* @return \Aimeos\MW\Config\Iface Configuration object
*/
function getConfig( array $confPaths, array $options )
{
$config = array();
if( isset( $options['config'] ) )
{
foreach( (array) $options['config'] as $path )
{
if( is_file( $path ) ) {
$config = array_replace_recursive( $config, require $path );
} else {
$confPaths[] = $path;
}
}
}
$conf = new \Aimeos\MW\Config\PHPArray( $config, $confPaths );
$conf = new \Aimeos\MW\Config\Decorator\Memory( $conf );
if( isset( $options['option'] ) )
{
foreach( (array) $options['option'] as $option )
{
$parts = explode( ':', $option );
if( count( $parts ) !== 2 )
{
printf( "Invalid config option \"%1\$s\"\n", $option );
usage();
}
$conf->set( str_replace( '\\', '/', $parts[0] ), $parts[1] );
}
}
return $conf;
}
/**
* Returns a new context object
*
* @param \Aimeos\MW\Config\Iface $conf Configuration object
* @return \Aimeos\MShop\Context\Item\Iface New context object
*/
function getContext( \Aimeos\MW\Config\Iface $conf )
{
$ctx = new \Aimeos\MShop\Context\Item\Standard();
$ctx->setConfig( $conf );
$dbm = new \Aimeos\MW\DB\Manager\DBAL( $conf );
$ctx->setDatabaseManager( $dbm );
$logger = new \Aimeos\MW\Logger\Errorlog( \Aimeos\MW\Logger\Base::INFO );
$ctx->setLogger( $logger );
$session = new \Aimeos\MW\Session\None();
$ctx->setSession( $session );
$cache = new \Aimeos\MW\Cache\None();
$ctx->setCache( $cache );
$process = new \Aimeos\MW\Process\Pcntl( $conf->get( 'pcntl_max', 4 ), $conf->get( 'pcntl_priority', 19 ) );
$process = new \Aimeos\MW\Process\Decorator\Check( $process );
$ctx->setProcess( $process );
return $ctx;
}
/**
* Returns the fixed and cleaned up database configuration
*
* @param \Aimeos\MW\Config\Iface $conf Configuration object
* @return array Updated database configuration
*/
function getDbConfig( \Aimeos\MW\Config\Iface $conf )
{
$dbconfig = $conf->get( 'resource', array() );
foreach( $dbconfig as $rname => $dbconf )
{
if( strncmp( $rname, 'db', 2 ) !== 0 ) {
unset( $dbconfig[$rname] );
} else {
$conf->set( 'resource/' . $rname . '/limit', 5 );
}
}
return $dbconfig;
}
/**
* Prints the command usage and options, exits the program after printing
*/
function usage()
{
printf( "Usage: php setup.php [--extdir=<path>]* [--config=<path>|<file>]* [--option=key:value]* [--action=migrate|rollback|clean] [--task=<name>] [sitecode] [tplsite]\n" );
exit ( 1 );
}
$exectimeStart = microtime( true );
try
{
$params = $_SERVER['argv'];
array_shift( $params );
$options = getOptions( $params );
if( ( $site = array_shift( $params ) ) === null ) {
$site = 'default';
}
if( ( $tplsite = array_shift( $params ) ) === null ) {
$tplsite = 'default';
}
require 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$aimeos = new \Aimeos\Bootstrap( ( isset( $options['extdir'] ) ? (array) $options['extdir'] : array() ) );
$taskPaths = $aimeos->getSetupPaths( $tplsite );
$conf = getConfig( $aimeos->getConfigPaths(), $options );
$conf->set( 'setup/site', $site );
$dbconfig = getDbConfig( $conf );
$ctx = getContext( $conf );
$dbm = $ctx->getDatabaseManager();
$manager = new \Aimeos\MW\Setup\Manager\Multiple( $dbm, $dbconfig, $taskPaths, $ctx );
$task = ( isset( $options['task'] ) ? $options['task'] : null );
$manager->migrate( $task );
}
catch( Throwable $t )
{
echo "\n\nCaught PHP error while processing setup";
echo "\n\nMessage:\n";
echo $t->getMessage();
echo "\n\nStack trace:\n";
echo $t->getTraceAsString();
echo "\n\n";
exit( 1 );
}
catch( \Exception $e )
{
echo "\n\nCaught exception while processing setup";
echo "\n\nMessage:\n";
echo $e->getMessage();
echo "\n\nStack trace:\n";
echo $e->getTraceAsString();
echo "\n\n";
exit( 1 );
}
$exectimeStop = microtime( true );
printf( "Setup process took %1\$f sec\n\n", ( $exectimeStop - $exectimeStart ) );