Passed
Push — master ( 16ddec...5bff87 )
by Aimeos
05:22
created

getOptions()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 40
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 19
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 40
rs 8.4444
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2021
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
	throw new ErrorException( $message, 0, $severity, $file, $line );
19
});
20
21
ini_set( 'display_errors', 1 );
22
date_default_timezone_set( 'UTC' );
23
24
25
/**
26
 * Returns the configuration based on the given arguments
27
 *
28
 * @param array List of key/value string separated by colon (e.g. "key:value")
0 ignored issues
show
Bug introduced by
The type List was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
 * @return array Associative list of key value pairs
30
 */
31
function config( array $options ) : array
32
{
33
	$config = [];
34
35
	foreach( $options as $option )
36
	{
37
		list( $key, $val ) = explode( ':', $option );
38
		$config[$key] = $val;
39
	}
40
41
	return $config;
42
}
43
44
45
/**
46
 * Returns the command options given by the user
47
 *
48
 * @param array &$params List of parameters
49
 * @return array Associative list of option name and value(s)
50
 */
51
function options( array &$params )
52
{
53
	$options = [];
54
55
	foreach( $params as $key => $option )
56
	{
57
		if( $option === '--help' ) {
58
			usage();
59
		}
60
61
		if( !strncmp( $option, '--', 2 ) && ( $pos = strpos( $option, '=', 2 ) ) !== false )
62
		{
63
			if( ( $name = substr( $option, 2, $pos - 2 ) ) !== false )
64
			{
65
				if( isset( $options[$name] ) )
66
				{
67
					$options[$name] = (array) $options[$name];
68
					$options[$name][] = substr( $option, $pos + 1 );
69
				}
70
				else
71
				{
72
					$options[$name] = substr( $option, $pos + 1 );
73
				}
74
75
				unset( $params[$key] );
76
			}
77
			else
78
			{
79
				printf( "Invalid option \"%1\$s\"\n", $option );
80
				usage();
81
			}
82
		}
83
		elseif( $option[0] === '-' )
84
		{
85
			$options[$option[1]] = substr( $option, 1 );
86
			unset( $params[$key] );
87
		}
88
	}
89
90
	return $options;
91
}
92
93
94
/**
95
 * Returns the verbosity level based on the given arguments
96
 *
97
 * @param array Associative list of key value pairs
0 ignored issues
show
Bug introduced by
The type Associative was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
98
 * @param string Verbosity level ("v", "vv", "vvv" or empty string)
0 ignored issues
show
Bug introduced by
The type Verbosity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
99
 */
100
function verbose( array $options ) : string
101
{
102
	return isset( $options['q'] ) ? '' : ( $options['v'] ?? 'vv' );
103
}
104
105
106
/**
107
 * Prints the command usage and options, exits the program after printing
108
 */
109
function usage()
110
{
111
	printf( "Usage: php setup.php [OPTION]* [sitecode] [tplsite]\n" );
112
	printf( "  -q                       Quiet\n" );
113
	printf( "  -v                       Important messages\n" );
114
	printf( "  -vv                      Important and informational messages\n" );
115
	printf( "  -vvv                     Important, informational and debug messages\n" );
116
	printf( "  --extdir=<path>          Extension directory, use several times for multiple\n" );
117
	printf( "  --config=<path|file>     Configuration directory, use several times for multiple\n" );
118
	printf( "  --option=<key>:<value>   Additional configuration key and value separated by a colon\n" );
119
	exit( 1 );
0 ignored issues
show
Best Practice introduced by
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...
120
}
121
122
123
124
$exectimeStart = microtime( true );
125
126
try
127
{
128
	$params = $_SERVER['argv'];
129
	array_shift( $params );
130
131
	$options = options( $params );
132
133
	if( ( $site = array_shift( $params ) ) === null ) {
134
		$site = 'default';
135
	}
136
137
	if( ( $tplsite = array_shift( $params ) ) === null ) {
138
		$tplsite = 'default';
139
	}
140
141
	$boostrap = new \Aimeos\Bootstrap( (array) ( $options['extdir'] ?? [] ) );
142
	\Aimeos\Setup::use( $boostrap, config( (array) $options['option'] ?? [] ) )
143
		->verbose( verbose( $options ) )
144
		->up( $site, $tplsite );
145
}
146
catch( Throwable $t )
147
{
148
	echo "\n\nCaught PHP error while processing setup";
149
	echo "\n\nMessage:\n";
150
	echo $t->getMessage();
151
	echo "\n\nStack trace:\n";
152
	echo $t->getTraceAsString();
153
	echo "\n\n";
154
	exit( 1 );
155
}
156
catch( \Exception $e )
157
{
158
	echo "\n\nCaught exception while processing setup";
159
	echo "\n\nMessage:\n";
160
	echo $e->getMessage();
161
	echo "\n\nStack trace:\n";
162
	echo $e->getTraceAsString();
163
	echo "\n\n";
164
	exit( 1 );
165
}
166
167
$exectimeStop = microtime( true );
168
169
if( verbose( $options ) ) {
170
	printf( "Setup process took %1\$f sec\n\n", ( $exectimeStop - $exectimeStart ) );
171
}
172