From d02733b4b39d5d26706dda50ae8773c72ec647c1 Mon Sep 17 00:00:00 2001 From: Abhijit Rakas Date: Wed, 28 Nov 2018 17:58:08 +0530 Subject: [PATCH 1/2] Fix code structure of help and cli command --- php/commands/help.php | 3 ++- php/commands/src/CLI_Command.php | 33 ++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/php/commands/help.php b/php/commands/help.php index ca0c53cff..8750fee87 100644 --- a/php/commands/help.php +++ b/php/commands/help.php @@ -2,6 +2,7 @@ use \EE\Utils; use \EE\Dispatcher; +use cli\Shell; class Help_Command extends EE_Command { @@ -57,7 +58,7 @@ private static function show_help( $command ) { // Need to de-tab for wordwrapping to work properly. $out = str_replace( "\t", $tab, $out ); - $wordwrap_width = \cli\Shell::columns(); + $wordwrap_width = Shell::columns(); // Wordwrap with indent. $out = preg_replace_callback( diff --git a/php/commands/src/CLI_Command.php b/php/commands/src/CLI_Command.php index 6ab58af42..1a5d6284c 100644 --- a/php/commands/src/CLI_Command.php +++ b/php/commands/src/CLI_Command.php @@ -4,6 +4,11 @@ use EE\Model\Site; use EE\Utils; use Symfony\Component\Filesystem\Filesystem; +use cli\Table; +use cli\table\Ascii; +use EE\Formatter; +use EE\Process; +use EE\Completions; /** * Review current EE info, check for updates, or see defined aliases. @@ -116,7 +121,7 @@ public function info( $_, $assoc_args ) { if ( ! is_dir( $packages_dir ) ) { $packages_dir = null; } - if ( \EE\Utils\get_flag_value( $assoc_args, 'format' ) === 'json' ) { + if ( Utils\get_flag_value( $assoc_args, 'format' ) === 'json' ) { $info = array( 'php_binary_path' => $php_bin, 'global_config_path' => $runner->global_config_path, @@ -146,12 +151,12 @@ public function info( $_, $assoc_args ) { array( 'EE version', EE_VERSION ), ); - $info_table = new \cli\Table(); + $info_table = new Table(); $info_table->setRows( $info ); - $info_table->setRenderer( new \cli\table\Ascii() ); + $info_table->setRenderer( new Ascii() ); $lines = array_slice( $info_table->getDisplayLines(), 2 ); foreach ( $lines as $line ) { - \EE::line( $line ); + EE::line( $line ); } } } @@ -211,7 +216,7 @@ public function check_update( $_, $assoc_args ) { $updates = $this->get_updates( $assoc_args ); if ( $updates ) { - $formatter = new \EE\Formatter( + $formatter = new Formatter( $assoc_args, array( 'version', 'update_type', 'package_url' ) ); @@ -288,7 +293,7 @@ public function update( $_, $assoc_args ) { } EE::get_runner()->check_requirements(); EE::log( sprintf( 'Downloading from %s...', $download_url ) ); - $temp = \EE\Utils\get_temp_dir() . uniqid( 'ee_', true ) . '.phar'; + $temp = Utils\get_temp_dir() . uniqid( 'ee_', true ) . '.phar'; $headers = array(); $options = array( 'timeout' => 600, // 10 minutes ought to be enough for everybody. @@ -310,7 +315,7 @@ public function update( $_, $assoc_args ) { EE::log( 'Updating EasyEngine to new version. This might take some time.' ); $php_binary = Utils\get_php_binary(); - $process = EE\Process::create( "{$php_binary} $temp cli info", null, null ); + $process = Process::create( "{$php_binary} $temp cli info", null, null ); $result = $process->run(); if ( 0 !== $result->return_code || false === stripos( $result->stdout, 'EE version' ) ) { $multi_line = explode( PHP_EOL, $result->stderr ); @@ -386,7 +391,7 @@ private function get_updates( $assoc_args ) { } } foreach ( array( 'major', 'minor', 'patch' ) as $type ) { - if ( true === \EE\Utils\get_flag_value( $assoc_args, $type ) ) { + if ( true === Utils\get_flag_value( $assoc_args, $type ) ) { return ! empty( $updates[$type] ) ? array( $updates[$type] ) : false; } } @@ -446,10 +451,10 @@ private function get_updates( $assoc_args ) { * @subcommand param-dump */ public function param_dump( $_, $assoc_args ) { - $spec = \EE::get_configurator()->get_spec(); + $spec = EE::get_configurator()->get_spec(); - if ( \EE\Utils\get_flag_value( $assoc_args, 'with-values' ) ) { - $config = \EE::get_configurator()->to_array(); + if ( Utils\get_flag_value( $assoc_args, 'with-values' ) ) { + $config = EE::get_configurator()->to_array(); // Copy current config values to $spec foreach ( $spec as $key => $value ) { $current = null; @@ -460,7 +465,7 @@ public function param_dump( $_, $assoc_args ) { } } - if ( 'var_export' === \EE\Utils\get_flag_value( $assoc_args, 'format' ) ) { + if ( 'var_export' === Utils\get_flag_value( $assoc_args, 'format' ) ) { var_export( $spec ); } else { echo json_encode( $spec ); @@ -541,7 +546,7 @@ public function self_uninstall( $args, $assoc_args ) { */ public function completions( $_, $assoc_args ) { $line = substr( $assoc_args['line'], 0, $assoc_args['point'] ); - $compl = new \EE\Completions( $line ); + $compl = new Completions( $line ); $compl->render(); } @@ -590,7 +595,7 @@ public function alias( $_, $assoc_args ) { private function get_update_type_str( $assoc_args ) { $update_type = ' '; foreach ( array( 'major', 'minor', 'patch' ) as $type ) { - if ( true === \EE\Utils\get_flag_value( $assoc_args, $type ) ) { + if ( true === Utils\get_flag_value( $assoc_args, $type ) ) { $update_type = ' ' . $type . ' '; break; } From 30dd6d0f5340f7af76e04e387723a9e78f774968 Mon Sep 17 00:00:00 2001 From: Abhijit Rakas Date: Thu, 29 Nov 2018 13:10:41 +0530 Subject: [PATCH 2/2] Fix code structure --- php/EE/Bootstrap/AutoloaderStep.php | 4 +- php/EE/Bootstrap/IncludePackageAutoloader.php | 6 ++- php/EE/Bootstrap/InitializeLogger.php | 4 +- php/EE/Bootstrap/RegisterDeferredCommands.php | 8 +-- .../Bootstrap/RegisterFrameworkCommands.php | 7 ++- php/EE/Bootstrap/RunnerInstance.php | 4 +- php/EE/Completions.php | 12 +++-- php/EE/Configurator.php | 7 +-- php/EE/Dispatcher/CommandFactory.php | 33 +++++++----- php/EE/Dispatcher/CommandNamespace.php | 8 +-- php/EE/Dispatcher/CompositeCommand.php | 13 ++--- php/EE/Dispatcher/Subcommand.php | 25 +++++---- php/EE/Fetchers/Base.php | 6 ++- php/EE/FileCache.php | 8 +-- php/EE/Formatter.php | 36 +++++++------ php/EE/Iterators/CSV.php | 4 +- php/EE/Loggers/Base.php | 4 +- php/EE/Loggers/Execution.php | 6 ++- php/EE/Loggers/Quiet.php | 8 +-- php/EE/Loggers/Regular.php | 6 ++- php/EE/Migration/Base.php | 3 +- php/EE/Migration/Containers.php | 17 +++--- .../Migration/CustomContainerMigrations.php | 4 +- php/EE/Migration/Executor.php | 4 +- php/EE/Migration/GlobalContainers.php | 10 ++-- php/EE/Migration/SiteContainers.php | 7 +-- php/EE/Model/Migration.php | 4 +- php/EE/Process.php | 5 +- php/EE/RevertableStepProcessor.php | 3 +- php/EE/Runner.php | 11 ++-- php/class-ee-db.php | 4 +- php/class-ee.php | 21 ++++---- php/utils.php | 53 +++++++++++-------- 33 files changed, 213 insertions(+), 142 deletions(-) diff --git a/php/EE/Bootstrap/AutoloaderStep.php b/php/EE/Bootstrap/AutoloaderStep.php index 275c6d03f..ecaa59c3f 100644 --- a/php/EE/Bootstrap/AutoloaderStep.php +++ b/php/EE/Bootstrap/AutoloaderStep.php @@ -2,6 +2,8 @@ namespace EE\Bootstrap; +use EE; + /** * Abstract class AutoloaderStep. * @@ -42,7 +44,7 @@ public function process( BootstrapState $state ) { require $autoloader_path; $found_autoloader = true; } catch ( \Exception $exception ) { - \EE::warning( + EE::warning( "Failed to load autoloader '{$autoloader_path}'. Reason: " . $exception->getMessage() ); diff --git a/php/EE/Bootstrap/IncludePackageAutoloader.php b/php/EE/Bootstrap/IncludePackageAutoloader.php index 2fdbca737..f304ce5b1 100644 --- a/php/EE/Bootstrap/IncludePackageAutoloader.php +++ b/php/EE/Bootstrap/IncludePackageAutoloader.php @@ -2,6 +2,8 @@ namespace EE\Bootstrap; +use EE; + /** * Class IncludePackageAutoloader. * @@ -33,7 +35,7 @@ protected function get_autoloader_paths() { $autoloader_path = $runner()->get_packages_dir_path() . 'vendor/autoload.php'; if ( is_readable( $autoloader_path ) ) { - \EE::debug( + EE::debug( 'Loading packages from: ' . $autoloader_path, 'bootstrap' ); @@ -52,6 +54,6 @@ protected function get_autoloader_paths() { * @return void */ protected function handle_failure() { - \EE::debug( 'No package autoload found to load.', 'bootstrap' ); + EE::debug( 'No package autoload found to load.', 'bootstrap' ); } } diff --git a/php/EE/Bootstrap/InitializeLogger.php b/php/EE/Bootstrap/InitializeLogger.php index 2a209a2cd..d84872e02 100644 --- a/php/EE/Bootstrap/InitializeLogger.php +++ b/php/EE/Bootstrap/InitializeLogger.php @@ -2,6 +2,8 @@ namespace EE\Bootstrap; +use DirectoryIterator; + /** * Class InitializeLogger. * @@ -31,7 +33,7 @@ public function process( BootstrapState $state ) { */ private function declare_loggers() { $logger_dir = EE_ROOT . '/php/EE/Loggers'; - $iterator = new \DirectoryIterator( $logger_dir ); + $iterator = new DirectoryIterator( $logger_dir ); // Make sure the base class is declared first. include_once "$logger_dir/Base.php"; diff --git a/php/EE/Bootstrap/RegisterDeferredCommands.php b/php/EE/Bootstrap/RegisterDeferredCommands.php index aab103167..6e576b3f7 100644 --- a/php/EE/Bootstrap/RegisterDeferredCommands.php +++ b/php/EE/Bootstrap/RegisterDeferredCommands.php @@ -2,6 +2,8 @@ namespace EE\Bootstrap; +use EE; + /** * Class RegisterDeferredCommands. * @@ -27,7 +29,7 @@ public function process( BootstrapState $state ) { // Process deferred command additions for commands added through // plugins. - \EE::add_hook( + EE::add_hook( 'find_command_to_run_pre', array( $this, 'add_deferred_commands' ) ); @@ -39,10 +41,10 @@ public function process( BootstrapState $state ) { * Add deferred commands that are still waiting to be processed. */ public function add_deferred_commands() { - $deferred_additions = \EE::get_deferred_additions(); + $deferred_additions = EE::get_deferred_additions(); foreach ( $deferred_additions as $name => $addition ) { - \EE::add_command( + EE::add_command( $name, $addition['callable'], $addition['args'] diff --git a/php/EE/Bootstrap/RegisterFrameworkCommands.php b/php/EE/Bootstrap/RegisterFrameworkCommands.php index 7814accec..2acb67e91 100644 --- a/php/EE/Bootstrap/RegisterFrameworkCommands.php +++ b/php/EE/Bootstrap/RegisterFrameworkCommands.php @@ -2,6 +2,9 @@ namespace EE\Bootstrap; +use DirectoryIterator; +use EE; + /** * Class RegisterFrameworkCommands. * @@ -21,7 +24,7 @@ final class RegisterFrameworkCommands implements BootstrapStep { public function process( BootstrapState $state ) { $cmd_dir = EE_ROOT . '/php/commands'; - $iterator = new \DirectoryIterator( $cmd_dir ); + $iterator = new DirectoryIterator( $cmd_dir ); foreach ( $iterator as $filename ) { if ( '.php' !== substr( $filename, - 4 ) ) { @@ -31,7 +34,7 @@ public function process( BootstrapState $state ) { try { include_once "$cmd_dir/$filename"; } catch ( \Exception $exception ) { - \EE::warning( + EE::warning( "Could not add command {$cmd_dir}/{$filename}. Reason: " . $exception->getMessage() ); } diff --git a/php/EE/Bootstrap/RunnerInstance.php b/php/EE/Bootstrap/RunnerInstance.php index 12649f8a9..9379fcdfc 100644 --- a/php/EE/Bootstrap/RunnerInstance.php +++ b/php/EE/Bootstrap/RunnerInstance.php @@ -2,6 +2,8 @@ namespace EE\Bootstrap; +use EE; + /** * Class RunnerInstance. * @@ -27,6 +29,6 @@ public function __invoke() { require_once EE_ROOT . '/php/EE/Configurator.php'; } - return \EE::get_runner(); + return EE::get_runner(); } } diff --git a/php/EE/Completions.php b/php/EE/Completions.php index 11a103278..993df63f5 100644 --- a/php/EE/Completions.php +++ b/php/EE/Completions.php @@ -2,6 +2,8 @@ namespace EE; +use EE; + class Completions { private $words; @@ -52,7 +54,7 @@ public function __construct( $line ) { if ( $command->can_have_subcommands() ) { // add completion when command is `ee` and alias isn't set. if ( 'ee' === $command->get_name() && false === $is_alias && false == $is_help ) { - $aliases = \EE::get_configurator()->get_aliases(); + $aliases = EE::get_configurator()->get_aliases(); foreach ( $aliases as $name => $_ ) { $this->add( "$name " ); } @@ -109,9 +111,9 @@ private function get_command( $words ) { } } - $r = \EE::get_runner()->find_command_to_run( $positional_args ); + $r = EE::get_runner()->find_command_to_run( $positional_args ); if ( ! is_array( $r ) && array_pop( $positional_args ) == $this->cur_word ) { - $r = \EE::get_runner()->find_command_to_run( $positional_args ); + $r = EE::get_runner()->find_command_to_run( $positional_args ); } if ( ! is_array( $r ) ) { @@ -125,7 +127,7 @@ private function get_command( $words ) { private function get_global_parameters() { $params = array(); - foreach ( \EE::get_configurator()->get_spec() as $key => $details ) { + foreach ( EE::get_configurator()->get_spec() as $key => $details ) { if ( false === $details['runtime'] ) { continue; } @@ -160,7 +162,7 @@ private function add( $opt ) { public function render() { foreach ( $this->opts as $opt ) { - \EE::line( $opt ); + EE::line( $opt ); } } } diff --git a/php/EE/Configurator.php b/php/EE/Configurator.php index f4b142484..247cd9af0 100644 --- a/php/EE/Configurator.php +++ b/php/EE/Configurator.php @@ -3,6 +3,7 @@ namespace EE; use Mustangostang\Spyc; +use EE\Utils; /** * Handles file- and runtime-based configuration values. @@ -276,7 +277,7 @@ public function merge_array( $config ) { $value = $config[ $key ]; if ( 'require' == $key ) { - $value = \EE\Utils\expand_globs( $value ); + $value = Utils\expand_globs( $value ); } if ( $details['multiple'] ) { @@ -311,7 +312,7 @@ private static function load_yml( $yml_file ) { if ( isset( $config['require'] ) ) { self::arrayify( $config['require'] ); - $config['require'] = \EE\Utils\expand_globs( $config['require'] ); + $config['require'] = Utils\expand_globs( $config['require'] ); foreach ( $config['require'] as &$path ) { self::absolutize( $path, $yml_file_dir ); } @@ -336,7 +337,7 @@ private static function arrayify( &$val ) { * @param string $base Base path to prepend. */ private static function absolutize( &$path, $base ) { - if ( ! empty( $path ) && ! \EE\Utils\is_path_absolute( $path ) ) { + if ( ! empty( $path ) && ! Utils\is_path_absolute( $path ) ) { $path = $base . DIRECTORY_SEPARATOR . $path; } } diff --git a/php/EE/Dispatcher/CommandFactory.php b/php/EE/Dispatcher/CommandFactory.php index b476dc7ca..919b7af3f 100644 --- a/php/EE/Dispatcher/CommandFactory.php +++ b/php/EE/Dispatcher/CommandFactory.php @@ -2,6 +2,11 @@ namespace EE\Dispatcher; +use EE; +use ReflectionFunction; +use ReflectionClass; +use EE\DocParser; + /** * Creates CompositeCommand or Subcommand instances. * @@ -23,16 +28,16 @@ public static function create( $name, $callable, $parent ) { if ( ( is_object( $callable ) && ( $callable instanceof \Closure ) ) || ( is_string( $callable ) && function_exists( $callable ) ) ) { - $reflection = new \ReflectionFunction( $callable ); + $reflection = new ReflectionFunction( $callable ); $command = self::create_subcommand( $parent, $name, $callable, $reflection ); } elseif ( is_array( $callable ) && is_callable( $callable ) ) { - $reflection = new \ReflectionClass( $callable[0] ); + $reflection = new ReflectionClass( $callable[0] ); $command = self::create_subcommand( $parent, $name, array( $callable[0], $callable[1] ), $reflection->getMethod( $callable[1] ) ); } else { - $reflection = new \ReflectionClass( $callable ); + $reflection = new ReflectionClass( $callable ); if ( $reflection->isSubclassOf( '\EE\Dispatcher\CommandNamespace' ) ) { $command = self::create_namespace( $parent, $name, $callable ); } elseif ( $reflection->hasMethod( '__invoke' ) ) { @@ -74,7 +79,7 @@ private static function create_subcommand( $parent, $name, $callable, $reflectio $inherited_method = $reflection->getDeclaringClass()->getParentClass()->getMethod( $reflection->name ); $doc_comment = self::get_doc_comment( $inherited_method ); - $docparser = new \EE\DocParser( $doc_comment ); + $docparser = new DocParser( $doc_comment ); } if ( is_array( $callable ) ) { @@ -87,7 +92,7 @@ private static function create_subcommand( $parent, $name, $callable, $reflectio } } if ( ! $doc_comment ) { - \EE::debug( null === $doc_comment ? "Failed to get doc comment for {$name}." : "No doc comment for {$name}.", 'commandfactory' ); + EE::debug( null === $doc_comment ? "Failed to get doc comment for {$name}." : "No doc comment for {$name}.", 'commandfactory' ); } $when_invoked = function ( $args, $assoc_args ) use ( $callable ) { @@ -110,12 +115,12 @@ private static function create_subcommand( $parent, $name, $callable, $reflectio * @param mixed $callable */ private static function create_composite_command( $parent, $name, $callable ) { - $reflection = new \ReflectionClass( $callable ); + $reflection = new ReflectionClass( $callable ); $doc_comment = self::get_doc_comment( $reflection ); if ( ! $doc_comment ) { - \EE::debug( null === $doc_comment ? "Failed to get doc comment for {$name}." : "No doc comment for {$name}.", 'commandfactory' ); + EE::debug( null === $doc_comment ? "Failed to get doc comment for {$name}." : "No doc comment for {$name}.", 'commandfactory' ); } - $docparser = new \EE\DocParser( $doc_comment ); + $docparser = new DocParser( $doc_comment ); $container = new CompositeCommand( $parent, $name, $docparser ); @@ -144,12 +149,12 @@ private static function create_composite_command( $parent, $name, $callable ) { * @param mixed $callable */ private static function create_namespace( $parent, $name, $callable ) { - $reflection = new \ReflectionClass( $callable ); + $reflection = new ReflectionClass( $callable ); $doc_comment = self::get_doc_comment( $reflection ); if ( ! $doc_comment ) { - \EE::debug( null === $doc_comment ? "Failed to get doc comment for {$name}." : "No doc comment for {$name}.", 'commandfactory' ); + EE::debug( null === $doc_comment ? "Failed to get doc comment for {$name}." : "No doc comment for {$name}.", 'commandfactory' ); } - $docparser = new \EE\DocParser( $doc_comment ); + $docparser = new DocParser( $doc_comment ); return new CommandNamespace( $parent, $name, $docparser ); } @@ -169,12 +174,12 @@ private static function is_good_method( $method ) { * @param ReflectionMethod $reflection */ private static function get_inherited_docparser( $doc_comment, $reflection ) { - $docparser = new \EE\DocParser( $doc_comment ); + $docparser = new DocParser( $doc_comment ); while ( $docparser->has_tag( 'inheritdoc' ) ) { $inherited_method = $reflection->getDeclaringClass()->getParentClass()->getMethod( $reflection->name ); $doc_comment = self::get_doc_comment( $inherited_method ); - $docparser = new \EE\DocParser( $doc_comment ); + $docparser = new DocParser( $doc_comment ); } return $docparser; @@ -216,7 +221,7 @@ private static function get_doc_comment( $reflection ) { } elseif ( is_readable( $filename ) && ( $contents = file_get_contents( $filename ) ) ) { self::$file_contents[ $filename ] = $contents = explode( "\n", $contents ); } else { - \EE::debug( "Could not read contents for filename '{$filename}'.", 'commandfactory' ); + EE::debug( "Could not read contents for filename '{$filename}'.", 'commandfactory' ); return null; } diff --git a/php/EE/Dispatcher/CommandNamespace.php b/php/EE/Dispatcher/CommandNamespace.php index fc18b7a91..5a5e07358 100644 --- a/php/EE/Dispatcher/CommandNamespace.php +++ b/php/EE/Dispatcher/CommandNamespace.php @@ -30,11 +30,11 @@ public function show_usage() { foreach ( $methods as $name => $subcommand ) { $prefix = ( 0 == $i++ ) ? 'usage: ' : ' or: '; - if ( \EE::get_runner()->is_command_disabled( $subcommand ) ) { + if ( EE::get_runner()->is_command_disabled( $subcommand ) ) { continue; } - \EE::line( $subcommand->get_usage( $prefix ) ); + EE::line( $subcommand->get_usage( $prefix ) ); $count++; } @@ -43,8 +43,8 @@ public function show_usage() { ? "See 'ee help $cmd_name ' for more information on a specific command." : "The namespace $cmd_name does not contain any usable commands in the current context."; - \EE::line(); - \EE::line( $message ); + EE::line(); + EE::line( $message ); } } diff --git a/php/EE/Dispatcher/CompositeCommand.php b/php/EE/Dispatcher/CompositeCommand.php index 261282cd9..04be0492c 100644 --- a/php/EE/Dispatcher/CompositeCommand.php +++ b/php/EE/Dispatcher/CompositeCommand.php @@ -3,6 +3,7 @@ namespace EE\Dispatcher; use \EE\Utils; +use EE; /** * A non-leaf node in the command tree. @@ -34,7 +35,7 @@ public function __construct( $parent, $name, $docparser ) { $when_to_invoke = $docparser->get_tag( 'when' ); if ( $when_to_invoke ) { - \EE::get_runner()->register_early_invoke( $when_to_invoke, $this ); + EE::get_runner()->register_early_invoke( $when_to_invoke, $this ); } } @@ -177,17 +178,17 @@ public function show_usage() { foreach ( $methods as $name => $subcommand ) { $prefix = ( 0 == $i++ ) ? 'usage: ' : ' or: '; - if ( \EE::get_runner()->is_command_disabled( $subcommand ) ) { + if ( EE::get_runner()->is_command_disabled( $subcommand ) ) { continue; } - \EE::line( $subcommand->get_usage( $prefix ) ); + EE::line( $subcommand->get_usage( $prefix ) ); } $cmd_name = implode( ' ', array_slice( get_path( $this ), 1 ) ); - \EE::line(); - \EE::line( "See 'ee help $cmd_name ' for more information on a specific command." ); + EE::line(); + EE::line( "See 'ee help $cmd_name ' for more information on a specific command." ); } /** @@ -272,7 +273,7 @@ protected function get_global_params( $root_command = false ) { $binding['is_subcommand'] = true; } - foreach ( \EE::get_configurator()->get_spec() as $key => $details ) { + foreach ( EE::get_configurator()->get_spec() as $key => $details ) { if ( false === $details['runtime'] ) { continue; } diff --git a/php/EE/Dispatcher/Subcommand.php b/php/EE/Dispatcher/Subcommand.php index 520be2cb4..c74b634c8 100644 --- a/php/EE/Dispatcher/Subcommand.php +++ b/php/EE/Dispatcher/Subcommand.php @@ -4,6 +4,9 @@ use EE; use EE\Utils; +use EE\SynopsisValidator; +use EE\SynopsisParser; +use EE\DocParser; /** * A leaf node in the command tree. @@ -87,7 +90,7 @@ public function get_alias() { * @param string $prefix */ public function show_usage( $prefix = 'usage: ' ) { - \EE::line( $this->get_usage( $prefix ) ); + EE::line( $this->get_usage( $prefix ) ); } /** @@ -121,11 +124,11 @@ private function validate_args( $args, $assoc_args, $extra_args ) { return array( array(), $args, $assoc_args, $extra_args ); } - $validator = new \EE\SynopsisValidator( $synopsis ); + $validator = new SynopsisValidator( $synopsis ); $cmd_path = implode( ' ', get_path( $this ) ); foreach ( $validator->get_unknown() as $token ) { - \EE::warning( + EE::warning( sprintf( 'The `%s` command has an invalid synopsis part: %s', $cmd_path, @@ -141,13 +144,13 @@ private function validate_args( $args, $assoc_args, $extra_args ) { $unknown_positionals = $validator->unknown_positionals( $args ); if ( ! empty( $unknown_positionals ) && 'wp' !== $this->name ) { - \EE::error( + EE::error( 'Too many positional arguments: ' . implode( ' ', $unknown_positionals ) ); } - $synopsis_spec = \EE\SynopsisParser::parse( $synopsis ); + $synopsis_spec = SynopsisParser::parse( $synopsis ); $i = 0; $errors = array( 'fatal' => array(), @@ -156,7 +159,7 @@ private function validate_args( $args, $assoc_args, $extra_args ) { $mock_doc = array( $this->get_shortdesc(), '' ); $mock_doc = array_merge( $mock_doc, explode( "\n", $this->get_longdesc() ) ); $mock_doc = '/**' . PHP_EOL . '* ' . implode( PHP_EOL . '* ', $mock_doc ) . PHP_EOL . '*/'; - $docparser = new \EE\DocParser( $mock_doc ); + $docparser = new DocParser( $mock_doc ); foreach ( $synopsis_spec as $spec ) { if ( 'positional' === $spec['type'] ) { $spec_args = $docparser->get_arg_args( $spec['name'] ); @@ -169,13 +172,13 @@ private function validate_args( $args, $assoc_args, $extra_args ) { if ( ! empty( $spec['repeating'] ) ) { do { if ( isset( $args[ $i ] ) && ! in_array( $args[ $i ], $spec_args['options'] ) ) { - \EE::error( 'Invalid value specified for positional arg.' ); + EE::error( 'Invalid value specified for positional arg.' ); } $i++; } while ( isset( $args[ $i ] ) ); } else { if ( isset( $args[ $i ] ) && ! in_array( $args[ $i ], $spec_args['options'] ) ) { - \EE::error( 'Invalid value specified for positional arg.' ); + EE::error( 'Invalid value specified for positional arg.' ); } } } @@ -196,7 +199,7 @@ private function validate_args( $args, $assoc_args, $extra_args ) { } list( $returned_errors, $to_unset ) = $validator->validate_assoc( - array_merge( \EE::get_config(), $extra_args, $assoc_args ) + array_merge( EE::get_config(), $extra_args, $assoc_args ) ); foreach ( array( 'fatal', 'warning' ) as $error_type ) { $errors[ $error_type ] = array_merge( $errors[ $error_type ], $returned_errors[ $error_type ] ); @@ -227,7 +230,7 @@ private function validate_args( $args, $assoc_args, $extra_args ) { } } - \EE::error( $out ); + EE::error( $out ); } array_map( '\\EE::warning', $errors['warning'] ); @@ -288,7 +291,7 @@ public function invoke( $args, $assoc_args, $extra_args ) { private function get_parameters( $spec = array() ) { $local_parameters = array_column( $spec, 'name' ); $global_parameters = array_column( - EE\SynopsisParser::parse( $this->get_global_params() ), + SynopsisParser::parse( $this->get_global_params() ), 'name' ); diff --git a/php/EE/Fetchers/Base.php b/php/EE/Fetchers/Base.php index 7bc845e13..970bd3361 100644 --- a/php/EE/Fetchers/Base.php +++ b/php/EE/Fetchers/Base.php @@ -2,6 +2,8 @@ namespace EE\Fetchers; +use EE; + /** * Fetch a WordPress entity for use in a subcommand. */ @@ -27,7 +29,7 @@ public function get_check( $arg ) { $item = $this->get( $arg ); if ( ! $item ) { - \EE::error( sprintf( $this->msg, $arg ) ); + EE::error( sprintf( $this->msg, $arg ) ); } return $item; @@ -46,7 +48,7 @@ public function get_many( $args ) { if ( $item ) { $items[] = $item; } else { - \EE::warning( sprintf( $this->msg, $arg ) ); + EE::warning( sprintf( $this->msg, $arg ) ); } } diff --git a/php/EE/FileCache.php b/php/EE/FileCache.php index b8e900eb4..00ac1528f 100644 --- a/php/EE/FileCache.php +++ b/php/EE/FileCache.php @@ -14,6 +14,8 @@ namespace EE; use Symfony\Component\Finder\Finder; +use EE; +use DateTime; /** * Reads/writes to a filesystem cache @@ -221,9 +223,9 @@ public function clean() { // unlink expired files if ( $ttl > 0 ) { try { - $expire = new \DateTime(); + $expire = new DateTime(); } catch ( \Exception $e ) { - \EE::error( $e->getMessage() ); + EE::error( $e->getMessage() ); } $expire->modify( '-' . $ttl . ' seconds' ); @@ -260,7 +262,7 @@ protected function ensure_dir_exists( $dir ) { if ( ! is_dir( $dir ) ) { if ( ! @mkdir( $dir, 0777, true ) ) { // @codingStandardsIgnoreLine $error = error_get_last(); - \EE::warning( sprintf( "Failed to create directory '%s': %s.", $dir, $error['message'] ) ); + EE::warning( sprintf( "Failed to create directory '%s': %s.", $dir, $error['message'] ) ); return false; } } diff --git a/php/EE/Formatter.php b/php/EE/Formatter.php index b3dab359f..13fbce999 100644 --- a/php/EE/Formatter.php +++ b/php/EE/Formatter.php @@ -2,7 +2,11 @@ namespace EE; +use EE; use Mustangostang\Spyc; +use EE\Utils; +use cli\Table; +use cli\Colors; /** * Output one or more items in a given format (e.g. table, JSON). @@ -80,7 +84,7 @@ public function display_items( $items, $ascii_pre_colorized = false ) { if ( in_array( $this->args['format'], array( 'table', 'csv' ) ) ) { if ( is_object( $items ) && is_a( $items, 'Iterator' ) ) { - $items = \EE\Utils\iterator_map( $items, array( $this, 'transform_item_values_to_json' ) ); + $items = Utils\iterator_map( $items, array( $this, 'transform_item_values_to_json' ) ); } else { $items = array_map( array( $this, 'transform_item_values_to_json' ), $items ); } @@ -104,7 +108,7 @@ public function display_item( $item, $ascii_pre_colorized = false ) { if ( in_array( $this->args['format'], array( 'table', 'csv' ) ) && ( is_object( $value ) || is_array( $value ) ) ) { $value = json_encode( $value ); } - \EE::print_value( + EE::print_value( $value, array( 'format' => $this->args['format'], @@ -144,14 +148,14 @@ private function format( $items, $ascii_pre_colorized = false ) { break; case 'csv': - \EE\Utils\write_csv( STDOUT, $items, $fields ); + Utils\write_csv( STDOUT, $items, $fields ); break; case 'json': case 'yaml': $out = array(); foreach ( $items as $item ) { - $out[] = \EE\Utils\pick_fields( $item, $fields ); + $out[] = Utils\pick_fields( $item, $fields ); } if ( 'json' === $this->args['format'] ) { @@ -166,7 +170,7 @@ private function format( $items, $ascii_pre_colorized = false ) { break; default: - \EE::error( 'Invalid format: ' . $this->args['format'] ); + EE::error( 'Invalid format: ' . $this->args['format'] ); } } @@ -190,7 +194,7 @@ private function show_single_field( $items, $field ) { if ( 'json' == $this->args['format'] ) { $values[] = $item->$key; } else { - \EE::print_value( + EE::print_value( $item->$key, array( 'format' => $this->args['format'], @@ -221,7 +225,7 @@ private function find_item_key( $item, $field ) { } if ( ! isset( $key ) ) { - \EE::error( "Invalid field: $field." ); + EE::error( "Invalid field: $field." ); } return $key; @@ -260,13 +264,13 @@ private function show_multiple_fields( $data, $format, $ascii_pre_colorized = fa if ( 'table' == $format ) { self::show_table( $rows, $fields, $ascii_pre_colorized ); } elseif ( 'csv' == $format ) { - \EE\Utils\write_csv( STDOUT, $rows, $fields ); + Utils\write_csv( STDOUT, $rows, $fields ); } break; case 'yaml': case 'json': - \EE::print_value( + EE::print_value( $data, array( 'format' => $format, @@ -275,7 +279,7 @@ private function show_multiple_fields( $data, $format, $ascii_pre_colorized = fa break; default: - \EE::error( 'Invalid format: ' . $format ); + EE::error( 'Invalid format: ' . $format ); break; } @@ -290,26 +294,26 @@ private function show_multiple_fields( $data, $format, $ascii_pre_colorized = fa * @param bool|array $ascii_pre_colorized Optional. A boolean or an array of booleans to pass to `Table::setAsciiPreColorized()` if items in the table are pre-colorized. Default false. */ private static function show_table( $items, $fields, $ascii_pre_colorized = false ) { - $table = new \cli\Table(); + $table = new Table(); - $enabled = \cli\Colors::shouldColorize(); + $enabled = Colors::shouldColorize(); if ( $enabled ) { - \cli\Colors::disable( true ); + Colors::disable( true ); } $table->setAsciiPreColorized( $ascii_pre_colorized ); $table->setHeaders( $fields ); foreach ( $items as $item ) { - $table->addRow( array_values( \EE\Utils\pick_fields( $item, $fields ) ) ); + $table->addRow( array_values( Utils\pick_fields( $item, $fields ) ) ); } foreach ( $table->getDisplayLines() as $line ) { - \EE::line( $line ); + EE::line( $line ); } if ( $enabled ) { - \cli\Colors::enable( true ); + Colors::enable( true ); } } diff --git a/php/EE/Iterators/CSV.php b/php/EE/Iterators/CSV.php index d5a42ab7a..4a2f819f6 100644 --- a/php/EE/Iterators/CSV.php +++ b/php/EE/Iterators/CSV.php @@ -2,6 +2,8 @@ namespace EE\Iterators; +use EE: + /** * Allows incrementally reading and parsing lines from a CSV file. */ @@ -20,7 +22,7 @@ class CSV implements \Iterator { public function __construct( $filename, $delimiter = ',' ) { $this->filePointer = fopen( $filename, 'rb' ); if ( ! $this->filePointer ) { - \EE::error( sprintf( 'Could not open file: %s', $filename ) ); + EE::error( sprintf( 'Could not open file: %s', $filename ) ); } $this->delimiter = $delimiter; diff --git a/php/EE/Loggers/Base.php b/php/EE/Loggers/Base.php index f213a5cff..e0fb2b687 100644 --- a/php/EE/Loggers/Base.php +++ b/php/EE/Loggers/Base.php @@ -2,6 +2,8 @@ namespace EE\Loggers; +use EE; + /** * Base logger class */ @@ -22,7 +24,7 @@ abstract public function warning( $message ); * @return Runner Instance of the runner class */ protected function get_runner() { - return \EE::get_runner(); + return EE::get_runner(); } /** diff --git a/php/EE/Loggers/Execution.php b/php/EE/Loggers/Execution.php index 2ee7aa0cc..4641eb9d1 100644 --- a/php/EE/Loggers/Execution.php +++ b/php/EE/Loggers/Execution.php @@ -2,6 +2,8 @@ namespace EE\Loggers; +use EE; + /** * Execution logger captures all STDOUT and STDERR writes */ @@ -32,8 +34,8 @@ function __construct( $in_color = false ) { public function error_multi_line( $message_lines ) { $message = implode( "\n", $message_lines ); - $this->write( STDERR, \EE::colorize( "%RError:%n\n$message\n" ) ); - $this->write( STDERR, \EE::colorize( "%R---------%n\n\n" ) ); + $this->write( STDERR, EE::colorize( "%RError:%n\n$message\n" ) ); + $this->write( STDERR, EE::colorize( "%R---------%n\n\n" ) ); } /** diff --git a/php/EE/Loggers/Quiet.php b/php/EE/Loggers/Quiet.php index 6195288d3..52f775ca8 100644 --- a/php/EE/Loggers/Quiet.php +++ b/php/EE/Loggers/Quiet.php @@ -2,6 +2,8 @@ namespace EE\Loggers; +use EE; + /** * Quiet logger only logs errors. */ @@ -40,7 +42,7 @@ public function warning( $message ) { * @param string $message Message to write. */ public function error( $message ) { - $this->write( STDERR, \EE::colorize( "%RError:%n $message\n" ) ); + $this->write( STDERR, EE::colorize( "%RError:%n $message\n" ) ); } /** @@ -51,7 +53,7 @@ public function error( $message ) { public function error_multi_line( $message_lines ) { $message = implode( "\n", $message_lines ); - $this->write( STDERR, \EE::colorize( "%RError:%n\n$message\n" ) ); - $this->write( STDERR, \EE::colorize( "%R---------%n\n\n" ) ); + $this->write( STDERR, EE::colorize( "%RError:%n\n$message\n" ) ); + $this->write( STDERR, EE::colorize( "%R---------%n\n\n" ) ); } } diff --git a/php/EE/Loggers/Regular.php b/php/EE/Loggers/Regular.php index 168e8fbc8..3ddcade73 100644 --- a/php/EE/Loggers/Regular.php +++ b/php/EE/Loggers/Regular.php @@ -2,6 +2,8 @@ namespace EE\Loggers; +use cli\Colors; + /** * Default logger for success, warning, error, and standard messages. */ @@ -67,12 +69,12 @@ function( $line ) { $longest = max( array_map( 'strlen', $message_lines ) ); // write an empty line before the message - $empty_line = \cli\Colors::colorize( '%w%1 ' . str_repeat( ' ', $longest ) . ' %n' ); + $empty_line = Colors::colorize( '%w%1 ' . str_repeat( ' ', $longest ) . ' %n' ); $this->write( STDERR, "\n\t$empty_line\n" ); foreach ( $message_lines as $line ) { $padding = str_repeat( ' ', $longest - strlen( $line ) ); - $line = \cli\Colors::colorize( "%w%1 $line $padding%n" ); + $line = Colors::colorize( "%w%1 $line $padding%n" ); $this->write( STDERR, "\t$line\n" ); } diff --git a/php/EE/Migration/Base.php b/php/EE/Migration/Base.php index 5008036aa..8afb71c43 100644 --- a/php/EE/Migration/Base.php +++ b/php/EE/Migration/Base.php @@ -3,6 +3,7 @@ namespace EE\Migration; use Symfony\Component\Filesystem\Filesystem; +use EE\Model\Option; abstract class Base { @@ -16,7 +17,7 @@ abstract class Base { public function __construct() { $this->fs = new Filesystem(); $this->skip_this_migration = false; - $this->is_first_execution = ! \EE\Model\Option::get( 'version' ); + $this->is_first_execution = ! Option::get( 'version' ); $this->backup_dir = EE_BACKUP_DIR; $this->fs->mkdir( $this->backup_dir ); } diff --git a/php/EE/Migration/Containers.php b/php/EE/Migration/Containers.php index cf881acc1..1ae1e4372 100644 --- a/php/EE/Migration/Containers.php +++ b/php/EE/Migration/Containers.php @@ -4,6 +4,9 @@ use EE\RevertableStepProcessor; use EE; +use EE\Utils; +use EE\Model\Option; +use EE_DB; /** * Upgrade existing containers to new docker-image @@ -19,11 +22,11 @@ class Containers { * @throws \Exception */ public static function start_container_migration() { - EE\Utils\delem_log( 'Starting container migration' ); + Utils\delem_log( 'Starting container migration' ); self::$rsp = new RevertableStepProcessor(); - $img_versions = EE\Utils\get_image_versions(); + $img_versions = Utils\get_image_versions(); $current_versions = self::get_current_docker_images_versions(); $updated_images = []; @@ -49,7 +52,7 @@ public static function start_container_migration() { throw new \Exception( 'Unable to migrate sites to newer version' ); } - EE\Utils\delem_log( 'Container migration completed' ); + Utils\delem_log( 'Container migration completed' ); } /** @@ -82,7 +85,7 @@ public static function save_upgraded_image_versions( $current_versions, $new_ver */ public static function create_database_entry( $new_versions, $updated_images ) { foreach ( $updated_images as $image ) { - EE\Model\Option::update( [ [ 'key', $image ] ], [ 'value' => $new_versions[ $image ] ] ); + Option::update( [ [ 'key', $image ] ], [ 'value' => $new_versions[ $image ] ] ); } } @@ -96,7 +99,7 @@ public static function create_database_entry( $new_versions, $updated_images ) { */ public static function revert_database_entry( $old_version, $updated_images ) { foreach ( $updated_images as $image ) { - EE\Model\Option::update( [ [ 'key', $image ] ], [ 'value' => $old_version[ $image ] ] ); + Option::update( [ [ 'key', $image ] ], [ 'value' => $old_version[ $image ] ] ); } } @@ -109,7 +112,7 @@ public static function revert_database_entry( $old_version, $updated_images ) { * @throws \Exception */ public static function pull_or_error( $image, $version ) { - if ( ! \EE::exec( "docker pull $image:$version" ) ) { + if ( ! EE::exec( "docker pull $image:$version" ) ) { throw new \Exception( "Unable to pull $image. Please check logs for more details." ); } } @@ -195,7 +198,7 @@ private static function migrate_global_containers( $updated_images ) { */ public static function migrate_site_containers( $updated_images ) { - $db = new \EE_DB(); + $db = new EE_DB(); $sites = ( $db->table( 'sites' )->all() ); foreach ( $sites as $site ) { diff --git a/php/EE/Migration/CustomContainerMigrations.php b/php/EE/Migration/CustomContainerMigrations.php index 53feb8dd3..81cdbb9a6 100644 --- a/php/EE/Migration/CustomContainerMigrations.php +++ b/php/EE/Migration/CustomContainerMigrations.php @@ -52,7 +52,7 @@ private static function get_all_migrations() { $migration_path = EE_VENDOR_DIR . '/easyengine/' . $package . '/migrations/container'; if ( is_dir( $migration_path ) ) { $files = scandir( $migration_path ); - if ( \EE\Utils\inside_phar() ) { + if ( Utils\inside_phar() ) { $migrations[] = $files; } else { $migrations[] = array_slice( $files, 2 ); @@ -64,7 +64,7 @@ private static function get_all_migrations() { // get migrations from core. if ( is_dir( EE_ROOT . '/migrations/container' ) ) { $files = scandir( EE_ROOT . '/migrations/container' ); - if ( \EE\Utils\inside_phar() ) { + if ( Utils\inside_phar() ) { $migrations[] = $files; } else { $migrations[] = array_slice( $files, 2 ); diff --git a/php/EE/Migration/Executor.php b/php/EE/Migration/Executor.php index 249e00df9..c2d8d68a1 100644 --- a/php/EE/Migration/Executor.php +++ b/php/EE/Migration/Executor.php @@ -53,7 +53,7 @@ private static function get_all_migrations() { $migration_path = EE_VENDOR_DIR . '/easyengine/' . $package . '/migrations/db'; if ( is_dir( $migration_path ) ) { $files = scandir( $migration_path ); - if ( \EE\Utils\inside_phar() ) { + if ( Utils\inside_phar() ) { $migrations[] = $files; } else { $migrations[] = array_slice( $files, 2 ); @@ -65,7 +65,7 @@ private static function get_all_migrations() { // get migrations from core. if ( is_dir( EE_ROOT . '/migrations' ) ) { $files = scandir( EE_ROOT . '/migrations/db' ); - if ( \EE\Utils\inside_phar() ) { + if ( Utils\inside_phar() ) { $migrations[] = $files; } else { $migrations[] = array_slice( $files, 2 ); diff --git a/php/EE/Migration/GlobalContainers.php b/php/EE/Migration/GlobalContainers.php index 12f80ab89..1a088e625 100644 --- a/php/EE/Migration/GlobalContainers.php +++ b/php/EE/Migration/GlobalContainers.php @@ -3,6 +3,8 @@ namespace EE\Migration; use EE; +use EE_DOCKER; +use EE\Service\Utils as Service_Utils; /** * Upgrade existing global containers to new docker-image @@ -21,7 +23,7 @@ public static function get_updated_global_images( $updated_images ) { $global_images = self::get_all_global_images_with_service_name(); $running_global_services = []; foreach ( $global_images as $image => $container_name ) { - if ( 'running' === \EE_DOCKER::container_status( $container_name ) ) { + if ( 'running' === EE_DOCKER::container_status( $container_name ) ) { $running_global_services[] = $image; } } @@ -77,7 +79,7 @@ public static function down_global_containers( $updated_images ) { $global_service_name = ltrim( $global_container_name, 'ee-' ); EE::debug( "Removing $global_container_name" ); - if ( false !== \EE_DOCKER::container_status( $global_container_name ) ) { + if ( false !== EE_DOCKER::container_status( $global_container_name ) ) { if ( ! EE::exec( "docker-compose stop $global_service_name && docker-compose rm -f $global_service_name" ) ) { throw new \Exception( "Unable to stop $global_container_name container" ); } @@ -94,9 +96,9 @@ public static function down_global_containers( $updated_images ) { public static function global_service_up( $service_name ) { EE::debug( 'Start ' . $service_name . ' container up' ); if ( 'global-nginx-proxy' === $service_name ) { - \EE\Service\Utils\nginx_proxy_check(); + Service_Utils\nginx_proxy_check(); } else { - \EE\Service\Utils\init_global_container( $service_name ); + Service_Utils\init_global_container( $service_name ); } } diff --git a/php/EE/Migration/SiteContainers.php b/php/EE/Migration/SiteContainers.php index 4d22e76e8..4c43dd188 100644 --- a/php/EE/Migration/SiteContainers.php +++ b/php/EE/Migration/SiteContainers.php @@ -4,6 +4,7 @@ use EE; use Symfony\Component\Filesystem\Filesystem; +use Site_Command; /** * Migrate site specific containers to new images. @@ -18,7 +19,7 @@ class SiteContainers { * @return EE\Site\Type\HTML|EE\Site\Type\PHP|EE\Site\Type\WordPress */ public static function get_site_object( $site_type ) { - $site_command = new \Site_Command(); + $site_command = new Site_Command(); $site_class = $site_command::get_site_types()[ $site_type ]; return new $site_class(); @@ -138,7 +139,7 @@ public static function disable_site( $site_info, $site_object ) { */ public static function delete_volume( $volume_name, $symlink_path ) { $fs = new Filesystem(); - \EE::exec( 'docker volume rm ' . $volume_name ); + EE::exec( 'docker volume rm ' . $volume_name ); $fs->remove( $symlink_path ); } @@ -157,7 +158,7 @@ public static function create_volume( $site, $volume_name, $symlink_path ) { 'path_to_symlink' => $symlink_path, ], ]; - \EE::docker()->create_volumes( $site_url, $volumes ); + EE::docker()->create_volumes( $site_url, $volumes ); } /** diff --git a/php/EE/Model/Migration.php b/php/EE/Model/Migration.php index 5991d7a07..4e5ed398a 100644 --- a/php/EE/Model/Migration.php +++ b/php/EE/Model/Migration.php @@ -2,6 +2,8 @@ namespace EE\Model; +use EE_DB; + /** * Migration model class. */ @@ -14,7 +16,7 @@ class Migration extends Base { */ public static function get_migrations() { - $db = new \EE_DB(); + $db = new EE_DB(); $migrations = $db->table( 'migrations' ) ->select( 'migration' ) ->get(); diff --git a/php/EE/Process.php b/php/EE/Process.php index 351d51e39..b5af9449a 100644 --- a/php/EE/Process.php +++ b/php/EE/Process.php @@ -3,6 +3,7 @@ namespace EE; use EE\Utils; +use RuntimeException; /** * Run a system process, and learn what happened. @@ -112,7 +113,7 @@ public function run_check() { // $r->STDERR is incorrect, but kept incorrect for backwards-compat if ( $r->return_code || ! empty( $r->STDERR ) ) { - throw new \RuntimeException( $r ); + throw new RuntimeException( $r ); } return $r; @@ -128,7 +129,7 @@ public function run_check_stderr() { $r = $this->run(); if ( $r->return_code || ! empty( $r->stderr ) ) { - throw new \RuntimeException( $r ); + throw new RuntimeException( $r ); } return $r; diff --git a/php/EE/RevertableStepProcessor.php b/php/EE/RevertableStepProcessor.php index 993062eb0..46ad8aeec 100644 --- a/php/EE/RevertableStepProcessor.php +++ b/php/EE/RevertableStepProcessor.php @@ -3,6 +3,7 @@ namespace EE; use EE; +use EE\Utils; /** * RevertibleStepProcessor @@ -58,7 +59,7 @@ public function execute() { EE::debug( "Executed $context." ); } catch ( \Exception $e ) { $exception_message = $e->getMessage(); - $callable = EE\Utils\get_callable_name( $step['up'] ); + $callable = Utils\get_callable_name( $step['up'] ); EE::error( "Encountered error while processing $context in $callable. Exception: $exception_message", false ); $this->rollback(); $this->steps = []; diff --git a/php/EE/Runner.php b/php/EE/Runner.php index 3a83acfea..f7a73c0b8 100644 --- a/php/EE/Runner.php +++ b/php/EE/Runner.php @@ -10,6 +10,7 @@ use EE\Utils; use Monolog\Logger; use Mustangostang\Spyc; +use EE\RevertableStepProcessor; /** * Performs the execution of a command. @@ -118,7 +119,7 @@ public function check_requirements( $show_error = true ) { * Function to run migrations required to upgrade to the newer version. Will always be invoked from the newer phar downloaded inside the /tmp folder */ private function migrate() { - $rsp = new \EE\RevertableStepProcessor(); + $rsp = new RevertableStepProcessor(); $rsp->add_step( 'ee-db-migrations', 'EE\Migration\Executor::execute_migrations' ); $rsp->add_step( 'ee-custom-container-migrations', 'EE\Migration\CustomContainerMigrations::execute_migrations' ); @@ -251,7 +252,7 @@ private function cmd_starts_with( $prefix ) { * @return array|string Command, args, and path on success; error message on failure */ public function find_command_to_run( $args ) { - $command = \EE::get_root_command(); + $command = EE::get_root_command(); EE::do_hook( 'find_command_to_run_pre' ); @@ -512,7 +513,7 @@ private function generate_ssh_command( $bits, $ee_command ) { * @return bool */ public function is_command_disabled( $command ) { - $path = implode( ' ', array_slice( \EE\Dispatcher\get_path( $command ), 1 ) ); + $path = implode( ' ', array_slice( Dispatcher\get_path( $command ), 1 ) ); return in_array( $path, $this->config['disabled_commands'] ); } @@ -574,7 +575,7 @@ public function get_required_files() { } public function init_config() { - $configurator = \EE::get_configurator(); + $configurator = EE::get_configurator(); $argv = array_slice( $GLOBALS['argv'], 1 ); @@ -902,7 +903,7 @@ private function trigger_migration( $version ) { */ private function get_subcommand_suggestion( $entry, CompositeCommand $root_command = null ) { $commands = array(); - $this->enumerate_commands( $root_command ?: \EE::get_root_command(), $commands ); + $this->enumerate_commands( $root_command ?: EE::get_root_command(), $commands ); return Utils\get_suggestion( $entry, $commands, $threshold = 2 ); } diff --git a/php/class-ee-db.php b/php/class-ee-db.php index 14d08942b..6040af362 100644 --- a/php/class-ee-db.php +++ b/php/class-ee-db.php @@ -1,5 +1,7 @@ get_where_fragment( [ $key, $args[0][ $key ] ] ); diff --git a/php/class-ee.php b/php/class-ee.php index 3a84061d6..2aa7c023c 100644 --- a/php/class-ee.php +++ b/php/class-ee.php @@ -6,6 +6,9 @@ use EE\Process; use EE\Utils; use Mustangostang\Spyc; +use cli\Colors; +use EE\DocParser; +use EE\SynopsisParser; /** * Various utilities for EE commands. @@ -166,7 +169,7 @@ function () use ( $cache ) { * @return string Colorized string. */ public static function colorize( $string ) { - return \cli\Colors::colorize( $string, self::get_runner()->in_color() ); + return Colors::colorize( $string, self::get_runner()->in_color() ); } /** @@ -338,7 +341,7 @@ public static function add_command( $name, $callable, $args = array() ) { $subcommand = new Dispatcher\CompositeCommand( $command, $subcommand_name, - new \EE\DocParser( '' ) + new DocParser( '' ) ); $command->add_subcommand( $subcommand_name, $subcommand ); } else { @@ -383,7 +386,7 @@ public static function add_command( $name, $callable, $args = array() ) { if ( is_string( $args['synopsis'] ) ) { $leaf_command->set_synopsis( $args['synopsis'] ); } elseif ( is_array( $args['synopsis'] ) ) { - $synopsis = \EE\SynopsisParser::render( $args['synopsis'] ); + $synopsis = SynopsisParser::render( $args['synopsis'] ); $leaf_command->set_synopsis( $synopsis ); $long_desc = ''; $bits = explode( ' ', $synopsis ); @@ -688,7 +691,7 @@ public static function error_multi_line( $message_lines ) { * @param array $assoc_args Skips prompt if 'yes' is provided. */ public static function confirm( $question, $assoc_args = array(), $exit = true ) { - if ( ! \EE\Utils\get_flag_value( $assoc_args, 'yes' ) ) { + if ( ! Utils\get_flag_value( $assoc_args, 'yes' ) ) { fwrite( STDOUT, $question . ' [y/n] ' ); $answer = strtolower( trim( fgets( STDIN ) ) ); @@ -752,7 +755,7 @@ public static function get_value_from_arg_or_stdin( $args, $index ) { * @param array $assoc_args */ public static function read_value( $raw_value, $assoc_args = array() ) { - if ( \EE\Utils\get_flag_value( $assoc_args, 'format' ) === 'json' ) { + if ( Utils\get_flag_value( $assoc_args, 'format' ) === 'json' ) { $value = json_decode( $raw_value, true ); if ( null === $value ) { EE::error( sprintf( 'Invalid JSON: %s', $raw_value ) ); @@ -771,9 +774,9 @@ public static function read_value( $raw_value, $assoc_args = array() ) { * @param array $assoc_args Arguments passed to the command, determining format. */ public static function print_value( $value, $assoc_args = array() ) { - if ( \EE\Utils\get_flag_value( $assoc_args, 'format' ) === 'json' ) { + if ( Utils\get_flag_value( $assoc_args, 'format' ) === 'json' ) { $value = json_encode( $value ); - } elseif ( \EE\Utils\get_flag_value( $assoc_args, 'format' ) === 'yaml' ) { + } elseif ( Utils\get_flag_value( $assoc_args, 'format' ) === 'yaml' ) { $value = Spyc::YAMLDump( $value, 2, 0 ); } elseif ( is_array( $value ) || is_object( $value ) ) { $value = var_export( $value ); @@ -936,7 +939,7 @@ public static function launch_self( $command, $args = array(), $assoc_args = arr $config_path = escapeshellarg( $config_path ); $args = implode( ' ', array_map( 'escapeshellarg', $args ) ); - $assoc_args = \EE\Utils\assoc_args_to_str( $assoc_args ); + $assoc_args = Utils\assoc_args_to_str( $assoc_args ); $full_command = "EE_CONFIG_PATH={$config_path} {$php_bin} {$script_path} {$command} {$args} {$assoc_args}"; @@ -1066,7 +1069,7 @@ public static function runcommand( $command, $options = array() ) { $script_path = $GLOBALS['argv'][0]; // Persist runtime arguments unless they've been specified otherwise. - $configurator = \EE::get_configurator(); + $configurator = EE::get_configurator(); $argv = array_slice( $GLOBALS['argv'], 1 ); list( $_, $_, $runtime_config ) = $configurator->parse_args( $argv ); foreach ( $runtime_config as $k => $v ) { diff --git a/php/utils.php b/php/utils.php index 93e4f1548..3ec0f070c 100644 --- a/php/utils.php +++ b/php/utils.php @@ -9,6 +9,16 @@ use EE; use EE\Iterators\Transform; use Mustangostang\Spyc; +use ArrayIterator; +use EE\Formatter; +use Mustache_Engine; +use cli\Shell; +use EE\NoOp; +use cli\progress\Bar; +use RuntimeException; +use Requests; +use cli\Table; +use cli\table\Ascii; const PHAR_STREAM_PREFIX = 'phar://'; @@ -114,7 +124,7 @@ function load_command( $name ) { */ function iterator_map( $it, $fn ) { if ( is_array( $it ) ) { - $it = new \ArrayIterator( $it ); + $it = new ArrayIterator( $it ); } if ( ! method_exists( $it, 'add_transform' ) ) { @@ -272,7 +282,7 @@ function esc_cmd( $cmd ) { */ function format_items( $format, $items, $fields ) { $assoc_args = compact( 'format', 'fields' ); - $formatter = new \EE\Formatter( $assoc_args ); + $formatter = new Formatter( $assoc_args ); $formatter->display_items( $items ); } @@ -286,6 +296,7 @@ function format_items( $format, $items, $fields ) { * @param array $headers List of CSV columns (optional) */ function write_csv( $fd, $rows, $headers = array() ) { + if ( ! empty( $headers ) ) { fputcsv( $fd, $headers ); } @@ -349,7 +360,7 @@ function launch_editor_for_input( $input, $filename = 'EE' ) { } while ( ! $tmpfile ); if ( ! $tmpfile ) { - \EE::error( 'Error creating temporary file.' ); + EE::error( 'Error creating temporary file.' ); } $output = ''; @@ -390,7 +401,7 @@ function mustache_render( $template_name, $data = array() ) { $template = file_get_contents( $template_name ); - $m = new \Mustache_Engine( + $m = new Mustache_Engine( array( 'escape' => function ( $val ) { return $val; }, @@ -416,11 +427,11 @@ function mustache_render( $template_name, $data = array() ) { * @return cli\progress\Bar|EE\NoOp */ function make_progress_bar( $message, $count, $interval = 100 ) { - if ( \cli\Shell::isPiped() ) { - return new \EE\NoOp; + if ( Shell::isPiped() ) { + return new NoOp; } - return new \cli\progress\Bar( $message, $count, $interval ); + return new Bar( $message, $count, $interval ); } /** @@ -515,12 +526,12 @@ function http_request( $method, $url, $data = null, $headers = array(), $options if ( $halt_on_error ) { EE::error( $error_msg ); } - throw new \RuntimeException( $error_msg ); + throw new RuntimeException( $error_msg ); } } try { - return \Requests::request( $url, $headers, $data, $method, $options ); + return Requests::request( $url, $headers, $data, $method, $options ); } catch ( \Requests_Exception $ex ) { // CURLE_SSL_CACERT_BADFILE only defined for PHP >= 7. if ( 'curlerror' !== $ex->getType() || ! in_array( curl_errno( $ex->getData() ), array( CURLE_SSL_CONNECT_ERROR, CURLE_SSL_CERTPROBLEM, 77 /*CURLE_SSL_CACERT_BADFILE*/ ), true ) ) { @@ -528,19 +539,19 @@ function http_request( $method, $url, $data = null, $headers = array(), $options if ( $halt_on_error ) { EE::error( $error_msg ); } - throw new \RuntimeException( $error_msg, null, $ex ); + throw new RuntimeException( $error_msg, null, $ex ); } // Handle SSL certificate issues gracefully - \EE::warning( sprintf( "Re-trying without verify after failing to get verified url '%s' %s.", $url, $ex->getMessage() ) ); + EE::warning( sprintf( "Re-trying without verify after failing to get verified url '%s' %s.", $url, $ex->getMessage() ) ); $options['verify'] = false; try { - return \Requests::request( $url, $headers, $data, $method, $options ); + return Requests::request( $url, $headers, $data, $method, $options ); } catch ( \Requests_Exception $ex ) { $error_msg = sprintf( "Failed to get non-verified url '%s' %s.", $url, $ex->getMessage() ); if ( $halt_on_error ) { EE::error( $error_msg ); } - throw new \RuntimeException( $error_msg, null, $ex ); + throw new RuntimeException( $error_msg, null, $ex ); } } } @@ -714,7 +725,7 @@ function get_temp_dir() { $temp = trailingslashit( sys_get_temp_dir() ); if ( ! is_writable( $temp ) ) { - \EE::warning( "Temp directory isn't writable: {$temp}" ); + EE::warning( "Temp directory isn't writable: {$temp}" ); } return $temp; @@ -1514,24 +1525,24 @@ function get_type( $assoc_args, $arg_types, $default = false ) { * */ function format_table( $items, $log_in_file = false ) { - $item_table = new \cli\Table(); + $item_table = new Table(); $item_table->setRows( $items ); - $item_table->setRenderer( new \cli\table\Ascii() ); + $item_table->setRenderer( new Ascii() ); $lines = array_slice( $item_table->getDisplayLines(), 3 ); array_pop( $lines ); $delem = $item_table->getDisplayLines()[0]; if ( $log_in_file ) { foreach ( $lines as $line ) { - \EE::log( $delem ); - \EE::log( $line ); + EE::log( $delem ); + EE::log( $line ); } } else { foreach ( $lines as $line ) { - \EE::line( $delem ); - \EE::line( $line ); + EE::line( $delem ); + EE::line( $line ); } } - \EE::log( $delem ); + EE::log( $delem ); } /**