Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor for --type support of ee site command #104

Merged
merged 41 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9030635
Move AcmePhp from core to site-command
mrrobot47 Aug 28, 2018
9dcdd84
Move site related files from core to site-command
mrrobot47 Aug 28, 2018
ab8c4d0
Update composer dependencies
mrrobot47 Aug 28, 2018
74dbc3a
Update namespaces and change Site_Command to HTML site-type
mrrobot47 Aug 28, 2018
106134c
Use constants where applicable
mrrobot47 Aug 28, 2018
7dbb02f
Add site-type routing in site-command
mrrobot47 Aug 28, 2018
2535117
add help hook for site-type and register html site-type
mrrobot47 Aug 28, 2018
a2cbf6a
Move template from core to site-command
mrrobot47 Aug 28, 2018
b947d24
Update template path
mrrobot47 Aug 28, 2018
81e5159
Update namespace and filenames
mrrobot47 Aug 28, 2018
1cf1d40
Update namespace and filenames
mrrobot47 Aug 28, 2018
b8ff0f0
Load site utility function file
mrrobot47 Aug 28, 2018
2976ebe
Add type namespace in shutdownhandler and letsencrypt class
mrrobot47 Aug 28, 2018
a7382df
Remove unnecessary namespace
mrrobot47 Aug 28, 2018
da2cb12
Add type namespace to Abstract class
mrrobot47 Aug 28, 2018
ebd3eff
Add type namespace to letsencrypt class
mrrobot47 Aug 28, 2018
0178748
Update EE usage to \EE acc to new namespace
mrrobot47 Aug 28, 2018
a8af7f1
Update getting of args in help and set default type
mrrobot47 Aug 28, 2018
5df0eaf
Update EE usage to \EE for letsencrypt class
mrrobot47 Aug 28, 2018
000c413
Update execeptions according to namespace changes
mrrobot47 Aug 28, 2018
a7f5d88
Merge branch 'develop' into refactor-for-type
mrrobot47 Aug 29, 2018
c9590a5
Move hooks to helper
mrrobot47 Aug 29, 2018
f5d6cce
Reorganize files to helper and site-type folders
mrrobot47 Aug 29, 2018
eaefba4
Move use statement to hooks file
mrrobot47 Aug 29, 2018
828e731
Update doc comment
mrrobot47 Aug 29, 2018
ed5c6e0
Update warning message
mrrobot47 Aug 29, 2018
eb2e1e4
Update sprintf
mrrobot47 Aug 29, 2018
69b6976
Remove TODO from code to track in seperate issue
mrrobot47 Aug 29, 2018
4d9dc92
Update hook name and doc comment
mrrobot47 Aug 29, 2018
4f66fc2
Add early return in help hook
mrrobot47 Aug 29, 2018
75e55f7
Rename html class file
mrrobot47 Aug 29, 2018
6dd18f8
Update help info in doc block
mrrobot47 Aug 29, 2018
18c462f
Update path for require
mrrobot47 Aug 29, 2018
077316c
Move shutdownfunction and common rollback to abstract class
mrrobot47 Aug 29, 2018
b5c167d
Move file loading to composer
mrrobot47 Aug 29, 2018
e408a92
Update loading sequence
mrrobot47 Aug 29, 2018
72444d8
Change hook addition
mrrobot47 Aug 29, 2018
eb15ce5
Remove test trigger of error
mrrobot47 Aug 29, 2018
a313af6
Update hooks file to add hooks
mrrobot47 Aug 29, 2018
482b4f8
Update sprintf usage
mrrobot47 Aug 30, 2018
cd38bc7
Update function name
mrrobot47 Aug 30, 2018
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
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
"": "src/",
"AcmePhp\\Cli\\": "AcmePhp/Cli"
},
"files": [ "site-command.php" ]
"files": [
"src/helper/hooks.php",
"src/helper/site-utils.php",
"site-command.php"
]
},
"extra": {
"branch-alias": {
Expand Down
6 changes: 0 additions & 6 deletions site-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,5 @@
require_once $autoload;
}

// Load utility functions.
require_once 'src/helper/site-utils.php';

// Load hooks.
require_once 'src/helper/hooks.php';

EE::add_command( 'site', 'Site_Command' );
Site_Command::add_site_type( 'html', 'EE\Site\Type\HTML' );
11 changes: 3 additions & 8 deletions src/Site_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function instance() {
public static function add_site_type( $name, $callback ) {

if ( isset( self::$instance->site_types[ $name ] ) ) {
EE::warning( sprintf( '%s site-type has already been previously registered by %s. It will be over-written by the new package class %s. Please update your packages to resolve this.', $name, self::$instance->site_types[ $name ], $callback ) );
EE::warning( sprintf( '%s site-type had already been previously registered by %s. It is overridden by the new package class %s. Please update your packages to resolve this.', $name, self::$instance->site_types[ $name ], $callback ) );
}
self::$instance->site_types[ $name ] = $callback;
}
Expand All @@ -53,6 +53,7 @@ public static function get_site_types() {
}

/**
* Performs site operations. Check `ee help site` for more info.
* Invoked function of site-type routing. Called when `ee site` is invoked.
* Performs the routing to respective site-type passed using either `--type=`,
* Or discovers the type from the site-name and fetches the type from it,
Expand All @@ -73,9 +74,7 @@ public function __invoke( $args, $assoc_args ) {

if ( ! isset( $site_types[ $type ] ) ) {
$error = sprintf(
"'%s' is not a registered site type of 'ee site --type=%s'. See 'ee help site --type=%s' for available subcommands.",
$type,
$type,
'\'%1$s\' is not a registered site type of \'ee site --type=%1$s\'. See \'ee help site --type=%1$s\' for available subcommands.',
$type
);
EE::error( $error );
Expand Down Expand Up @@ -106,10 +105,6 @@ private function determine_type( $args ) {
// default site-type
$type = 'html';

// TODO: get type from config file as below
// $config_type = EE::get_config('type');
// $type = empty( $config_type ) ? 'html' : $config_type;

$last_arg = array_pop( $args );
if ( substr( $last_arg, 0, 4 ) === 'http' ) {
$last_arg = str_replace( [ 'https://', 'http://' ], '', $last_arg );
Expand Down
26 changes: 26 additions & 0 deletions src/helper/class-ee-site.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ abstract class EE_Site_Command {
private $site;

public function __construct() {

pcntl_signal( SIGTERM, [ $this, "rollback" ] );
pcntl_signal( SIGHUP, [ $this, "rollback" ] );
pcntl_signal( SIGUSR1, [ $this, "rollback" ] );
pcntl_signal( SIGINT, [ $this, "rollback" ] );
$shutdown_handler = new Shutdown_Handler();
register_shutdown_function( [ $shutdown_handler, "cleanup" ], [ &$this ] );
}

/**
Expand Down Expand Up @@ -538,6 +545,25 @@ private function populate_site_info( $args ) {
}
}

/**
* Shutdown function to catch and rollback from fatal errors.
*/
protected function shutDownFunction() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this function name in camlecase?


$logger = \EE::get_file_logger()->withName( 'site-command' );
$error = error_get_last();
if ( isset( $error ) && $error['type'] === E_ERROR ) {
\EE::warning( 'An Error occurred. Initiating clean-up.' );
$logger->error( 'Type: ' . $error['type'] );
$logger->error( 'Message: ' . $error['message'] );
$logger->error( 'File: ' . $error['file'] );
$logger->error( 'Line: ' . $error['line'] );
$this->rollback();
}
}

abstract public function create( $args, $assoc_args );

abstract protected function rollback();

}
66 changes: 38 additions & 28 deletions src/helper/hooks.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
<?php

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

use EE\Dispatcher\CommandFactory;

/**
* Add hook before the invocation of help command to appropriately handle the help for given site-type.
* Callback function of `before_invoke:help` hook: Add routing for "ee help site" command before the invocation of help
* command.
*
* @param array $args Commandline arguments passed to help command.
* @param array $assoc_args Associative arguments passed to help command.
*/
function Before_Help_Command( $args, $assoc_args ) {

if ( isset( $args[0] ) && 'site' === $args[0] ) {
$site_types = Site_Command::get_site_types();
if ( isset( $assoc_args['type'] ) ) {
$type = $assoc_args['type'];
} else {
//TODO: get from config.
$type = 'html';
}

if ( isset( $site_types[ $type ] ) ) {
$callback = $site_types[ $type ];

$command = EE::get_root_command();
$leaf_command = CommandFactory::create( 'site', $callback, $command );
$command->add_subcommand( 'site', $leaf_command );
} else {
$error = sprintf(
"'%s' is not a registered site type of 'ee site --type=%s'. See 'ee help site --type=%s' for available subcommands.",
$type,
$type,
$type
);
EE::error( $error );
}
function ee_site_help_cmd_routing( $args, $assoc_args ) {

if ( ( ! isset( $args[0] ) ) || ( 'site' !== $args[0] ) ) {
return;
}

$site_types = Site_Command::get_site_types();
if ( isset( $assoc_args['type'] ) ) {
$type = $assoc_args['type'];
} else {
$type = 'html';
}

if ( isset( $site_types[ $type ] ) ) {
$callback = $site_types[ $type ];

$command = EE::get_root_command();
$leaf_command = CommandFactory::create( 'site', $callback, $command );
$command->add_subcommand( 'site', $leaf_command );
} else {
$error = sprintf(
"'%s' is not a registered site type of 'ee site --type=%s'. See 'ee help site --type=%s' for available subcommands.",
$type,
$type,
$type
);
EE::error( $error );
}

}

EE::add_hook( 'before_invoke:help', 'Before_Help_Command' );
EE::add_hook( 'before_invoke:help', 'ee_site_help_cmd_routing' );
28 changes: 4 additions & 24 deletions src/site-type/HTML.php → src/site-type/html.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,10 @@ class HTML extends EE_Site_Command {

public function __construct() {

$this->level = 0;
pcntl_signal( SIGTERM, [ $this, "rollback" ] );
pcntl_signal( SIGHUP, [ $this, "rollback" ] );
pcntl_signal( SIGUSR1, [ $this, "rollback" ] );
pcntl_signal( SIGINT, [ $this, "rollback" ] );
$shutdown_handler = new Shutdown_Handler();
register_shutdown_function( [ $shutdown_handler, "cleanup" ], [ &$this ] );
parent::__construct();
$this->level = 0;
$this->docker = \EE::docker();
$this->logger = \EE::get_file_logger()->withName( 'site_command' );
$this->logger = \EE::get_file_logger()->withName( 'html_type' );
$this->fs = new Filesystem();
}

Expand Down Expand Up @@ -329,7 +324,7 @@ private function catch_clean( $e ) {
/**
* Roll back on interrupt.
*/
private function rollback() {
protected function rollback() {

\EE::warning( 'Exiting gracefully after rolling back. This may take some time.' );
if ( $this->level > 0 ) {
Expand All @@ -339,19 +334,4 @@ private function rollback() {
exit;
}

/**
* Shutdown function to catch and rollback from fatal errors.
*/
private function shutDownFunction() {

$error = error_get_last();
if ( isset( $error ) && $error['type'] === E_ERROR ) {
\EE::warning( 'An Error occurred. Initiating clean-up.' );
$this->logger->error( 'Type: ' . $error['type'] );
$this->logger->error( 'Message: ' . $error['message'] );
$this->logger->error( 'File: ' . $error['file'] );
$this->logger->error( 'Line: ' . $error['line'] );
$this->rollback();
}
}
}