PHP code example of popphp / pop-console

1. Go to this page and download the library: Download popphp/pop-console library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

popphp / pop-console example snippets


use Pop\Console\Console;

$console = new Console();
$console->setHeader('My Application'); // Set a global header at the start of the script
$console->setFooter('The End');        // Set a global footer at the end of the script

$console->append('Here is some console information.');
$console->append('Hope you enjoyed it!');
$console->send();

use Pop\Console\Console;

$console = new Console(40, 2); // wrap width of 40, margin of 2 spaces
$console->append(
    'Here is some console information. This is a really long string. It will have to wrap.'
);
$console->send();

use Pop\Console\Console;

$console = new Console(40);
$console->write(
    'Here is some console information. This is a really long string. It will have to wrap.'
);

use Pop\Console\Console;

$console = new Console(40);
$console->write('Here ', false);          // No new line, but use margin
$console->write('is ', false, false);     // No new line, no margin
$console->write('some ', false, false);   // No new line, no margin
$console->write('content.', true, false); // Use new line, but no margin

use Pop\Console\Console;
use Pop\Console\Color;

$console = new Console();
$console->write(
    'Here is some ' . 
    $console->colorize('IMPORTANT', Color::BOLD_RED) .
    ' console information.'
);

use Pop\Console\Console;
use Pop\Console\Color;

$console = new Console();
$console->write(
    'Here is some ' . 
    Color::colorize('IMPORTANT', Color::BOLD_RED) .
    ' console information.'
);

use Pop\Console\Console;

$console = new Console();
$console->line();

use Pop\Console\Console;

$console = new Console();
$console->line('=', 20);

use Pop\Console\Console;

$console = new Console(80);
$console->header('Hello World');

use Pop\Console\Console;

$console = new Console();
$console->header('Hello World', '=', 40, 'center');

use Pop\Console\Console;

$console = new Console();
$console->headerCenter('Hello World', '=');

use Pop\Console\Console;

$console = new Console(40);
$console->alertDanger('Hello World', 'auto');
$console->alertWarning('Hello World', 'auto');
$console->alertSuccess('Hello World', 'auto');
$console->alertInfo('Hello World', 'auto');
$console->alertPrimary('Hello World', 'auto');
$console->alertSecondary('Hello World', 'auto');
$console->alertDark('Hello World', 'auto');
$console->alertLight('Hello World', 'auto');
$console->alertBox('Hello World', '-', '|', 'auto');

use Pop\Console\Console;

$console = new Console();
$name    = $console->prompt('Please provide your name: ');
$console->write('Hello ' . $name . '!');

use Pop\Console\Console;

$console = new Console();
$letter  = $console->prompt(
    'Which is your favorite letter: A, B, C, or D? ',
    ['A', 'B', 'C', 'D'],
    true
);
$console->write('Your favorite letter is ' . $letter . '.');

// Returns an array of the selected values
$types = $console->promptMulti('Select one or more, comma-separated: ', ['1', '2', '3']);

use Pop\Console\Console;

$console = new Console();
$console->confirm();
$console->write('The user said yes.');

use Pop\Console\Console;

$stream = fopen('php://memory', 'r+');
fwrite($stream, 'Nick' . PHP_EOL);
rewind($stream);

$console = new Console();
$console->setInputStream($stream);

$name = $console->prompt('Please provide your name: '); // 'Nick', no real TTY 

use Pop\Console\Console;
use Pop\Console\Color;

$console = new Console();

$console->table(
    ['Name', 'Status'],
    [
        ['kettle', 'active'],
        ['brew',   'idle'],
    ],
    '-', '|', Color::BOLD_GREEN
);

$console->table([], [['a', 'bb'], ['ccc', 'd']], '-', null);

use Pop\Console\Console;

$console = new Console();
$bar     = $console->progressBar(100, 'Processing');

foreach ($items as $i => $item) {
    // do work
    $bar->advance();
}

$bar->finish();

use Pop\Console\Console;

$console = new Console();

$console->isColor();            // Whether the terminal's TERM env var indicates color support
$console->isWindows();          // Whether the environment is Windows
$console->getAvailableColors(); // Associative array of all Color::* constant names and values
$console->clear();              // Clears the terminal screen

$console->getServer();          // The full $_SERVER array captured at construction
$console->getServer('argv');    // A single $_SERVER key, or null if not set
$console->getEnv();             // The full $_ENV array captured at construction
$console->getEnv('APP_ENV');    // A single $_ENV key, or null if not set

use Pop\Console\Console;
use Pop\Console\Command;

$command1 = new Command(name: 'users');
$command1->setParams('--list [<id>]');
$command1->setHelp('This is the users help screen');

$command2 = new Command(name: 'roles');
$command2->setParams('--list [<id>]');
$command2->setHelp('This is the roles help screen');

$console = new Console();
$console->addCommand($command1);
$console->addCommand($command2);

// Or add several at once:
$console->addCommands([$command1, $command2]);

$console->hasCommand('users');    // true
$console->getCommand('users');    // the $command1 object, or null if not found
$console->getCommands();          // ['users' => $command1, 'roles' => $command2]

use Pop\Console\Command;

class UsersCommand extends Command
{
    public function handle(string $id): void
    {
        $this->console()->write('Showing user ' . $id);
    }
}

$command = new UsersCommand(name: 'users', params: '--list [<id>]', help: 'This is the users help screen');
$command->dispatch(null, ['123']); // calls handle('123')

use Pop\Console\Command;
use Pop\Console\Console;

$command = new Command($application, new Console(120), 'users', '--list [<id>]', 'This is the users help screen');

$command->hasApplication(); // true
$command->hasConsole();     // true

use Pop\Console\Command;
use Pop\Console\CommandRegistry;

$registry = new CommandRegistry();
$registry->add(new Command(name: 'users', params: '--list [<id>]', help: 'This is the users help screen'));

$registry->has('users');  // true
$registry->get('users');  // the Command object
$registry->all();         // ['users' => $command]

use Pop\Console\CommandRegistry;

$routes = CommandRegistry::loadRoutes([
    'help' => [
        'controller' => 'MyApp\Command\HelpCommand',
        'action'     => 'handle',
    ],
], __DIR__ . '/src/Command');

$console->help();

$console->help('users'); // 'This is the users help screen'

$console->addCommands([
    new Command(name: 'db:migrate', help: 'Migrate the database'),
    new Command(name: 'db:seed', help: 'Seed the database'),
    new Command(name: 'user:list', help: 'List users'),
]);

$console->help(null, false, 'db');

use Pop\Console\Console;

$this->console->addCommandsFromRoutes($cliRouteMatch, './myapp');

    public function __construct(Application $application, Console $console)
    {
        $this->application = $application;
        $this->console     = $console;

        $this->console->setHelpColors(
            Color::BOLD_CYAN, Color::BOLD_GREEN, Color::BOLD_MAGENTA
            );
        $this->console->addCommandsFromRoutes(
            $application->router()->getRouteMatch(), './kettle'
        );
    }

    public function help()
    {
        $this->console->help();
    }