PHP code example of ptlis / shell-command

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

    

ptlis / shell-command example snippets


use ptlis\ShellCommand\CommandBuilder;

$builder = new CommandBuilder();

use ptlis\ShellCommand\CommandBuilder;
use ptlis\ShellCommand\UnixEnvironment;

$builder = new CommandBuilder(new UnixEnvironment());

$command = $builder
    ->setCommand('foo')
    ->addArgument('--bar=baz')
    ->buildCommand()

$builder->setCommand('git')             // Executable in $PATH
    
$builder->setCommand('./local/bin/git') // Relative to current working directory
    
$builder->setCommand('/usr/bin/git')    // Fully qualified path

$build->setCommand('~/.script.sh')      // Path relative to $HOME

$builder
    ->setTimeout(30 * 1000 * 1000)          // Wait 30 seconds

$builder
    ->setPollTimeout(30 * 1000 * 1000)          // Wait 30 seconds


$builder
    ->setCwd('/path/to/working/directory/')

$builder
    ->addArgument('--foo=bar')

$builder
    ->addArgument('--foo=bar', $myVar === 5)

$builder
    ->addArguments([
        '--foo=bar',
        '-xzcf',
        'if=/dev/sda of=/dev/sdb'
    ])

$builder
    ->addArguments([
        '--foo=bar',
        '-xzcf',
        'if=/dev/sda of=/dev/sdb'
    ], $myVar === 5)

$builder
    ->addRawArgument("--foo='bar'")

$builder
    ->addRawArgument('--foo=bar', $myVar === 5)

$builder
    ->addRawArguments([
        "--foo='bar'",
        '-xzcf',
    ])

$builder
    ->addRawArguments([
        '--foo=bar',
        '-xzcf',
        'if=/dev/sda of=/dev/sdb'
    ], $myVar === 5)

$builder
    ->addEnvironmentVariable('TEST_VARIABLE', '123')

$builder
    ->addEnvironmentVariable('TEST_VARIABLE', '123', $myVar === 5)

$builder
    ->addEnvironmentVariables([
        'TEST_VARIABLE' => '123',
        'FOO' => 'bar'
    ])

$builder
    ->addEnvironmentVariables([
        'TEST_VARIABLE' => '123',
        'FOO' => 'bar'
    ], $foo === 5)

$builder
    ->addProcessObserver(
        new AllLogger(
            new DiskLogger(),
            LogLevel::DEBUG
        )
    )

$command = $builder
    // ...
    ->buildCommand();

$result = $command
    ->runSynchronous(); 

$result->getExitCode();         // 0 for success, anything else conventionally indicates an error
$result->getStdOut();           // The contents of stdout (as a string)
$result->getStdOutLines();      // The contents of stdout (as an array of lines)
$result->getStdErr();           // The contents of stderr (as a string)
$result->getStdErrLines();      // The contents of stderr (as an array of lines)
$result->getExecutedCommand();  // Get the executed command as a string, including environment variables
$result->getWorkingDirectory(); // Get the directory the command was executed in 

$process = $command->runAsynchronous();

if (!$process->isRunning()) {
    echo 'done' . PHP_EOL;
}

$process->stop();

$process->wait();

$process->getPid();

$stdOut = $process->readStream(ProcessInterface::STDOUT);

$process->writeInput('Data to pass to the running process via STDIN');

$exitCode = $process->getExitCode();

$process->sendSignal(ProcessInterface::SIGTERM);

    $commandString = $process->getCommand();

$eventLoop = \React\EventLoop\Factory::create();

$promise = $command->runAsynchonous()->getPromise($eventLoop);

$eventLoop->run();