PHP code example of mikoweb / php-cli-executor

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

    

mikoweb / php-cli-executor example snippets




use Mikoweb\CLIExecutor\Executor;
use Mikoweb\CLIExecutor\Config;
use Mikoweb\CLIExecutor\Validator\Validator;
use Mikoweb\CLIExecutor\Validator\Exceptions\InvalidOutputException;

$config = new Config(__DIR__ . '/sample-cli.php'); // set path to your CLI script
// $config = new Config('cli_path', 'php_bin_or_other'); // you can set php bin path

$executor = new Executor($config);
$validator = new Validator();

$output = $executor->execute(['app:test']); // set command options, arguments etc.

try {
    $validator->validate($output);
    // $validator->validate($output, false); // if set false in second argument method not throw exception and return ValidationResultInterface

    echo $output->isSuccessful());
    echo $output->getStatus()); // output status code like http
    echo $output->getData()->get('message')); // you can get result property
    // echo $output->getData()->getData(); // or you can get full data
} catch (InvalidOutputException $exception) {
    echo $output->getErrorMessage());
    echo $output->getStatus());
    
    // you can access to $exception->getValidationResult(), $exception->getMessage(), $exception->getCode() etc. 
}



use Mikoweb\CLIExecutor\Writer\WriterBuilder;

$writer = new WriterBuilder();
$writer
    ->setMessage('ok')
    ->printMessageAsSuccess(true)
    ->write()
;

exit(0);

$writer = new WriterBuilder();
$writer
    ->setErrorMessage("something's wrong")
    ->printErrorMessageAsError(true)
    ->setStatus(OutputStatus::STATUS_INTERNAL_ERROR)
    ->write()
;

echo '
Lorem ipsum // unnecessary, example

<output>
{
    "message": "ok",
    "status": 200
}
</output>

Lorem ipsum // unnecessary, example
';

exit(0);

echo '
<output>
{
    "error_message": "something\'s wrong",
    "status": 500
}
</output>
';

exit(0);

exit(1);

class XmlOutputParser extends AbstractOutputParser
{
    public function decode(string $data): array
    {
        return simplexml_to_assoc(simplexml_load_string($data));
    }
}

$executor = new Executor($config, new XmlOutputParser());

class XmlSerializer implements SerializerInterface
{
    public function serialize(array $data): string
    {
        return JMS::serialize($data, 'xml');
    }
}

$writer = new WriterBuilder(new XmlSerializer());