Skip to content

Commit 13b8a99

Browse files
committed
Add comments and update variables
Signed-off-by: Riddhesh Sanghvi <[email protected]>
1 parent 1803125 commit 13b8a99

File tree

1 file changed

+80
-47
lines changed

1 file changed

+80
-47
lines changed

src/Site_Command.php

Lines changed: 80 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,52 @@
1616
use \Symfony\Component\Filesystem\Filesystem;
1717

1818
class Site_Command extends EE_Site_Command {
19+
20+
/**
21+
* @var string $command Name of the command being run.
22+
*/
1923
private $command;
20-
private $site_name;
21-
private $site_root;
22-
private $site_type;
23-
private $db;
24+
25+
/**
26+
* @var array $site Associative array containing essential site related information.
27+
*/
28+
private $site;
29+
30+
/**
31+
* @var object $docker Object to access `EE::docker()` functions.
32+
*/
2433
private $docker;
34+
35+
/**
36+
* @var int $level The level of creation in progress. Essential for rollback in case of failure.
37+
*/
2538
private $level;
39+
40+
/**
41+
* @var object $logger Object of logger.
42+
*/
2643
private $logger;
44+
45+
/**
46+
* @var bool $le Whether the site is letsencrypt or not.
47+
*/
2748
private $le;
49+
50+
/**
51+
* @var bool $skip_chk To skip site status check pre-installation.
52+
*/
2853
private $skip_chk;
54+
55+
/**
56+
* @var Filesystem $fs Symfony Filesystem object.
57+
*/
2958
private $fs;
3059

60+
/**
61+
* @var Object $db Object to access `EE::db()` functions.
62+
*/
63+
private $db;
64+
3165
public function __construct() {
3266

3367
$this->level = 0;
@@ -67,14 +101,14 @@ public function create( $args, $assoc_args ) {
67101
EE::warning( 'This is a beta version. Please don\'t use it in production.' );
68102
$this->logger->debug( 'args:', $args );
69103
$this->logger->debug( 'assoc_args:', empty( $assoc_args ) ? [ 'NULL' ] : $assoc_args );
70-
$this->site_name = strtolower( EE\Utils\remove_trailing_slash( $args[0] ) );
71-
$this->site_type = EE\Utils\get_flag_value( $assoc_args, 'type', 'html' );
72-
if ( 'html' !== $this->site_type ) {
73-
EE::error( "Invalid site-type: $this->site_type" );
104+
$this->site['name'] = strtolower( EE\Utils\remove_trailing_slash( $args[0] ) );
105+
$this->site['type'] = EE\Utils\get_flag_value( $assoc_args, 'type', 'html' );
106+
if ( 'html' !== $this->site['type'] ) {
107+
EE::error( sprintf( 'Invalid site-type: %s', $this->site['type'] ) );
74108
}
75109

76-
if ( $this->db::site_in_db( $this->site_name ) ) {
77-
EE::error( "Site $this->site_name already exists. If you want to re-create it please delete the older one using:\n`ee site delete $this->site_name`" );
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'] ) );
78112
}
79113

80114
$this->le = EE\Utils\get_flag_value( $assoc_args, 'letsencrypt' );
@@ -97,17 +131,16 @@ public function create( $args, $assoc_args ) {
97131
public function info( $args, $assoc_args ) {
98132

99133
EE\Utils\delem_log( 'site info start' );
100-
if ( ! isset( $this->site_name ) ) {
134+
if ( ! isset( $this->site['name'] ) ) {
101135
$args = EE\SiteUtils\auto_site_name( $args, $this->command, __FUNCTION__ );
102136
$this->populate_site_info( $args );
103137
}
104-
$ssl = $this->le ? 'Enabled' : 'Not Enabled';
105-
EE::log( "Details for site $this->site_name:" );
138+
$ssl = $this->le ? 'Enabled' : 'Not Enabled';
106139
$prefix = ( $this->le ) ? 'https://' : 'http://';
107140
$info = [
108-
[ 'Site', $prefix . $this->site_name ],
109-
[ 'Access mailhog', $prefix . $this->site_name . '/ee-admin/mailhog/' ],
110-
[ 'Site Root', $this->site_root ],
141+
[ 'Site', $prefix . $this->site['name'] ],
142+
[ 'Access mailhog', $prefix . $this->site['name'] . '/ee-admin/mailhog/' ],
143+
[ 'Site Root', $this->site['root'] ],
111144
[ 'SSL', $ssl ],
112145
];
113146

@@ -122,25 +155,25 @@ public function info( $args, $assoc_args ) {
122155
*/
123156
private function configure_site_files() {
124157

125-
$site_conf_dir = $this->site_root . '/config';
126-
$site_docker_yml = $this->site_root . '/docker-compose.yml';
127-
$site_conf_env = $this->site_root . '/.env';
158+
$site_conf_dir = $this->site['root'] . '/config';
159+
$site_docker_yml = $this->site['root'] . '/docker-compose.yml';
160+
$site_conf_env = $this->site['root'] . '/.env';
128161
$site_nginx_default_conf = $site_conf_dir . '/nginx/default.conf';
129-
$site_src_dir = $this->site_root . '/app/src';
162+
$site_src_dir = $this->site['root'] . '/app/src';
130163
$process_user = posix_getpwuid( posix_geteuid() );
131164

132-
EE::log( "Creating site $this->site_name." );
165+
EE::log( sprintf( 'Creating site %s.', $this->site['name'] ) );
133166
EE::log( 'Copying configuration files.' );
134167

135168
$filter = [];
136-
$filter[] = $this->site_type;
169+
$filter[] = $this->site['type'];
137170
$filter[] = $this->le;
138171
$site_docker = new Site_Docker();
139172
$docker_compose_content = $site_docker->generate_docker_compose_yml( $filter );
140-
$default_conf_content = $default_conf_content = EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/config/nginx/default.conf.mustache', [ 'server_name' => $this->site_name ] );
173+
$default_conf_content = $default_conf_content = EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/config/nginx/default.conf.mustache', [ 'server_name' => $this->site['name'] ] );
141174

142175
$env_data = [
143-
'virtual_host' => $this->site_name,
176+
'virtual_host' => $this->site['name'],
144177
'user_id' => $process_user['uid'],
145178
'group_id' => $process_user['gid'],
146179
];
@@ -155,13 +188,13 @@ private function configure_site_files() {
155188

156189
$index_data = [
157190
'version' => 'v' . EE_VERSION,
158-
'site_src_root' => $this->site_root . '/app/src',
191+
'site_src_root' => $this->site['root'] . '/app/src',
159192
];
160193
$index_html = EE\Utils\mustache_render( SITE_TEMPLATE_ROOT . '/index.html.mustache', $index_data );
161194
$this->fs->mkdir( $site_src_dir );
162195
$this->fs->dumpFile( $site_src_dir . '/index.html', $index_html );
163196

164-
EE\Siteutils\add_site_redirects( $this->site_name, $this->le );
197+
EE\Siteutils\add_site_redirects( $this->site['name'], $this->le );
165198

166199
EE::success( 'Configuration files copied.' );
167200
} catch ( Exception $e ) {
@@ -174,30 +207,30 @@ private function configure_site_files() {
174207
*/
175208
private function create_site() {
176209

177-
$this->site_root = WEBROOT . $this->site_name;
178-
$this->level = 1;
210+
$this->site['root'] = WEBROOT . $this->site['name'];
211+
$this->level = 1;
179212
try {
180-
EE\Siteutils\create_site_root( $this->site_root, $this->site_name );
213+
EE\Siteutils\create_site_root( $this->site['root'], $this->site['name'] );
181214
$this->level = 2;
182-
EE\Siteutils\setup_site_network( $this->site_name );
215+
EE\Siteutils\setup_site_network( $this->site['name'] );
183216
$this->level = 3;
184217
$this->configure_site_files();
185218

186-
EE\Siteutils\start_site_containers( $this->site_root );
219+
EE\Siteutils\start_site_containers( $this->site['root'] );
187220

188-
EE\Siteutils\create_etc_hosts_entry( $this->site_name );
221+
EE\Siteutils\create_etc_hosts_entry( $this->site['name'] );
189222
if ( ! $this->skip_chk ) {
190223
$this->level = 4;
191-
EE\Siteutils\site_status_check( $this->site_name );
224+
EE\Siteutils\site_status_check( $this->site['name'] );
192225
}
193226
} catch ( Exception $e ) {
194227
$this->catch_clean( $e );
195228
}
196229

197230
if ( $this->le ) {
198-
$this->init_le( $this->site_name, $this->site_root, false );
231+
$this->init_le( $this->site['name'], $this->site['root'], false );
199232
}
200-
$this->info( [ $this->site_name ], [] );
233+
$this->info( [ $this->site['name'] ], [] );
201234
$this->create_site_db_entry();
202235
}
203236

@@ -208,9 +241,9 @@ private function create_site_db_entry() {
208241

209242
$ssl = $this->le ? 1 : 0;
210243
$data = [
211-
'sitename' => $this->site_name,
212-
'site_type' => $this->site_type,
213-
'site_path' => $this->site_root,
244+
'sitename' => $this->site['name'],
245+
'site_type' => $this->site['type'],
246+
'site_path' => $this->site['root'],
214247
'site_command' => $this->command,
215248
'is_ssl' => $ssl,
216249
'created_on' => date( 'Y-m-d H:i:s', time() ),
@@ -232,18 +265,18 @@ private function create_site_db_entry() {
232265
*/
233266
private function populate_site_info( $args ) {
234267

235-
$this->site_name = EE\Utils\remove_trailing_slash( $args[0] );
268+
$this->site['name'] = EE\Utils\remove_trailing_slash( $args[0] );
236269

237-
if ( $this->db::site_in_db( $this->site_name ) ) {
270+
if ( $this->db::site_in_db( $this->site['name'] ) ) {
238271

239-
$db_select = $this->db::select( [], [ 'sitename' => $this->site_name ], 'sites', 1 );
272+
$db_select = $this->db::select( [], [ 'sitename' => $this->site['name'] ], 'sites', 1 );
240273

241-
$this->site_type = $db_select['site_type'];
242-
$this->site_root = $db_select['site_path'];
243-
$this->le = $db_select['is_ssl'];
274+
$this->site['type'] = $db_select['site_type'];
275+
$this->site['root'] = $db_select['site_path'];
276+
$this->le = $db_select['is_ssl'];
244277

245278
} else {
246-
EE::error( "Site $this->site_name does not exist." );
279+
EE::error( sprintf( 'Site %s does not exist.', $this->site['name'] ) );
247280
}
248281
}
249282

@@ -273,7 +306,7 @@ private function catch_clean( $e ) {
273306
EE\Utils\delem_log( 'site cleanup start' );
274307
EE::warning( $e->getMessage() );
275308
EE::warning( 'Initiating clean-up.' );
276-
$this->delete_site( $this->level, $this->site_name, $this->site_root );
309+
$this->delete_site( $this->level, $this->site['name'], $this->site['root'] );
277310
EE\Utils\delem_log( 'site cleanup end' );
278311
exit;
279312
}
@@ -285,7 +318,7 @@ private function rollback() {
285318

286319
EE::warning( 'Exiting gracefully after rolling back. This may take some time.' );
287320
if ( $this->level > 0 ) {
288-
$this->delete_site( $this->level, $this->site_name, $this->site_root );
321+
$this->delete_site( $this->level, $this->site['name'], $this->site['root'] );
289322
}
290323
EE::success( 'Rollback complete. Exiting now.' );
291324
exit;

0 commit comments

Comments
 (0)