Skip to content

Feature to conditionally hide commands#1037

Open
RyanNerd wants to merge 1 commit intoconsolidation:3.xfrom
RyanNerd:conditional-command-registration
Open

Feature to conditionally hide commands#1037
RyanNerd wants to merge 1 commit intoconsolidation:3.xfrom
RyanNerd:conditional-command-registration

Conversation

@RyanNerd
Copy link

@RyanNerd RyanNerd commented Jul 4, 2021

Overview

This pull request:

  • Fixes a bug
  • Adds a feature
  • Breaks backwards compatibility
  • Has tests that cover changes
  • Adds or fixes documentation

Summary

If a command class has a public string|array $hiddenCommands property then any command listed in the property will not be added to Robo

Closes #1013

Description

For example:

<?php
class MyCommands
{
    public $hiddenCommands = [];

    public function __construct()
    {
        if (str_contains('window', strtolower(PHP_OS))) {
            // We're using Windows so hide the linux:example command
            $this->hiddenCommands[] = 'linux:example';
        } else {
            // We're not using Windows so hide the com command
            $this->hiddenCommands[] = 'com';
        }
    }

    /**
     * This command will always be available
     */
    public function hello()
    {
        $this->say('Hi!');
    }

    /**
     * @hidden This command will not be available because it has a hidden attribute
     */
    public function goodBye()
    {
        $this->say('Bye!');
    }

    /**
     * Command will not work in Windows
     * @link: https://www.php.net/manual/en/function.cli-set-process-title.php
     */
    public function linuxExample()
    {
        $title = "My Amazing PHP Script";
        $pid = getmypid(); // you can use this to see your process title in ps

        if (!cli_set_process_title($title)) {
            echo "Unable to set process title for PID $pid...\n";
            exit(1);
        } else {
            echo "The process title '$title' for PID $pid has been set for your process!\n";
            sleep(5);
        }
    }

    /**
     * COM is only available on Windows
     */
    public function com()
    {
        $domainObject = new COM("WinNT://Domain");
        foreach ($domainObject as $obj) {
            $this->say($obj->Name);
        }
    }
}

If a commands class has a `public string|array $hiddenCommands` property then any command listed in the property will not be added to Robo

Closes consolidation#1013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Disabling/Hiding commands conditionally

1 participant