Terminal: An Elegant Wrapper around the Symfony Process Component

Packages

March 27th, 2020

terminal-symfony-process-wrapper.png

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:

1Terminal::in(storage_path('sites/123'))
2 ->timeout(120)
3 ->retries(3)
4 ->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:

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

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

1$response->code() : int;
2$response->ok() : bool;
3$response->successful() : bool;
4 
5$response->lines() : array;
6$response->output() : string;
7(string) $response: string;
8 
9// Terminal captures exceptions that you can throw
10$response->throw();
11 
12// You can access the symfony process
13$response->process();
14 
15// All calls not found on the Response are passed to Symfony process
16// \Symfony\Component\Process\Process::isRunning()
17$response->isRunning();

Here are a few more neat examples from the readme:

1// Passing data to the command
2Terminal::with([
3 'firstname' => 'John',
4 'lastname' => 'Doe',
5])->run('echo Hello, {{ $firstname}} {{ $lastname }}');
6 
7// Retries and duration between each retry
8Terminal::retries(3, 100)->run('rm -rf vendor');
9 
10// Environment variables
11Terminal::withEnvironmentVariables([
12 'APP_ENV' => 'testing',
13])->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.