Issues (251)

Severity
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2024
6
 */
7
8
9
require 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
10
11
12
if( php_sapi_name() != 'cli' ) {
13
	exit( 'Setup can only be started via command line for security reasons' );
14
}
15
16
17
set_error_handler( function( $severity, $message, $file, $line ) {
18
	if( $severity & E_DEPRECATED === 0 ) {
19
		throw new ErrorException( $message, 0, $severity, $file, $line );
20
	}
21
	error_log( $message . ' in ' . $file . ' on line ' . $line );
22
});
23
24
ini_set( 'display_errors', 1 );
25
date_default_timezone_set( 'UTC' );
26
27
28
/**
29
 * Returns the configuration based on the given arguments
30
 *
31
 * @param array $options List of key/value string separated by colon (e.g. "key:value")
32
 * @return array Associative list of key value pairs
33
 */
34
function config( array $options ) : array
35
{
36
	$config = [];
37
38
	foreach( $options as $option )
39
	{
40
		list( $key, $val ) = explode( ':', $option );
41
		$config[$key] = $val;
42
	}
43
44
	return $config;
45
}
46
47
48
/**
49
 * Returns the command options given by the user
50
 *
51
 * @param array &$params List of parameters
52
 * @return array Associative list of option name and value(s)
53
 */
54
function options( array &$params )
55
{
56
	$options = [];
57
58
	foreach( $params as $key => $option )
59
	{
60
		if( $option === '--help' ) {
61
			usage();
62
		}
63
64
		if( !strncmp( $option, '--', 2 ) && ( $pos = strpos( $option, '=', 2 ) ) !== false )
65
		{
66
			if( ( $name = substr( $option, 2, $pos - 2 ) ) !== false )
67
			{
68
				if( isset( $options[$name] ) )
69
				{
70
					$options[$name] = (array) $options[$name];
71
					$options[$name][] = substr( $option, $pos + 1 );
72
				}
73
				else
74
				{
75
					$options[$name] = substr( $option, $pos + 1 );
76
				}
77
78
				unset( $params[$key] );
79
			}
80
			else
81
			{
82
				printf( "Invalid option \"%1\$s\"\n", $option );
83
				usage();
84
			}
85
		}
86
		elseif( $option[0] === '-' )
87
		{
88
			$options[$option[1]] = substr( $option, 1 );
89
			unset( $params[$key] );
90
		}
91
	}
92
93
	return $options;
94
}
95
96
97
/**
98
 * Returns the verbosity level based on the given arguments
99
 *
100
 * @param array $options Associative list of key value pairs
101
 * @return string Verbosity level ("v", "vv", "vvv" or empty string)
102
 */
103
function verbose( array $options ) : string
104
{
105
	return isset( $options['q'] ) ? '' : ( $options['v'] ?? 'vv' );
106
}
107
108
109
/**
110
 * Prints the command usage and options, exits the program after printing
111
 */
112
function usage()
113
{
114
	printf( "Usage: php up.php [OPTION]* [sitecode] [tplsite]\n" );
115
	printf( "  -q                       Quiet\n" );
116
	printf( "  -v                       Important messages\n" );
117
	printf( "  -vv                      Important and informational messages\n" );
118
	printf( "  -vvv                     Important, informational and debug messages\n" );
119
	printf( "  --extdir=<path>          Extension directory, use several times for multiple\n" );
120
	printf( "  --config=<path|file>     Configuration directory, use several times for multiple\n" );
121
	printf( "  --option=<key>:<value>   Additional configuration key and value separated by a colon\n" );
122
	exit( 1 );
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
123
}
124
125
126
127
$exectimeStart = microtime( true );
128
129
try
130
{
131
	$params = $_SERVER['argv'];
132
	array_shift( $params );
133
134
	$options = options( $params );
135
136
	if( ( $site = array_shift( $params ) ) === null ) {
137
		$site = 'default';
138
	}
139
140
	if( ( $tplsite = array_shift( $params ) ) === null ) {
141
		$tplsite = 'default';
142
	}
143
144
	$boostrap = new \Aimeos\Bootstrap( (array) ( $options['extdir'] ?? [] ) );
145
	\Aimeos\Setup::use( $boostrap, config( (array) ( $options['option'] ?? [] ) ) )
146
		->verbose( verbose( $options ) )
147
		->up( $site, $tplsite );
148
}
149
catch( Throwable $t )
150
{
151
	echo "\n\nCaught PHP error while processing setup";
152
	echo "\n\nMessage:\n";
153
	echo $t->getMessage();
154
	echo "\n\nStack trace:\n";
155
	echo $t->getTraceAsString();
156
	echo "\n\n";
157
	exit( 1 );
158
}
159
catch( \Exception $e )
160
{
161
	echo "\n\nCaught exception while processing setup";
162
	echo "\n\nMessage:\n";
163
	echo $e->getMessage();
164
	echo "\n\nStack trace:\n";
165
	echo $e->getTraceAsString();
166
	echo "\n\n";
167
	exit( 1 );
168
}
169
170
$exectimeStop = microtime( true );
171
172
if( verbose( $options ) ) {
173
	printf( "Setup process took %1\$f sec\n\n", ( $exectimeStop - $exectimeStart ) );
174
}
175