Skip to content

Commit 39cd066

Browse files
committed
Merge branch 'develop'
2 parents f8e9b80 + c1dbc5c commit 39cd066

8 files changed

Lines changed: 268 additions & 0 deletions

File tree

.distignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.DS_Store
2+
.git
3+
.gitignore
4+
.gitlab-ci.yml
5+
.editorconfig
6+
.travis.yml
7+
behat.yml
8+
circle.yml
9+
bin/
10+
features/
11+
utils/
12+
*.zip
13+
*.tar.gz
14+
*.swp
15+
*.txt
16+
*.log

.editorconfig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
# WordPress Coding Standards
5+
# https://make.wordpress.org/core/handbook/coding-standards/
6+
7+
root = true
8+
9+
[*]
10+
charset = utf-8
11+
end_of_line = lf
12+
insert_final_newline = true
13+
trim_trailing_whitespace = true
14+
indent_style = tab
15+
16+
[{.jshintrc,*.json,*.yml,*.feature}]
17+
indent_style = space
18+
indent_size = 2
19+
20+
[{*.txt}]
21+
end_of_line = crlf
22+
23+
[composer.json]
24+
indent_style = space
25+
indent_size = 4

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.DS_Store
2+
node_modules/
3+
vendor/
4+
*.zip
5+
*.tar.gz
6+
*.swp
7+
*.txt
8+
*.log
9+
composer.lock
10+
.idea

composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "easyengine/mailhog-command",
3+
"description": "Commanad to enable disable mailhog for sites.",
4+
"type": "ee-cli-package",
5+
"homepage": "https://github.com/easyengine/mailhog-command",
6+
"license": "MIT",
7+
"authors": [],
8+
"minimum-stability": "dev",
9+
"prefer-stable": true,
10+
"autoload": {
11+
"psr-4": {
12+
"": "src/"
13+
},
14+
"files": [ "mailhog-command.php" ]
15+
},
16+
"extra": {
17+
"branch-alias": {
18+
"dev-master": "1.x-dev"
19+
},
20+
"bundled": true,
21+
"commands": [
22+
"mailhog"
23+
]
24+
}
25+
}

ee.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require:
2+
- mailhog-command.php

mailhog-command.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
if ( ! class_exists( 'EE' ) ) {
4+
return;
5+
}
6+
7+
$autoload = dirname( __FILE__ ) . '/vendor/autoload.php';
8+
if ( file_exists( $autoload ) ) {
9+
require_once $autoload;
10+
}
11+
12+
EE::add_command( 'mailhog', 'Mailhog_Command' );

phpcs.xml.dist

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="EE">
3+
<description>WordPress Coding Standards for EE</description>
4+
5+
<!-- Show sniff codes in all reports, and progress while running -->
6+
<arg value="sp"/>
7+
<!-- Check all PHP files in directory tree by default. -->
8+
<arg name="extensions" value="php"/>
9+
<!-- Run different reports -->
10+
<arg name="report" value="full"/>
11+
<arg name="report" value="summary"/>
12+
<arg name="report" value="source"/>
13+
14+
<file>.</file>
15+
<exclude-pattern>*/ci/*</exclude-pattern>
16+
<exclude-pattern>*/features/*</exclude-pattern>
17+
<exclude-pattern>*/packages/*</exclude-pattern>
18+
<exclude-pattern>*/tests/*</exclude-pattern>
19+
<exclude-pattern>*/utils/*</exclude-pattern>
20+
<exclude-pattern>*/vendor/*</exclude-pattern>
21+
22+
<rule ref="PHPCompatibility">
23+
<!-- Polyfill package is used so array_column() is available for PHP 5.4- -->
24+
<exclude name="PHPCompatibility.PHP.NewFunctions.array_columnFound"/>
25+
<!-- Both magic quotes directives set in wp-settings-cli.php to provide consistent starting point. -->
26+
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_runtimeDeprecatedRemoved"/>
27+
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_sybaseDeprecatedRemoved"/>
28+
</rule>
29+
<config name="testVersion" value="7.0-"/>
30+
31+
<rule ref="WordPress-Core">
32+
<exclude name="Squiz.PHP.DisallowMultipleAssignments.Found" />
33+
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar" />
34+
<exclude name="WordPress.NamingConventions.ValidVariableName.MemberNotSnakeCase" />
35+
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCase" />
36+
</rule>
37+
<rule ref="WordPress.Files.FileName">
38+
<properties>
39+
<property name="strict_class_file_names" value="false"/>
40+
</properties>
41+
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase" />
42+
</rule>
43+
</ruleset>

src/Mailhog_Command.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
4+
use EE\Model\Site;
5+
use function EE\Site\Utils\auto_site_name;
6+
7+
/**
8+
* Enables/Disables admin-tools on a site.
9+
*
10+
* ## EXAMPLES
11+
*
12+
* # Enable admin tools on site
13+
* $ ee admin-tools up example.com
14+
*
15+
* @package ee-cli
16+
*/
17+
class Mailhog_Command extends EE_Command {
18+
19+
/**
20+
* @var array $db Object containing essential site related information.
21+
*/
22+
private $site_data;
23+
24+
/**
25+
* Enables mailhog on given site.
26+
*
27+
* ## OPTIONS
28+
*
29+
* [<site-name>]
30+
* : Name of website to enable mailhog on.
31+
*/
32+
public function up( $args, $assoc_args ) {
33+
34+
\EE\Auth\Utils\init_global_admin_tools_auth();
35+
36+
EE\Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' );
37+
$args = auto_site_name( $args, 'mailhog', __FUNCTION__ );
38+
$this->site_data = Site::find( EE\Utils\remove_trailing_slash( $args[0] ) );
39+
if ( ! $this->site_data || ! $this->site_data->site_enabled ) {
40+
EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) );
41+
}
42+
43+
if ( $this->mailhog_enabled() ) {
44+
EE::error( 'Mailhog is already up.' );
45+
}
46+
EE::docker()::docker_compose_up( $this->site_data->site_fs_path, [ 'mailhog' ] );
47+
EE::exec( "docker-compose exec postfix postconf -e 'relayhost = mailhog:1025'" );
48+
EE::exec( 'docker-compose restart postfix' );
49+
EE::success( sprintf( 'Mailhog enabled for %s site', $this->site_data->site_url ) );
50+
}
51+
52+
/**
53+
* Disables mailhog on given site.
54+
*
55+
* ## OPTIONS
56+
*
57+
* [<site-name>]
58+
* : Name of website to disable mailhog on.
59+
*/
60+
public function down( $args, $assoc_args ) {
61+
62+
EE\Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' );
63+
$args = auto_site_name( $args, 'mailhog', __FUNCTION__ );
64+
$this->site_data = Site::find( EE\Utils\remove_trailing_slash( $args[0] ) );
65+
if ( ! $this->site_data || ! $this->site_data->site_enabled ) {
66+
EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) );
67+
}
68+
69+
if ( ! $this->mailhog_enabled() ) {
70+
EE::error( 'Mailhog is already down.' );
71+
}
72+
EE::exec( 'docker-compose stop mailhog' );
73+
EE::exec( 'docker-compose exec postfix postconf -e \'relayhost =\'' );
74+
EE::exec( 'docker-compose restart postfix' );
75+
EE::success( sprintf( 'Mailhog disabled for %s site', $this->site_data->site_url ) );
76+
}
77+
78+
/**
79+
* Outputs status of mailhog for a site.
80+
*
81+
* ## OPTIONS
82+
*
83+
* [<site-name>]
84+
* : Name of website to know mailhog status for.
85+
*/
86+
public function status( $args, $assoc_args ) {
87+
88+
EE\Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' );
89+
$args = auto_site_name( $args, 'mailhog', __FUNCTION__ );
90+
$this->site_data = Site::find( EE\Utils\remove_trailing_slash( $args[0] ) );
91+
if ( ! $this->site_data || ! $this->site_data->site_enabled ) {
92+
EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) );
93+
}
94+
95+
if ( $this->mailhog_enabled() ) {
96+
EE::log( sprintf( 'Mailhog is UP for %s site.', $this->site_data->site_url ) );
97+
} else {
98+
EE::log( sprintf( 'Mailhog is DOWN for %s site.', $this->site_data->site_url ) );
99+
}
100+
}
101+
102+
/**
103+
* Function to check if mailhog is present in docker-compose.yml or not.
104+
*/
105+
private function check_mailhog_available() {
106+
107+
chdir( $this->site_data->site_fs_path );
108+
$launch = EE::launch( 'docker-compose config --services' );
109+
$services = explode( PHP_EOL, trim( $launch->stdout ) );
110+
if ( in_array( 'mailhog', $services, true ) ) {
111+
return;
112+
}
113+
EE::debug( 'Site type: ' . $this->site_data->site_type );
114+
EE::debug( 'Site command: ' . $this->site_data->app_sub_type );
115+
EE::error( sprintf( '%s site does not have support to enable/disable mailhog.', $this->site_data->site_url ) );
116+
}
117+
118+
/**
119+
* Function to check if mailhog is possible for the site-type and is enabled or not.
120+
*
121+
* @return bool Status of mailhog enabled or not.
122+
*/
123+
private function mailhog_enabled() {
124+
125+
$this->check_mailhog_available();
126+
$launch = EE::launch( 'docker-compose ps -q mailhog' );
127+
$id = trim( $launch->stdout );
128+
if ( empty( $id ) ) {
129+
return false;
130+
}
131+
132+
return 'running' === EE::docker()::container_status( $id );
133+
}
134+
135+
}

0 commit comments

Comments
 (0)