Terminal: An Elegant Wrapper around the Symfony Process Component
Published on by Paul Redmond
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:
Terminal::in(storage_path('sites/123')) ->timeout(120) ->retries(3) ->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$response->throw(); // You can access the symfony process$response->process(); // All calls not found on the Response are passed to Symfony process// \Symfony\Component\Process\Process::isRunning()$response->isRunning();
Here are a few more neat examples from the readme:
// Passing data to the commandTerminal::with([ 'firstname' => 'John', 'lastname' => 'Doe',])->run('echo Hello, {{ $firstname}} {{ $lastname }}'); // Retries and duration between each retryTerminal::retries(3, 100)->run('rm -rf vendor'); // Environment variablesTerminal::withEnvironmentVariables([ '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.