-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathSite_Command.php
More file actions
411 lines (344 loc) · 12.5 KB
/
Site_Command.php
File metadata and controls
411 lines (344 loc) · 12.5 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
use EE\Dispatcher\CommandFactory;
use EE\Model\Site;
/**
* Adds site related functionality to EasyEngine
*/
class Site_Command {
/**
* @var array $site_types Array to hold all the registered site types and their callback classes.
*/
protected static $site_types = [];
/**
* @var Site_Command $instance Hold an instance of the class.
*/
private static $instance;
/**
* The singleton method to hold the instance of site-command.
*
* @return Site_Command
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new Site_Command();
}
return self::$instance;
}
/**
* Function to register different site-types.
*
* @param string $name Name of the site-type.
* @param string $callback The callback function/class for that type.
*/
public static function add_site_type( $name, $callback ) {
if ( isset( self::instance()->site_types[ $name ] ) ) {
EE::warning( sprintf( '%s site-type had already been previously registered by %s. It is overridden by the new package class %s. Please update your packages to resolve this.', $name, self::$instance->site_types[ $name ], $callback ) );
}
self::instance()::$site_types[ $name ] = $callback;
}
/**
* Method to get the list of registered site-types.
*
* @return array associative array of site-types and their callbacks.
*/
public static function get_site_types() {
return self::instance()::$site_types;
}
/**
* Performs site operations. Check `ee help site` for more info.
* Invoked function of site-type routing. Called when `ee site` is invoked.
* Performs the routing to respective site-type passed using either `--type=`,
* Or discovers the type from the site-name and fetches the type from it,
* Or falls down to the default site-type defined by the user,
* Or finally the most basic site-type and the default included in this package, type=html.
*/
public function __invoke( $args, $assoc_args ) {
if ( ! empty( $args[0] ) && 'cmd-dump' === $args[0] ) {
$this->cmd_dump();
return;
}
$last_arg = array_pop( $args );
if ( substr( $last_arg, 0, 4 ) === 'http' ) {
$last_arg = str_replace( [ 'https://', 'http://' ], '', $last_arg );
}
if ( ! empty( $last_arg ) ) {
$args[] = EE\Utils\remove_trailing_slash( $last_arg );
}
$site_types = self::get_site_types();
$assoc_args = $this->convert_old_args_to_new_args( $args, $assoc_args );
// default site-type.
$type = 'html';
if ( in_array( reset( $args ), [ 'create', 'update' ], true ) || empty( $args ) ) {
$unset = true;
if ( ! empty( $args[0] ) && 'create' === $args[0] ) {
$args = $this->name_checks_and_updates( $args );
\EE\Auth\Utils\init_global_admin_tools_auth( false );
}
if ( ! empty( $args[0] ) && 'update' === $args[0] ) {
$unset = false;
$type = $this->determine_type( $type, $args );
}
if ( isset( $assoc_args['type'] ) ) {
$type = $assoc_args['type'];
if ( $unset ) {
unset( $assoc_args['type'] );
}
}
} elseif ( in_array( reset( $args ), [ 'ssl-renew' ], true ) && array_key_exists( 'all', $assoc_args ) ) {
$sites = Site::all();
unset( $assoc_args['all'] );
foreach ( $sites as $site ) {
$type = $site->site_type;
$args = [ 'site', 'ssl-renew', $site->site_url ];
$callback = $site_types[ $type ];
if ( 'le' !== $site->site_ssl || ! $site->site_enabled || 'inherit' === $site->site_ssl ) {
continue;
}
$api_key_absent = empty( EE\Utils\get_config_value( 'cloudflare-api-key' ) ) && empty ( get_config_value( 'cloudflare-api-token' ) );
$skip_wildcard_warning = false;
if ( $site->site_ssl_wildcard && $api_key_absent ) {
EE::warning( "Wildcard site found: $site->site_url, skipping it as api keys not found. Please renew this site manually using command `ee site ssl-renew $site->site_url`" );
if ( ! $skip_wildcard_warning ) {
EE::warning( "As this is a wildcard certificate, it cannot be automatically renewed.\nPlease run `ee site ssl-renew $site->site_url` to renew the certificate, or add cloudflare api key in EasyEngine config. Ref: https://rt.cx/eecf" );
$skip_wildcard_warning = true;
}
continue;
}
$command = EE::get_root_command();
$leaf_command = CommandFactory::create( 'site', $callback, $command );
$command->add_subcommand( 'site', $leaf_command );
EE::run_command( $args, $assoc_args );
}
die;
} else {
$type = $this->determine_type( $type, $args );
}
array_unshift( $args, 'site' );
if ( ! isset( $site_types[ $type ] ) ) {
$error = sprintf(
'\'%1$s\' is not a registered site type of \'ee site --type=%1$s\'. See \'ee help site --type=%1$s\' for available subcommands.',
$type
);
EE::error( $error );
}
$callback = $site_types[ $type ];
$command = EE::get_root_command();
$leaf_command = CommandFactory::create( 'site', $callback, $command );
$command->add_subcommand( 'site', $leaf_command );
EE::run_command( $args, $assoc_args );
}
/**
* Function to determine type.
*
* Discovers the type from the site-name and fetches the type from it,
* Or falls down to the default site-type defined by the user,
* Or finally the most basic site-type and the default included in this package, type=html.
*
* @param array $args Command line arguments passed to site-command.
* @param string $default_type Default site-type.
*
* @return string site-type.
* @throws \EE\ExitException
*
*/
private function determine_type( $default_type, $args ) {
$type = $default_type;
$last_arg = array_pop( $args );
$arg_search = Site::find( $last_arg, [ 'site_type' ] );
if ( $arg_search ) {
return $arg_search->site_type;
}
$site_name = EE\Site\Utils\get_site_name();
if ( $site_name ) {
if ( strpos( $last_arg, '.' ) !== false ) {
$args[] = $site_name;
EE::error(
sprintf(
'%s is not a valid site-name. Did you mean `ee site %s`?',
$last_arg,
implode( ' ', $args )
)
);
}
$type = Site::find( $site_name, [ 'site_type' ] )->site_type;
}
return $type;
}
/**
* Convert EE v3 args to the newer syntax of arguments.
*
* @param array $args Commandline arguments passed.
* @param array $assoc_args Commandline associative arguments passed.
*
* @return array Updated $assoc_args.
*/
private function convert_old_args_to_new_args( $args, $assoc_args ) {
if (
( ! in_array( reset( $args ), [ 'create' ], true ) &&
! empty( $args ) ) ||
! empty( $assoc_args['type'] )
) {
return $assoc_args;
}
$ee3_compat_array_map_to_type = [
'wp' => [ 'type' => 'wp' ],
'vip' => [ 'type' => 'wp', 'vip' => ( $assoc_args['vip'] ?? '' ) ],
'wpsubdom' => [ 'type' => 'wp', 'mu' => 'subdom' ],
'wpsubdir' => [ 'type' => 'wp', 'mu' => 'subdir' ],
'wpredis' => [ 'type' => 'wp', 'cache' => true ],
'html' => [ 'type' => 'html' ],
'php' => [ 'type' => 'php' ],
'mysql' => [ 'type' => 'php', 'with-db' => true ],
'le' => [ 'ssl' => 'le' ],
'letsencrypt' => [ 'ssl' => 'le' ],
];
foreach ( $ee3_compat_array_map_to_type as $from => $to ) {
if ( isset( $assoc_args[ $from ] ) ) {
unset( $assoc_args[ $from ] );
$assoc_args = array_merge( $assoc_args, $to );
}
}
if ( ! empty( $assoc_args['type'] ) && 'wp' === $assoc_args['type'] ) {
// ee3 backward compatibility flags
$wp_compat_array_map = [
'user' => 'admin-user',
'pass' => 'admin-pass',
'email' => 'admin-email',
];
foreach ( $wp_compat_array_map as $from => $to ) {
if ( isset( $assoc_args[ $from ] ) ) {
$assoc_args[ $to ] = $assoc_args[ $from ];
unset( $assoc_args[ $from ] );
}
}
}
// backward compatibility error for deprecated flags.
$unsupported_create_old_args = [
'w3tc',
'wpsc',
'wpfc',
'pagespeed',
'hhvm',
];
$old_arg = array_intersect( $unsupported_create_old_args, array_keys( $assoc_args ) );
$old_args = implode( ' --', $old_arg );
if ( isset( $args[1] ) && 'create' === $args[1] && ! empty ( $old_arg ) ) {
\EE::error( "Sorry, --$old_args flag/s is/are no longer supported in EE v4.\nPlease run `ee help " . implode( ' ', $args ) . '`.' );
}
return $assoc_args;
}
/**
* Check and update site-url according to the environment and minimum requirements.
*
* @param array $args Input args.
*
* @return array $args Updated args.
*/
private function name_checks_and_updates( $args ) {
if ( empty( $args[1] ) ) {
return $args;
}
$site_url = $args[1];
$ends_with_string = '.test';
$diff = strlen( $site_url ) - strlen( $ends_with_string );
if ( $diff >= 0 && false !== strpos( $site_url, $ends_with_string, $diff ) ) {
return $args;
}
if ( false !== strpos( $site_url, '.' ) ) {
if ( IS_DARWIN ) {
EE::warning( sprintf( 'We only support the `.test` TLD in dev environment. Please configure dns entry manually for %s', $site_url ) );
}
} else {
$site_url .= $ends_with_string;
$args[1] = $site_url;
}
return $args;
}
private function cmd_dump() {
$site_types = self::get_site_types();
$command = EE::get_root_command();
foreach ( $site_types as $name => $callback ) {
$site_type_name = 'site_type_' . $name;
$leaf_command = CommandFactory::create( $site_type_name, $callback, $command );
$command->add_subcommand( $site_type_name, $leaf_command );
}
$command_doc = $this->command_to_array( $command );
$site_command_key = array_search( 'site', array_column( $command_doc['subcommands'], 'name' ) );
$site_subcommands = [];
foreach ( $command_doc['subcommands'] as $key => $subcommand ) {
if ( strpos( $subcommand['name'], 'site_type_' ) !== false ) {
$get_type = explode( '_', $subcommand['name'] );
if ( empty( $get_type[2] ) ) {
continue;
}
$site_subcommands[ $subcommand['name'] ] = $subcommand['subcommands'];
unset( $command_doc['subcommands'][ $key ] );
}
}
$common_commands = reset( $site_subcommands );
$total_site_types = count( $site_subcommands );
$comparator = [];
foreach ( $common_commands as $command ) {
$comparator[ $command['name'] ] = [ 'longdesc' => $command['longdesc'], 'common_count' => 0 ];
}
foreach ( $site_subcommands as $site_type_sub_commands ) {
foreach ( $site_type_sub_commands as $site_type_sub_command ) {
if ( ! empty( $comparator[ $site_type_sub_command['name'] ] ) && ( 0 === strcmp( $comparator[ $site_type_sub_command['name'] ]['longdesc'], $site_type_sub_command['longdesc'] ) ) ) {
$comparator[ $site_type_sub_command['name'] ]['common_count'] ++;
}
}
}
foreach ( $comparator as $command_name => $data ) {
if ( $total_site_types !== $data['common_count'] ) {
$key_to_unset = array_search( $command_name, array_column( $common_commands, 'name' ) );
if ( isset( $common_commands[ $key_to_unset ] ) ) {
unset( $common_commands[ $key_to_unset ] );
$common_commands = array_values( $common_commands );
}
}
}
$site_type_specific_commands = [];
foreach ( $site_subcommands as $key_type => $sub_commands ) {
$get_type = explode( '_', $key_type );
if ( empty( $get_type[2] ) ) {
continue;
}
$type = $get_type[2];
$specific_commands = array_udiff( $site_subcommands[ $key_type ], $common_commands, [
$this,
'compare_command_names'
] );
$mapped_array = array_map( function ( $cmd ) use ( $type ) {
$cmd['name'] = $cmd['name'] . ' --type=' . $type;
return $cmd;
}, $specific_commands );
$site_type_specific_commands = array_merge( $site_type_specific_commands, $mapped_array );
}
$final_site_sub_commands = array_merge( $common_commands, $site_type_specific_commands );
$names = array_column( $final_site_sub_commands, 'name' );
array_multisort( $names, SORT_ASC, $final_site_sub_commands );
$command_doc['subcommands'][ $site_command_key ]['subcommands'] = $final_site_sub_commands;
echo json_encode( $command_doc );
}
private function compare_command_names( $a, $b ) {
if ( $a['name'] === $b['name'] ) {
return 0;
} else {
return ( $a['name'] < $b['name'] ? - 1 : 1 );
}
}
private function command_to_array( $command ) {
$dump = array(
'name' => $command->get_name(),
'description' => $command->get_shortdesc(),
'longdesc' => $command->get_longdesc(),
);
foreach ( $command->get_subcommands() as $subcommand ) {
$dump['subcommands'][] = $this->command_to_array( $subcommand );
}
if ( empty( $dump['subcommands'] ) ) {
$dump['synopsis'] = (string) $command->get_synopsis();
}
return $dump;
}
}