Skip to content

Instantly share code, notes, and snippets.

@damsfx
Last active July 30, 2025 15:03
Show Gist options
  • Select an option

  • Save damsfx/19c094fa37af0edeb961e95c84b7d36e to your computer and use it in GitHub Desktop.

Select an option

Save damsfx/19c094fa37af0edeb961e95c84b7d36e to your computer and use it in GitHub Desktop.

WinterCMS console command output

This is a console command used as a demonstration to quickly find the various possibilities offered by the output of a console command.

Infos

  1. Copy the file as TestOutputCommand.php in your plugin console directory
  2. Adapt namespace Acme\Myplugin\Console to your plugin
  3. Register the command in your plugin's plugin.php file
    public function register(): void
    {
        // ... 
        $this->registerConsoleCommand(
            'myplugin.testoutput',
            \Acme\Myplugin\Console\TestOutputCommand::class
        );
    }

Warning

Some commands are not rendered in the same way on all OS, especially Windows.

TestOutputCommand.php file

<?php

namespace Acme\Myplugin\Console;

use ReflectionProperty;
use Symfony\Component\Console\Color;
use Symfony\Component\Console\Helper\TableSeparator;
use Winter\Storm\Console\Command;

class TestOutputCommand extends Command
{
    /**
     * @var string The console command name.
     */
    protected static $defaultName = 'myplugin:testoutput';

    /**
     * @var string The name and signature of this command.
     */
    protected $signature = 'myplugin:testoutput
        {commandType? : Type of command to display (none, symfony, laravel or styles).}
        ';

    /**
     * @var string The console command description.
     */
    protected $description = 'Demonstration of console output methods';

    /**
     * Execute the console command.
     * @return void
     */
    public function handle()
    {
        // Classic symfony output methods
        $this->output->newLine();
        if (!$this->argument('commandType') || $this->argument('commandType') === 'symfony') {
            $this->symfonyCommands();
        }


        // Laravel output methods
        $this->output->newLine();
        if (!$this->argument('commandType') || $this->argument('commandType') === 'laravel') {
            $this->laravelCommands();
        }

        // Demonstrate styles
        $this->output->newLine();
        if (!$this->argument('commandType') || $this->argument('commandType') === 'styles') {
            $this->demonstrateStyles();
        }

        return Command::SUCCESS;
    }


    /**
     * Display symfony console commands.
     */
    private function symfonyCommands(): void
    {
        // Title
        $this->output->block('>> title : $this->output->title', null, 'fg=yellow');
        $this->output->title('Lorem ipsum dolor sit amet');
        $this->newLine();

        // Section
        $this->output->block('>> section : $this->output->section', null, 'fg=yellow');
        $this->output->section('Adding a User');

        // Text
        $this->output->block('>> text : $this->output->text', null, 'fg=yellow');
        $this->output->text('Lorem ipsum dolor sit amet');
        $this->newLine();
        // ...
        $this->output->block('>> text (multi-line) : $this->output->text', null, 'fg=yellow');
        $this->output->text([
            'Lorem ipsum dolor sit amet',
            'Consectetur adipiscing elit',
            'Aenean sit amet arcu vitae sem faucibus porta',
        ]);
        $this->newLine();

        // Listing
        $this->output->block('>> listing : $this->output->listing', null, 'fg=yellow');
        $this->output->listing([
            'Element #1 Lorem ipsum dolor sit amet',
            'Element #2 Lorem ipsum dolor sit amet',
            'Element #3 Lorem ipsum dolor sit amet',
        ]);

        // Table
        $this->output->block('>> table : $this->table or $this->output->table', null, 'fg=yellow');
        $this->table(
            ['Header 1', 'Header 2'],
            [
                ['Cell 1-1', 'Cell 1-2'],
                ['Cell 2-1', 'Cell 2-2'],
                ['Cell 3-1', 'Cell 3-2'],
            ]
        );
        // ... Horizontal table
        $this->output->block('>> horizontalTable : $this->output->horizontalTable', null, 'fg=yellow');
        $this->output->horizontalTable(
            ['Header 1', 'Header 2'],
            [
                ['Cell 1-1', 'Cell 1-2'],
                ['Cell 2-1', 'Cell 2-2'],
                ['Cell 3-1', 'Cell 3-2'],
            ]
        );

        // Definition List
        $this->output->block('>> definitionList : $this->output->definitionList', null, 'fg=yellow');
        $this->output->definitionList(
            'This is a title',
            ['foo1' => 'bar1'],
            ['foo2' => 'bar2'],
            ['foo3' => 'bar3'],
            new TableSeparator(),
            'This is another title',
            ['foo4' => 'bar4']
        );

        // Tree - only available in Symfony 7.3+
        // $this->output->block('>> tree : $this->tree', null, 'fg=yellow');
        // $this->tree([
        //     'src' =>  [
        //         'Controller' => [
        //             'DefaultController.php',
        //         ],
        //         'Kernel.php',
        //     ],
        //     'templates' => [
        //         'base.html.twig',
        //     ],
        // ]);

        // Note
        $this->output->block('>> note : $this->output->note', null, 'fg=yellow');
        $this->output->note('Lorem ipsum dolor sit amet');
        // ...
        $this->output->block('>> note (multi-line) : $this->output->note', null, 'fg=yellow');
        $this->output->note([
            'Lorem ipsum dolor sit amet',
            'Consectetur adipiscing elit',
            'Aenean sit amet arcu vitae sem faucibus porta',
        ]);

        // Caution
        $this->output->block('>> caution : $this->output->caution', null, 'fg=yellow');
        $this->output->caution('Lorem ipsum dolor sit amet');
        // ...
        $this->output->block('>> caution (multi-line) : $this->output->caution', null, 'fg=yellow');
        $this->output->caution([
            'Lorem ipsum dolor sit amet',
            'Consectetur adipiscing elit',
            'Aenean sit amet arcu vitae sem faucibus porta',
        ]);

        // Success
        $this->output->block('>> success : $this->output->success', null, 'fg=yellow');
        $this->output->success('Lorem ipsum dolor sit amet');
        // ...
        $this->output->block('>> success (multi-line) : $this->output->success', null, 'fg=yellow');
        $this->output->success([
            'Lorem ipsum dolor sit amet',
            'Consectetur adipiscing elit',
        ]);

        // Info
        $this->output->block('>> info : $this->info or $this->output->info', null, 'fg=yellow');
        $this->info('Lorem ipsum dolor sit amet');
        // ...
        $this->output->block('>> info (multi-line) : $this->output->info', null, 'fg=yellow');
        $this->output->info([
            'Lorem ipsum dolor sit amet',
            'Consectetur adipiscing elit',
        ]);

        // Warning
        $this->output->block('>> warning : $this->output->warning', null, 'fg=yellow');
        $this->output->warning('Lorem ipsum dolor sit amet');
        // ...
        $this->output->block('>> warning (multi-line) : $this->output->warning', null, 'fg=yellow');
        $this->output->warning([
            'Lorem ipsum dolor sit amet',
            'Consectetur adipiscing elit',
        ]);

        // Error
        $this->output->block('>> error : $this->error', null, 'fg=yellow');
        $this->error('Lorem ipsum dolor sit amet');
        // ...
        $this->output->block('>> error (multi-line) : $this->output->error', null, 'fg=yellow');
        $this->output->error([
            'Lorem ipsum dolor sit amet',
            'Consectetur adipiscing elit',
        ]);

        // Ask
        $this->output->block('>> ask : $this->ask', null, 'fg=yellow');
        $name = $this->ask('What is your name?', 'John Doe');
        $this->line('<info>Your name is: ' . $name . '</info>');
        $this->newLine();

        // Choice
        $this->output->block('>> choice : $this->choice', null, 'fg=yellow');
        $burger = $this->choice(
            'What is your favorite burger?',
            ['cheeseburger' => 'Cheeseburger', 'veggie' => 'Veggie Burger', 'chicken' => 'Chicken Burger', 'fish' => 'Fish Burger'],
            'cheeseburger'
        );
        $this->line('<info>Your favorite burger is: ' . $burger . '</info>');
        $this->newLine();
        //...

        $this->output->block('>> choice (multiple) : $this->choice', null, 'fg=yellow');
        $burger = $this->choice(
            'What are your favorite burgers?',
            ['cheeseburger' => 'Cheeseburger', 'veggie' => 'Veggie Burger', 'chicken' => 'Chicken Burger', 'fish' => 'Fish Burger'],
            'cheeseburger',
            null,
            true // Enable multiple selections
        );
        $this->line('<info>Your favorite burgers are: ' . implode(', ', $burger) . '</info>');
        $this->newLine();
    }

    /**
     * Display Laravel console commands.
     */
    private function laravelCommands(): void
    {
        // Info
        $this->output->block('>> info : $this->components->info', null, 'fg=yellow');
        $this->components->info('Lorem ipsum dolor sit amet');
        $this->output->newLine();

        // Warning
        $this->output->block('>> warning : $this->components->warn', null, 'fg=yellow');
        $this->components->warn('Lorem ipsum dolor sit amet');
        $this->output->newLine();

        // Error
        $this->output->block('>> error : $this->components->error', null, 'fg=yellow');
        $this->components->error('Lorem ipsum dolor sit amet');
        $this->output->newLine();

        // Alert
        $this->output->block('>> alert : $this->components->alert', null, 'fg=yellow');
        $this->components->alert('Lorem ipsum dolor sit amet');
        $this->output->newLine();

        // Task
        $this->output->block('>> task : $this->components->task', null, 'fg=yellow');
        $this->components->task('Lorem ipsum dolor sit amet', fn () => $this->fakeCallable());
        $this->output->newLine();

        // Two columns details
        $this->output->block('>> two columns : $this->components->TwoColumnDetail', null, 'fg=yellow');
        $this->components->TwoColumnDetail(
            'Column 1: Lorem ipsum dolor sit amet',
            'Column 2: Consectetur adipiscing elit'
        );
        $this->output->newLine();

        // Bullet list
        $this->output->block('>> bullet list : $this->components->bulletList', null, 'fg=yellow');
        $this->components->bulletList([
            'Lorem ipsum dolor sit amet',
            'Consectetur adipiscing elit',
            'Aenean sit amet arcu vitae sem faucibus porta',
        ]);
        $this->output->newLine();

        // Ask
        $this->output->block('>> ask : $this->components->ask', null, 'fg=yellow');
        $name = $this->components->ask('What is your name?', 'John Doe');
        $this->output->writeln('<info>Your name is: ' . $name . '</info>');
        $this->output->newLine();

        // Ask with completion
        $this->output->block('>> ask with completion : $this->components->askWithCompletion', null, 'fg=yellow');
        $color = $this->components->askWithCompletion(
            'What is your favorite color?',
            ['red', 'green', 'blue', 'yellow'],
            'blue'
        );
        $this->output->writeln('<info>Your favorite color is: ' . $color . '</info>');
        $this->output->newLine();

        // Choice
        $this->output->block('>> choice : $this->components->choice', null, 'fg=yellow');
        $burger = $this->components->choice(
            'What is your favorite burger?',
            ['cheeseburger' => 'Cheeseburger', 'veggie' => 'Veggie Burger', 'chicken' => 'Chicken Burger', 'fish' => 'Fish Burger'],
            'cheeseburger'
        );
        $this->output->writeln('<info>Your favorite burger is: ' . $burger . '</info>');
        $this->output->newLine();
        //...

        $this->output->block('>> choice (multiple) : $this->components->choice', null, 'fg=yellow');
        $burger = $this->components->choice(
            'What are your favorite burgers?',
            ['cheeseburger' => 'Cheeseburger', 'veggie' => 'Veggie Burger', 'chicken' => 'Chicken Burger', 'fish' => 'Fish Burger'],
            'cheeseburger',
            null,
            true // Enable multiple selections
        );
        $this->output->writeln('<info>Your favorite burgers are: ' . implode(', ', $burger) . '</info>');
        $this->output->newLine();

        // Confirm
        $this->output->block('>> confirm : $this->components->confirm', null, 'fg=yellow');
        $confirmed = $this->components->confirm('Do you feel good about his demo?', true);
        $this->output->writeln('<info>' . ($confirmed ? 'Nice that you feel good!' : 'Sorry to hear that.') . '</info>');
        $this->output->newLine();
    }


    private function demonstrateStyles(): void
    {
        $reflect = new \ReflectionClass(Color::class);
        $availableColorsAndOptions = $reflect->getConstants(\ReflectionProperty::IS_PRIVATE);
        $lines= [];

        foreach ($availableColorsAndOptions as $key => $options) {
            $lines[] = '';
            $lines[] = $key;
            $lines[] = '-------------------------------';

            foreach ($options as $color => $value) {
                switch ($key) {
                    case 'COLORS':
                        $lines[] = "<fg={$color}>{$color}</>";
                        break;
                    case 'BRIGHT_COLORS':
                        $lines[] = "<fg={$color}>{$color}</>";
                        break;
                    case 'AVAILABLE_OPTIONS':
                        $lines[] = "<options={$color}>{$color}</>";
                        break;
                }
            }
        }

        $this->output->text($lines);

        $styles = [
            '',
            'Styles',
            '-------------------------------',
            '<info>info</info>',
            '<comment>comment</comment>',
            '<question>question</question>',
            '<error>error</error>',
        ];

        $this->output->text($styles);

        $custom = [
            '',
            'Custom combos',
            '-------------------------------',
            '<fg=blue;options=blink;bg=yellow>blue text on yellow background</>',
            'Clickable URL: <fg=bright-blue;href=https://gist.github.com/damsfx/19c094fa37af0edeb961e95c84b7d36e;>Edit this gist on github.com</>',
        ];

        $this->output->text($custom);
    }

    /**
     * Simulate a callable function for demonstration purposes.
     * This is just a placeholder to simulate some processing time.
     */
    private function fakeCallable(): void
    {
        // Simulate a delay with a random sleep
        usleep(rand(100000, 500000));
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment