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
34 changes: 34 additions & 0 deletions features/command.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1518,3 +1518,37 @@ Feature: WP-CLI Commands
"""
wp custom
"""

Scenario: subcommand alias should respect @when definition
Given an empty directory
And a custom-cmd.php file:
"""
<?php
class Test_Command {
/**
* test
*
* @alias bar
*
* @when before_wp_load
*
*/
public function foo( $args, $assoc_args ) {
echo 'Hello' . PHP_EOL;
}
}

WP_CLI::add_command( 'test', Test_Command::class );
"""

When I run `wp --require=custom-cmd.php test foo`
Then STDOUT should contain:
"""
Hello
"""

When I run `wp --require=custom-cmd.php test bar`
Then STDOUT should contain:
"""
Hello
"""
4 changes: 2 additions & 2 deletions php/WP_CLI/Dispatcher/Subcommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ class Subcommand extends CompositeCommand {
private $when_invoked;

public function __construct( $parent, $name, $docparser, $when_invoked ) {
$this->alias = $docparser->get_tag( 'alias' );

parent::__construct( $parent, $name, $docparser );

$this->when_invoked = $when_invoked;

$this->alias = $docparser->get_tag( 'alias' );

$this->synopsis = $docparser->get_synopsis();
if ( ! $this->synopsis && $this->longdesc ) {
$this->synopsis = self::extract_synopsis( $this->longdesc );
Expand Down
8 changes: 7 additions & 1 deletion php/WP_CLI/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ public function register_context_manager( ContextManager $context_manager ) {
* @param Subcommand $command
*/
public function register_early_invoke( $when, $command ) {
$this->early_invoke[ $when ][] = array_slice( Dispatcher\get_path( $command ), 1 );
$cmd_path = array_slice( Dispatcher\get_path( $command ), 1 );
$this->early_invoke[ $when ][] = $cmd_path;
if ( $command->get_alias() ) {
array_pop( $cmd_path );
$cmd_path[] = $command->get_alias();
$this->early_invoke[ $when ][] = $cmd_path;
}
}

/**
Expand Down