1313 * @package ee-cli
1414 */
1515
16+ use EE \Model \Site ;
1617use \Symfony \Component \Filesystem \Filesystem ;
1718
1819class Site_Command extends EE_Site_Command {
1920
20- /**
21- * @var string $command Name of the command being run.
22- */
23- private $ command ;
24-
2521 /**
2622 * @var array $site Associative array containing essential site related information.
2723 */
@@ -57,29 +53,22 @@ class Site_Command extends EE_Site_Command {
5753 */
5854 private $ fs ;
5955
60- /**
61- * @var Object $db Object to access `EE::db()` functions.
62- */
63- private $ db ;
64-
6556 public function __construct () {
6657
6758 $ this ->level = 0 ;
68- $ this ->command = 'site ' ;
6959 pcntl_signal ( SIGTERM , [ $ this , "rollback " ] );
7060 pcntl_signal ( SIGHUP , [ $ this , "rollback " ] );
7161 pcntl_signal ( SIGUSR1 , [ $ this , "rollback " ] );
7262 pcntl_signal ( SIGINT , [ $ this , "rollback " ] );
7363 $ shutdown_handler = new Shutdown_Handler ();
7464 register_shutdown_function ( [ $ shutdown_handler , "cleanup " ], [ &$ this ] );
75- $ this ->db = EE ::db ();
7665 $ this ->docker = EE ::docker ();
7766 $ this ->logger = EE ::get_file_logger ()->withName ( 'site_command ' );
7867 $ this ->fs = new Filesystem ();
7968 }
8069
8170 /**
82- * Runs the standard WordPress Site installation.
71+ * Runs the standard WordPress site installation.
8372 *
8473 * ## OPTIONS
8574 *
@@ -101,14 +90,14 @@ public function create( $args, $assoc_args ) {
10190 EE ::warning ( 'This is a beta version. Please don \'t use it in production. ' );
10291 $ this ->logger ->debug ( 'args: ' , $ args );
10392 $ this ->logger ->debug ( 'assoc_args: ' , empty ( $ assoc_args ) ? [ 'NULL ' ] : $ assoc_args );
104- $ this ->site ['name ' ] = strtolower ( EE \Utils \remove_trailing_slash ( $ args [0 ] ) );
93+ $ this ->site ['url ' ] = strtolower ( EE \Utils \remove_trailing_slash ( $ args [0 ] ) );
10594 $ this ->site ['type ' ] = EE \Utils \get_flag_value ( $ assoc_args , 'type ' , 'html ' );
10695 if ( 'html ' !== $ this ->site ['type ' ] ) {
10796 EE ::error ( sprintf ( 'Invalid site-type: %s ' , $ this ->site ['type ' ] ) );
10897 }
10998
110- if ( $ this -> db :: site_in_db ( $ this ->site ['name ' ] ) ) {
111- EE ::error ( sprintf ( "Site %1 \$s already exists. If you want to re-create it please delete the older one using: \n`ee site delete %1 \$s` " , $ this ->site ['name ' ] ) );
99+ if ( Site:: find ( $ this ->site ['url ' ] ) ) {
100+ EE ::error ( sprintf ( "Site %1 \$s already exists. If you want to re-create it please delete the older one using: \n`ee site delete %1 \$s` " , $ this ->site ['url ' ] ) );
112101 }
113102
114103 $ this ->le = EE \Utils \get_flag_value ( $ assoc_args , 'letsencrypt ' );
@@ -131,14 +120,14 @@ public function create( $args, $assoc_args ) {
131120 public function info ( $ args , $ assoc_args ) {
132121
133122 EE \Utils \delem_log ( 'site info start ' );
134- if ( ! isset ( $ this ->site ['name ' ] ) ) {
135- $ args = EE \SiteUtils \auto_site_name ( $ args , $ this -> command , __FUNCTION__ );
123+ if ( ! isset ( $ this ->site ['url ' ] ) ) {
124+ $ args = EE \SiteUtils \auto_site_name ( $ args , ' site ' , __FUNCTION__ );
136125 $ this ->populate_site_info ( $ args );
137126 }
138127 $ ssl = $ this ->le ? 'Enabled ' : 'Not Enabled ' ;
139128 $ prefix = ( $ this ->le ) ? 'https:// ' : 'http:// ' ;
140129 $ info = [
141- [ 'Site ' , $ prefix . $ this ->site ['name ' ] ],
130+ [ 'Site ' , $ prefix . $ this ->site ['url ' ] ],
142131 [ 'Site Root ' , $ this ->site ['root ' ] ],
143132 [ 'SSL ' , $ ssl ],
144133 ];
@@ -161,18 +150,18 @@ private function configure_site_files() {
161150 $ site_src_dir = $ this ->site ['root ' ] . '/app/src ' ;
162151 $ process_user = posix_getpwuid ( posix_geteuid () );
163152
164- EE ::log ( sprintf ( 'Creating site %s. ' , $ this ->site ['name ' ] ) );
153+ EE ::log ( sprintf ( 'Creating site %s. ' , $ this ->site ['url ' ] ) );
165154 EE ::log ( 'Copying configuration files. ' );
166155
167156 $ filter = [];
168157 $ filter [] = $ this ->site ['type ' ];
169158 $ filter [] = $ this ->le ;
170159 $ site_docker = new Site_Docker ();
171160 $ docker_compose_content = $ site_docker ->generate_docker_compose_yml ( $ filter );
172- $ default_conf_content = $ default_conf_content = EE \Utils \mustache_render ( SITE_TEMPLATE_ROOT . '/config/nginx/default.conf.mustache ' , [ 'server_name ' => $ this ->site ['name ' ] ] );
161+ $ default_conf_content = $ default_conf_content = EE \Utils \mustache_render ( SITE_TEMPLATE_ROOT . '/config/nginx/default.conf.mustache ' , [ 'server_name ' => $ this ->site ['url ' ] ] );
173162
174163 $ env_data = [
175- 'virtual_host ' => $ this ->site ['name ' ],
164+ 'virtual_host ' => $ this ->site ['url ' ],
176165 'user_id ' => $ process_user ['uid ' ],
177166 'group_id ' => $ process_user ['gid ' ],
178167 ];
@@ -193,7 +182,7 @@ private function configure_site_files() {
193182 $ this ->fs ->mkdir ( $ site_src_dir );
194183 $ this ->fs ->dumpFile ( $ site_src_dir . '/index.html ' , $ index_html );
195184
196- EE \Siteutils \add_site_redirects ( $ this ->site ['name ' ], $ this ->le );
185+ EE \Siteutils \add_site_redirects ( $ this ->site ['url ' ], $ this ->le );
197186
198187 EE ::success ( 'Configuration files copied. ' );
199188 } catch ( Exception $ e ) {
@@ -206,30 +195,30 @@ private function configure_site_files() {
206195 */
207196 private function create_site () {
208197
209- $ this ->site ['root ' ] = WEBROOT . $ this ->site ['name ' ];
198+ $ this ->site ['root ' ] = WEBROOT . $ this ->site ['url ' ];
210199 $ this ->level = 1 ;
211200 try {
212- EE \Siteutils \create_site_root ( $ this ->site ['root ' ], $ this ->site ['name ' ] );
201+ EE \Siteutils \create_site_root ( $ this ->site ['root ' ], $ this ->site ['url ' ] );
213202 $ this ->level = 2 ;
214- EE \Siteutils \setup_site_network ( $ this ->site ['name ' ] );
203+ EE \Siteutils \setup_site_network ( $ this ->site ['url ' ] );
215204 $ this ->level = 3 ;
216205 $ this ->configure_site_files ();
217206
218207 EE \Siteutils \start_site_containers ( $ this ->site ['root ' ] );
219208
220- EE \Siteutils \create_etc_hosts_entry ( $ this ->site ['name ' ] );
209+ EE \Siteutils \create_etc_hosts_entry ( $ this ->site ['url ' ] );
221210 if ( ! $ this ->skip_chk ) {
222211 $ this ->level = 4 ;
223- EE \Siteutils \site_status_check ( $ this ->site ['name ' ] );
212+ EE \Siteutils \site_status_check ( $ this ->site ['url ' ] );
224213 }
225214 } catch ( Exception $ e ) {
226215 $ this ->catch_clean ( $ e );
227216 }
228217
229218 if ( $ this ->le ) {
230- $ this ->init_le ( $ this ->site ['name ' ], $ this ->site ['root ' ], false );
219+ $ this ->init_le ( $ this ->site ['url ' ], $ this ->site ['root ' ], false );
231220 }
232- $ this ->info ( [ $ this ->site ['name ' ] ], [] );
221+ $ this ->info ( [ $ this ->site ['url ' ] ], [] );
233222 $ this ->create_site_db_entry ();
234223 }
235224
@@ -238,18 +227,17 @@ private function create_site() {
238227 */
239228 private function create_site_db_entry () {
240229
241- $ ssl = $ this ->le ? 1 : 0 ;
242- $ data = [
243- 'sitename ' => $ this ->site ['name ' ],
230+ $ ssl = $ this ->le ? 'letsencrypt ' : null ;
231+
232+ $ site = Site::create ([
233+ 'site_url ' => $ this ->site ['url ' ],
244234 'site_type ' => $ this ->site ['type ' ],
245- 'site_path ' => $ this ->site ['root ' ],
246- 'site_command ' => $ this ->command ,
247- 'is_ssl ' => $ ssl ,
248- 'created_on ' => date ( 'Y-m-d H:i:s ' , time () ),
249- ];
235+ 'site_fs_path ' => $ this ->site ['root ' ],
236+ 'site_ssl ' => $ ssl ,
237+ ]);
250238
251239 try {
252- if ( $ this -> db :: insert ( $ data ) ) {
240+ if ( $ site ) {
253241 EE ::log ( 'Site entry created. ' );
254242 } else {
255243 throw new Exception ( 'Error creating site entry in database. ' );
@@ -264,18 +252,17 @@ private function create_site_db_entry() {
264252 */
265253 private function populate_site_info ( $ args ) {
266254
267- $ this ->site ['name ' ] = EE \Utils \remove_trailing_slash ( $ args [0 ] );
268-
269- if ( $ this ->db ::site_in_db ( $ this ->site ['name ' ] ) ) {
255+ $ this ->site ['url ' ] = EE \Utils \remove_trailing_slash ( $ args [0 ] );
270256
271- $ db_select = $ this -> db :: select ( [], [ ' sitename ' => $ this ->site ['name ' ] ], ' sites ' , 1 );
257+ $ site = Site:: find ( $ this ->site ['url ' ] );
272258
273- $ this ->site ['type ' ] = $ db_select ['site_type ' ];
274- $ this ->site ['root ' ] = $ db_select ['site_path ' ];
275- $ this ->le = $ db_select ['is_ssl ' ];
259+ if ( $ site ) {
260+ $ this ->site ['type ' ] = $ site ->site_type ;
261+ $ this ->site ['root ' ] = $ site ->site_fs_path ;
262+ $ this ->le = $ site ->site_ssl ;
276263
277264 } else {
278- EE ::error ( sprintf ( 'Site %s does not exist. ' , $ this ->site ['name ' ] ) );
265+ EE ::error ( sprintf ( 'Site %s does not exist. ' , $ this ->site ['url ' ] ) );
279266 }
280267 }
281268
@@ -305,7 +292,7 @@ private function catch_clean( $e ) {
305292 EE \Utils \delem_log ( 'site cleanup start ' );
306293 EE ::warning ( $ e ->getMessage () );
307294 EE ::warning ( 'Initiating clean-up. ' );
308- $ this ->delete_site ( $ this ->level , $ this ->site ['name ' ], $ this ->site ['root ' ] );
295+ $ this ->delete_site ( $ this ->level , $ this ->site ['url ' ], $ this ->site ['root ' ] );
309296 EE \Utils \delem_log ( 'site cleanup end ' );
310297 exit ;
311298 }
@@ -317,7 +304,7 @@ private function rollback() {
317304
318305 EE ::warning ( 'Exiting gracefully after rolling back. This may take some time. ' );
319306 if ( $ this ->level > 0 ) {
320- $ this ->delete_site ( $ this ->level , $ this ->site ['name ' ], $ this ->site ['root ' ] );
307+ $ this ->delete_site ( $ this ->level , $ this ->site ['url ' ], $ this ->site ['root ' ] );
321308 }
322309 EE ::success ( 'Rollback complete. Exiting now. ' );
323310 exit ;
0 commit comments