PHP code example of utopia-php / console

1. Go to this page and download the library: Download utopia-php/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/ */

    

utopia-php / console example snippets



Utopia\Console\Console;
use Utopia\Console\Command;

Console::success('Ready to work!');

$answer = Console::confirm('Continue? [y/N]');

if ($answer !== 'y') {
    Console::warning('Aborting...');
    Console::exit(1);
}

$output = '';
$stderr = '';
$command = new Command(PHP_BINARY)
    ->option('-r', 'echo "Hello";');

$exitCode = Console::execute($command, '', $output, $stderr, 3);

Console::log("Command returned {$exitCode} with: {$output}");

Console::log('Plain log');        // stdout
Console::success('Green log');    // stdout
Console::info('Blue log');        // stdout
Console::warning('Yellow log');   // stderr
Console::error('Red log');        // stderr

$command = new Command(PHP_BINARY)
    ->option('-r', 'fwrite(STDOUT, "success\\n");');

$output = '';
$input = '';
$stderr = '';
$exitCode = Console::execute($command, $input, $output, $stderr, 3);

echo $exitCode;  // 0
echo $output;    // "success\n"

$command = new Command('tar')
    ->flag('-cz')
    ->option('-f', 'archive.tar.gz')
    ->option('-C', '/tmp/project')
    ->argument('.');

$pipeline = Command::pipe(
    new Command('ps')->flag('-ef'),
    new Command('grep')->argument('php-fpm'),
    new Command('wc')->flag('-l'),
);

$deploy = Command::and(
    Command::group(
        Command::or(
            new Command('build'),
            new Command('build:fallback'),
        )
    ),
    new Command('publish'),
);

$logs = Command::appendStdout(
    Command::pipe(
        new Command('cat')->argument('app.log'),
        new Command('grep')->argument('ERROR'),
    ),
    'errors.log',
);



use Utopia\Console\Console;

Console::loop(function () {
    echo "Hello World\n";
}, 1); // 1 second