PHP code example of mikhailovlab / php-fluent-console

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

    

mikhailovlab / php-fluent-console example snippets

 
public function setCommand(string $cmd): self
 
public function addKey(string $key): self

public function encoding(?string $encoding = null): self

public function decoding(): self

public function getCommand(): string

public function run(): bool

public function getOutput(): array

public function getReturnCode(): int

public function hasError(string $pattern): bool

public function getMatches(string|array $patterns): array
 
$cli = new ConsoleRunner();
$cli->setCommand('ipconfig')
    ->addKey('/all');
// Optional encoding for Cyrillic output
// ->encoding('866')
// ->decoding();

if ($cli->run()) {
    print_r($cli->getOutput());
    exit();
}

exit('Error, code: ' . $cli->getReturnCode());
 
$cli = new ConsoleRunner();
$cli->setCommand('csptest')
    ->addKey('-keyset')
    ->addKey('-enum_cont')
    ->addKey('-verifycontext')
    ->addKey('-fqcn');

if ($cli->run()) {
    // Filter output lines matching the pattern (e.g., container names)
    print_r($cli->getMatches('#\\\\.*#'));
    exit();
}

$pattern = '/\[ErrorCode:\s*(0x[0-9A-Fa-f]+)\]/';
exit('Error code: ' . $cli->getMatches($pattern)[0]);
 
class customRunner extends ConsoleRunner
{
    private $methods = [
        'keyset',
        'enum_cont',
        'verifycontext',
        'fqcn'
    ];

    public function __call(string $name, array $arguments): self
    {
        if (in_array($name, $this->methods)) {
            $this->addKey('-' . $name);

            if (!empty($arguments)) {
                foreach ($arguments as $arg) {
                    $this->addKey((string) $arg);
                }
            }

            return $this;
        }

        throw new \BadMethodCallException("Method $name not supported");
    }

}


$cli = new customRunner()
    ->setCommand('csptest')
    ->keyset()
    ->enum_cont() 
    ->verifycontext()
    ->fqcn();