Terminal: An Elegant Wrapper around the Symfony Process Component

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:

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 command
Terminal::with([
    '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
Terminal::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.

Filed in: News / packages
Laravel News Partners

Laravel Jobs

Senior Full Stack Engineer - Laravel
Remote
Givebutter
Laravel partner seeks Lead Developer
Remote
Kirschbaum Development Group
Senior Programmer **Immediate Opening** (send resume to daniel@sbgcorp.com)
Remote
SBG Corp
Web Developer - Laravel
Grand Forks, North Dakota
Center for Rural Health
Senior Full-Stack Laravel Developer
Berlin, Germany
felmo.de

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.