Terminal: An Elegant Wrapper around the Symfony Process Component


March 27th, 2020

Terminal: An Elegant Wrapper around the Symfony Process Component

Terminal is a neat PHP package by Titas Gailius that provides a “wrapper around Symfony’s Process component.” It has a fluent interface, which makes running terminal commands a breeze:

->run('wp cli update');

In the most simple form, a command might look like the following—which removes a lot of boilerplate and lower-level details of the wonderful Symfony Process component:

$response = Terminal::run('rm -rf vendor');

Here are some things you can do with the response from the run command:

$response->code() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->lines() : array;
$response->output() : string;
(string) $response: string;
// Terminal captures exceptions that you can throw
// You can access the symfony process
// All calls not found on the Response are passed to Symfony process
// \Symfony\Component\Process\Process::isRunning()

Here are a few more neat examples from the readme:

// Passing data to the command
'firstname' => 'John',
'lastname' => 'Doe',
])->run('echo Hello, {{ $firstname}} {{ $lastname }}');
// Retries and duration between each retry
Terminal::retries(3, 100)->run('rm -rf vendor');
// Environment variables
'APP_ENV' => 'testing',
])->run('rm -rf $DIRECTORY');

You can learn more about this package, get full installation instructions, and view the source code on GitHub at TitasGailius/terminal.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.