Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .distignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.DS_Store
.git
.gitignore
.gitlab-ci.yml
.editorconfig
.travis.yml
behat.yml
circle.yml
bin/
features/
utils/
*.zip
*.tar.gz
*.swp
*.txt
*.log
25 changes: 25 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

# WordPress Coding Standards
# https://make.wordpress.org/core/handbook/coding-standards/

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab

[{.jshintrc,*.json,*.yml,*.feature}]
indent_style = space
indent_size = 2

[{*.txt}]
end_of_line = crlf

[composer.json]
indent_style = space
indent_size = 4
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.DS_Store
node_modules/
vendor/
*.zip
*.tar.gz
*.swp
*.txt
*.log
composer.lock
.idea
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "easyengine/mailhog-command",
"description": "Commanad to enable disable mailhog for sites.",
"type": "ee-cli-package",
"homepage": "https://github.com/easyengine/mailhog-command",
"license": "MIT",
"authors": [],
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"": "src/"
},
"files": [ "mailhog-command.php" ]
},
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
},
"bundled": true,
"commands": [
"mailhog"
]
}
}
2 changes: 2 additions & 0 deletions ee.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require:
- mailhog-command.php
12 changes: 12 additions & 0 deletions mailhog-command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

if ( ! class_exists( 'EE' ) ) {
return;
}

$autoload = dirname( __FILE__ ) . '/vendor/autoload.php';
if ( file_exists( $autoload ) ) {
require_once $autoload;
}

EE::add_command( 'mailhog', 'Mailhog_Command' );
43 changes: 43 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0"?>
<ruleset name="EE">
<description>WordPress Coding Standards for EE</description>

<!-- Show sniff codes in all reports, and progress while running -->
<arg value="sp"/>
<!-- Check all PHP files in directory tree by default. -->
<arg name="extensions" value="php"/>
<!-- Run different reports -->
<arg name="report" value="full"/>
<arg name="report" value="summary"/>
<arg name="report" value="source"/>

<file>.</file>
<exclude-pattern>*/ci/*</exclude-pattern>
<exclude-pattern>*/features/*</exclude-pattern>
<exclude-pattern>*/packages/*</exclude-pattern>
<exclude-pattern>*/tests/*</exclude-pattern>
<exclude-pattern>*/utils/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>

<rule ref="PHPCompatibility">
<!-- Polyfill package is used so array_column() is available for PHP 5.4- -->
<exclude name="PHPCompatibility.PHP.NewFunctions.array_columnFound"/>
<!-- Both magic quotes directives set in wp-settings-cli.php to provide consistent starting point. -->
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_runtimeDeprecatedRemoved"/>
<exclude name="PHPCompatibility.PHP.DeprecatedIniDirectives.magic_quotes_sybaseDeprecatedRemoved"/>
</rule>
<config name="testVersion" value="7.0-"/>

<rule ref="WordPress-Core">
<exclude name="Squiz.PHP.DisallowMultipleAssignments.Found" />
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar" />
<exclude name="WordPress.NamingConventions.ValidVariableName.MemberNotSnakeCase" />
<exclude name="WordPress.NamingConventions.ValidVariableName.NotSnakeCase" />
</rule>
<rule ref="WordPress.Files.FileName">
<properties>
<property name="strict_class_file_names" value="false"/>
</properties>
<exclude name="WordPress.Files.FileName.NotHyphenatedLowercase" />
</rule>
</ruleset>
130 changes: 130 additions & 0 deletions src/Mailhog_Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

/**
* Enables/Disables admin-tools on a site.
*
* ## EXAMPLES
*
* # Enable admin tools on site
* $ ee admin-tools up example.com
*
* @package ee-cli
*/

use EE\Model\Site;
use \Symfony\Component\Filesystem\Filesystem;

class Mailhog_Command extends EE_Command {

/**
* @var array $db Object containing essential site related information.
*/
private $site_data;

/**
* Enables mailhog on given site.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of website to enable mailhog on.
*/
public function up( $args, $assoc_args ) {

EE\Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' );
$args = EE\SiteUtils\auto_site_name( $args, 'mailhog', __FUNCTION__ );
$this->site_data = Site::find( EE\Utils\remove_trailing_slash( $args[0] ) );
if ( ! $this->site_data || ! $this->site_data->site_enabled ) {
EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) );
}

if ( $this->mailhog_enabled() ) {
EE::error( 'Mailhog is already up.' );
}
EE::docker()::docker_compose_up( $this->site_data->site_fs_path, [ 'mailhog' ] );
EE::exec( "docker-compose exec postfix postconf -e 'relayhost = mailhog:1025'" );
EE::success( sprintf( 'Mailhog enabled for %s site', $this->site_data->site_url ) );
}

/**
* Disables mailhog on given site.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of website to disable mailhog on.
*/
public function down( $args, $assoc_args ) {

EE\Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' );
$args = EE\SiteUtils\auto_site_name( $args, 'mailhog', __FUNCTION__ );
$this->site_data = Site::find( EE\Utils\remove_trailing_slash( $args[0] ) );
if ( ! $this->site_data || ! $this->site_data->site_enabled ) {
EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) );
}

if ( ! $this->mailhog_enabled() ) {
EE::error( 'Mailhog is already down.' );
}
EE::exec( 'docker-compose stop mailhog' );
EE::exec( 'docker-compose exec postfix postconf -e \'relayhost =\'' );
EE::success( sprintf( 'Mailhog disabled for %s site', $this->site_data->site_url ) );
}

/**
* Outputs status of mailhog for a site.
*
* ## OPTIONS
*
* [<site-name>]
* : Name of website to know mailhog status for.
*/
public function status( $args, $assoc_args ) {

EE\Utils\delem_log( 'mailhog' . __FUNCTION__ . ' start' );
$args = EE\SiteUtils\auto_site_name( $args, 'mailhog', __FUNCTION__ );
$this->site_data = Site::find( EE\Utils\remove_trailing_slash( $args[0] ) );
if ( ! $this->site_data || ! $this->site_data->site_enabled ) {
EE::error( sprintf( 'Site %s does not exist / is not enabled.', $args[0] ) );
}

if ( $this->mailhog_enabled() ) {
EE::log( sprintf( 'Mailhog is UP for %s site.', $this->site_data->site_url ) );
} else {
EE::log( sprintf( 'Mailhog is DOWN for %s site.', $this->site_data->site_url ) );
}
}

/**
* Function to check if mailhog is present in docker-compose.yml or not.
*/
private function check_mailhog_available() {

chdir( $this->site_data->site_fs_path );
$launch = EE::launch( 'docker-compose config --services' );
$services = explode( PHP_EOL, trim( $launch->stdout ) );
if ( ! in_array( 'mailhog', $services, true ) ) {
EE::debug( 'Site type: ' . $this->site_data->site_type );
EE::debug( 'Site command: ' . $this->site_data->app_sub_type );
EE::error( sprintf( '%s site does not have support to enable/disable mailhog.' ) );
}
}

/**
* Function to check if mailhog is possible for the site-type and is enabled or not.
*
* @return bool Status of mailhog enabled or not.
*/
private function mailhog_enabled() {

$this->check_mailhog_available();
$launch = EE::launch( 'docker-compose ps -q mailhog' );
$id = trim( $launch->stdout );
if ( empty( $id ) ) {
return false;
}

return 'running' === EE::docker()::container_status( $id );
}

}