1
1
<?php
2
2
3
+ use EE \Utils ;
4
+ use EE \Model \Site ;
5
+ use function EE \Site \Utils \auto_site_name ;
6
+
3
7
/**
4
- * Executes wp-cli command on a site .
8
+ * Brings up a shell to run wp-cli, composer etc .
5
9
*
6
10
* ## EXAMPLES
7
11
*
8
- * # Create simple WordPress site
9
- * $ ee wp test.local plugin list
12
+ * # Open shell of example.com
13
+ * $ ee shell example.com
10
14
*
11
15
* @package ee-cli
12
16
*/
13
-
14
- use EE \Utils ;
15
- use EE \Model \Site ;
16
-
17
17
class Shell_Command extends EE_Command {
18
18
19
19
/**
@@ -25,22 +25,31 @@ class Shell_Command extends EE_Command {
25
25
* : Name of website to run shell on.
26
26
*/
27
27
public function __invoke ( $ args ) {
28
+
28
29
EE \Utils \delem_log ( 'ee shell start ' );
29
- $ args = EE \ SiteUtils \ auto_site_name ( $ args , 'shell ' , '' );
30
+ $ args = auto_site_name ( $ args , 'shell ' , '' );
30
31
$ site_name = EE \Utils \remove_trailing_slash ( $ args [0 ] );
31
32
32
- $ site = Site::find ( $ site_name, [ ' site_enabled ' , ' site_fs_path ' ] );
33
+ $ site = Site::find ( $ site_name );
33
34
34
35
if ( ! $ site || ! $ site ->site_enabled ) {
35
36
EE ::error ( "Site $ site_name does not exist or is not enabled. " );
36
37
}
37
38
38
39
chdir ( $ site ->site_fs_path );
40
+ $ this ->check_shell_available ( 'php ' , $ site );
39
41
$ this ->run ( "docker-compose exec --user='www-data' php bash " );
40
42
EE \Utils \delem_log ( 'ee shell end ' );
41
43
}
42
44
45
+ /**
46
+ * Run the command to open shell.
47
+ *
48
+ * @param string $cmd Command to be executed to open shell.
49
+ * @param null|array $descriptors File descriptors for proc.
50
+ */
43
51
private function run ( $ cmd , $ descriptors = null ) {
52
+
44
53
EE \Utils \check_proc_available ( 'ee_shell ' );
45
54
if ( ! $ descriptors ) {
46
55
$ descriptors = array ( STDIN , STDOUT , STDERR );
@@ -57,4 +66,21 @@ private function run( $cmd, $descriptors = null ) {
57
66
}
58
67
}
59
68
69
+ /**
70
+ * Function to check if container supporting shell is present in docker-compose.yml or not.
71
+ *
72
+ * @param string $shell_container Container to be checked.
73
+ * @param Object $site Contains relevant site info.
74
+ */
75
+ private function check_shell_available ( $ shell_container , $ site ) {
76
+
77
+ $ launch = EE ::launch ( 'docker-compose config --services ' );
78
+ $ services = explode ( PHP_EOL , trim ( $ launch ->stdout ) );
79
+ if ( in_array ( $ shell_container , $ services , true ) ) {
80
+ return ;
81
+ }
82
+ EE ::debug ( 'Site type: ' . $ site ->site_type );
83
+ EE ::debug ( 'Site command: ' . $ site ->app_sub_type );
84
+ EE ::error ( sprintf ( '%s site does not have support to launch %s shell. ' , $ site ->site_url , $ shell_container ) );
85
+ }
60
86
}
0 commit comments