From 9b0f8ed5fec2bfbd42ae1560b0625e996a89f9f9 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Wed, 6 Apr 2016 05:29:05 -0700 Subject: [PATCH] Introduce `CompositeCommand->remove_subcommand()` This permits a subcommand to be de-registered from its parent. Also modifies the bootstrap process to always register commands, instead of lazyloading them based on command name. Packages and required commands are always loaded, so we should follow similar behavior with core commands. --- features/command.feature | 21 +++++++++++++++++++++ php/WP_CLI/Dispatcher/CompositeCommand.php | 13 +++++++++++++ php/WP_CLI/Dispatcher/RootCommand.php | 2 -- php/WP_CLI/Runner.php | 2 +- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/features/command.feature b/features/command.feature index 59015ad572..781ebf6fb6 100644 --- a/features/command.feature +++ b/features/command.feature @@ -490,3 +490,24 @@ Feature: WP-CLI Commands shop: cha cha cha burrito: """ + + Scenario: Removing a subcommand should remove it from the index + Given an empty directory + And a remove-comment.php file: + """ + remove_subcommand( 'comment' ); + """ + + When I run `wp` + Then STDOUT should contain: + """ + Manage comments. + """ + + When I run `wp --require=remove-comment.php` + Then STDOUT should not contain: + """ + Manage comments. + """ diff --git a/php/WP_CLI/Dispatcher/CompositeCommand.php b/php/WP_CLI/Dispatcher/CompositeCommand.php index a4712a592b..53862245b0 100644 --- a/php/WP_CLI/Dispatcher/CompositeCommand.php +++ b/php/WP_CLI/Dispatcher/CompositeCommand.php @@ -58,6 +58,19 @@ public function add_subcommand( $name, $command ) { $this->subcommands[ $name ] = $command; } + /** + * Remove a named subcommand from this composite command's set of contained + * subcommands + * + * @param string $name Represents how subcommand should be invoked + */ + public function remove_subcommand( $name ) { + if ( isset( $this->subcommands[ $name ] ) ) { + unset( $this->subcommands[ $name ] ); + } + } + + /** * Composite commands always contain subcommands. * diff --git a/php/WP_CLI/Dispatcher/RootCommand.php b/php/WP_CLI/Dispatcher/RootCommand.php index fb730da07e..0b720aea0c 100644 --- a/php/WP_CLI/Dispatcher/RootCommand.php +++ b/php/WP_CLI/Dispatcher/RootCommand.php @@ -53,8 +53,6 @@ public function find_subcommand( &$args ) { * @return array */ public function get_subcommands() { - Utils\load_all_commands(); - return parent::get_subcommands(); } } diff --git a/php/WP_CLI/Runner.php b/php/WP_CLI/Runner.php index 43bde527a2..ee11655a99 100644 --- a/php/WP_CLI/Runner.php +++ b/php/WP_CLI/Runner.php @@ -626,7 +626,7 @@ public function start() { // Load bundled commands early, so that they're forced to use the same // APIs as non-bundled commands. - Utils\load_command( $this->arguments[0] ); + Utils\load_all_commands(); $skip_packages = \WP_CLI::get_runner()->config['skip-packages']; if ( true === $skip_packages ) {