11<?php
22
3+ use EE \Utils ;
4+
35/**
46 * Manages global containers of EasyEngine.
57 *
1012 *
1113 * @package ee-cli
1214 */
13-
14- use EE \Utils ;
15-
1615class Service_Command extends EE_Command {
1716
17+ /**
18+ * @var array Array of containers defined in global docker-compose.yml
19+ */
1820 private $ whitelisted_containers = [
19- 'ee- nginx-proxy '
21+ 'nginx-proxy ' ,
2022 ];
2123
24+ /**
25+ * Service_Command constructor.
26+ *
27+ * Changes directory to EE_CONF_ROOT since that's where all docker-compose commands will be executed
28+ */
29+ public function __construct () {
30+ chdir ( EE_CONF_ROOT );
31+ }
32+
2233 /**
2334 * Starts global containers.
2435 *
2536 * ## OPTIONS
2637 *
27- * <container -name>
38+ * <service -name>
2839 * : Name of container.
2940 */
3041 public function start ( $ args , $ assoc_args ) {
3142 $ container = $ this ->filter_container ( $ args );
32- \EE \Utils \default_launch ( "docker start $ container " );
43+
44+ EE ::exec ( "docker-compose start $ container " , true , true );
45+ }
46+
47+ /**
48+ * Returns valid container name from arguments.
49+ */
50+ private function filter_container ( $ args ) {
51+ $ containers = array_intersect ( $ this ->whitelisted_containers , $ args );
52+
53+ if ( empty ( $ containers ) ) {
54+ EE ::error ( "Unable to find global EasyEngine container $ args [0 ]" );
55+ }
56+
57+ return $ containers [0 ];
3358 }
3459
3560 /**
3661 * Stops global containers.
3762 *
3863 * ## OPTIONS
3964 *
40- * <container -name>
65+ * <service -name>
4166 * : Name of container.
4267 */
4368 public function stop ( $ args , $ assoc_args ) {
4469 $ container = $ this ->filter_container ( $ args );
45- \ EE \ Utils \default_launch ( "docker stop $ container " );
70+ EE :: exec ( "docker-compose stop $ container " , true , true );
4671 }
4772
4873 /**
4974 * Restarts global containers.
5075 *
5176 * ## OPTIONS
5277 *
53- * <container -name>
78+ * <service -name>
5479 * : Name of container.
5580 */
5681 public function restart ( $ args , $ assoc_args ) {
5782 $ container = $ this ->filter_container ( $ args );
58- \ EE \ Utils \default_launch ( "docker restart $ container " );
83+ EE :: exec ( "docker-compose restart $ container " , true , true );
5984 }
6085
6186 /**
@@ -68,27 +93,23 @@ public function restart( $args, $assoc_args ) {
6893 */
6994 public function reload ( $ args , $ assoc_args ) {
7095 $ container = $ this ->filter_container ( $ args );
71- $ command = $ this ->container_reload_command ( $ container );
72- \ EE \ Utils \default_launch ( "docker exec $ container $ command " );
96+ $ command = $ this ->container_reload_command ( $ container );
97+ EE :: exec ( "docker-compose exec $ container $ command " , true , true );
7398 }
7499
75100 /**
76- * Returns valid container name from arguments.
101+ * Returns reload command of a container.
102+ * This is necessary since command to reload each service can be different.
103+ *
104+ * @param $container name of conntainer
105+ *
106+ * @return mixed
77107 */
78- private function filter_container ( $ args ) {
79- $ containers = array_intersect ( $ this ->whitelisted_containers , $ args );
80-
81- if ( empty ( $ containers ) ) {
82- EE ::error ( "Unable to find global EasyEngine container $ args [0 ]" );
83- }
84-
85- return $ containers [0 ];
86- }
87-
88- private function container_reload_command ( $ container ) {
108+ private function container_reload_command ( string $ container ) {
89109 $ command_map = [
90- 'ee- nginx-proxy ' => "sh -c 'nginx -t && service nginx reload' "
110+ 'nginx-proxy ' => "sh -c 'nginx -t && service nginx reload' " ,
91111 ];
112+
92113 return $ command_map [ $ container ];
93114 }
94115}
0 commit comments