-
Notifications
You must be signed in to change notification settings - Fork 118
/
up.php
174 lines (142 loc) · 4.03 KB
/
up.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
<?php
/**
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2015-2024
*/
require 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
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 ) {
if( $severity & E_DEPRECATED === 0 ) {
throw new ErrorException( $message, 0, $severity, $file, $line );
}
error_log( $message . ' in ' . $file . ' on line ' . $line );
});
ini_set( 'display_errors', 1 );
date_default_timezone_set( 'UTC' );
/**
* Returns the configuration based on the given arguments
*
* @param array $options List of key/value string separated by colon (e.g. "key:value")
* @return array Associative list of key value pairs
*/
function config( array $options ) : array
{
$config = [];
foreach( $options as $option )
{
list( $key, $val ) = explode( ':', $option );
$config[$key] = $val;
}
return $config;
}
/**
* 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 options( array &$params )
{
$options = [];
foreach( $params as $key => $option )
{
if( $option === '--help' ) {
usage();
}
if( !strncmp( $option, '--', 2 ) && ( $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();
}
}
elseif( $option[0] === '-' )
{
$options[$option[1]] = substr( $option, 1 );
unset( $params[$key] );
}
}
return $options;
}
/**
* Returns the verbosity level based on the given arguments
*
* @param array $options Associative list of key value pairs
* @return string Verbosity level ("v", "vv", "vvv" or empty string)
*/
function verbose( array $options ) : string
{
return isset( $options['q'] ) ? '' : ( $options['v'] ?? 'vv' );
}
/**
* Prints the command usage and options, exits the program after printing
*/
function usage()
{
printf( "Usage: php up.php [OPTION]* [sitecode] [tplsite]\n" );
printf( " -q Quiet\n" );
printf( " -v Important messages\n" );
printf( " -vv Important and informational messages\n" );
printf( " -vvv Important, informational and debug messages\n" );
printf( " --extdir=<path> Extension directory, use several times for multiple\n" );
printf( " --config=<path|file> Configuration directory, use several times for multiple\n" );
printf( " --option=<key>:<value> Additional configuration key and value separated by a colon\n" );
exit( 1 );
}
$exectimeStart = microtime( true );
try
{
$params = $_SERVER['argv'];
array_shift( $params );
$options = options( $params );
if( ( $site = array_shift( $params ) ) === null ) {
$site = 'default';
}
if( ( $tplsite = array_shift( $params ) ) === null ) {
$tplsite = 'default';
}
$boostrap = new \Aimeos\Bootstrap( (array) ( $options['extdir'] ?? [] ) );
\Aimeos\Setup::use( $boostrap, config( (array) ( $options['option'] ?? [] ) ) )
->verbose( verbose( $options ) )
->up( $site, $tplsite );
}
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 );
if( verbose( $options ) ) {
printf( "Setup process took %1\$f sec\n\n", ( $exectimeStop - $exectimeStart ) );
}