|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Executes wp-cli command on a site. |
| 5 | + * |
| 6 | + * ## EXAMPLES |
| 7 | + * |
| 8 | + * # Create simple WordPress site |
| 9 | + * $ ee wp test.local plugin list |
| 10 | + * |
| 11 | + * @package ee-cli |
| 12 | + */ |
| 13 | + |
| 14 | +use EE\Utils; |
| 15 | + |
| 16 | +class Shell_Command extends EE_Command { |
| 17 | + |
| 18 | + /** |
| 19 | + * Brings up a shell to run wp-cli, composer etc. |
| 20 | + * |
| 21 | + * ## OPTIONS |
| 22 | + * |
| 23 | + * [<site-name>] |
| 24 | + * : Name of website to run shell on. |
| 25 | + */ |
| 26 | + public function __invoke( $args ) { |
| 27 | + EE\Utils\delem_log( 'ee shell start' ); |
| 28 | + $args = EE\Utils\set_site_arg( $args, 'shell' ); |
| 29 | + $site_name = EE\Utils\remove_trailing_slash( $args[0] ); |
| 30 | + if ( EE::db()::site_in_db( $site_name ) ) { |
| 31 | + $db_select = EE::db()::select( ['site_path'], ['sitename' => $site_name]); |
| 32 | + $site_root = $db_select[0]['site_path']; |
| 33 | + } else { |
| 34 | + EE::error( "Site $site_name does not exist." ); |
| 35 | + } |
| 36 | + chdir($site_root); |
| 37 | + $this->run( "docker-compose exec --user='www-data' php bash" ); |
| 38 | + EE\Utils\delem_log( 'ee shell end' ); |
| 39 | + } |
| 40 | + |
| 41 | + private function run( $cmd, $descriptors = null ) { |
| 42 | + EE\Utils\check_proc_available( 'ee_shell' ); |
| 43 | + if ( ! $descriptors ) { |
| 44 | + $descriptors = array( STDIN, STDOUT, STDERR ); |
| 45 | + } |
| 46 | + |
| 47 | + $final_cmd = EE\Utils\force_env_on_nix_systems( $cmd ); |
| 48 | + $proc = EE\Utils\proc_open_compat( $final_cmd, $descriptors, $pipes ); |
| 49 | + if ( ! $proc ) { |
| 50 | + exit( 1 ); |
| 51 | + } |
| 52 | + $r = proc_close( $proc ); |
| 53 | + if ( $r ) { |
| 54 | + exit( $r ); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments