PHP code example of synapse / php-shell

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

    

synapse / php-shell example snippets


$pwd = new Neuron\Shell\Command('/bin/pwd'); // CommandNotFoundException if the command is not executable
$pwd->setWorkingDirectory('/tmp'); // DirectoryDoesNotExistException if the directory is not found
$output = $pwd->execute();
var_dump($output->getStdout()); // string(5) "/tmp"
var_dump($output->getStderr()); // string(0) ""
var_dump($output->getExitCode()); // int(0)

$cmd = new Neuron\Shell\Command('/usr/local/bin/my_command');
$cmd->addShortFlag('v');
$cmd->addShortFlag('c', '/tmp/test.json');
$cmd->addLongFlag('version');
$cmd->addLongFlag('config', '/tmp/test.json');

$echo = new Neuron\Shell\Command('/bin/echo');
$echo->addArgument('hi there!');

$cmd = new Neuron\Shell\Command('/usr/local/bin/my_command');
$cmd2 = new Neuron\Shell\Command('/usr/local/bin/my_command2');
$cmd->pipe($cmd2);
echo $cmd->getCmdString(); // "/usr/local/bin/my_command | /usr/local/bin/my_command2"

$echo = new \Neuron\Shell\Command('/bin/echo');
$echo->addArgument('fruit banana');
$output = $echo->execute();
var_dump($output->wasSuccessful()); // bool(true)
var_dump($output->stdoutContains('apple')); // bool(false)
var_dump($output->stdoutContains('banana')); // bool(true)