Shell
in package
Utilities for doing shell things.
Table of Contents
Methods
- escape() : string
- Safely interpolate command arguments.
- run() : ShellOutput
- Create a command object.
- runAsync() : Generator<string|int, string>
- Execute a command and yield each line.
- runSync() : string
- Execute a command as a whole block.
Methods
escape()
Safely interpolate command arguments.
public
static escape(string $cmd, array<string|int, mixed> $args) : string
Use an 'args' array like:
// 'ping {0}'
['google.com']
// 'ping {ip}'
['ip' => 'google.com']
Note, this does not escape the command components. However, I can't imagine why you would give that control to a user. If you have or even plan to - you should be shot.
Parameters
- $cmd : string
-
template string
- $args : array<string|int, mixed>
-
keyed args
Return values
string —escaped cmd string
run()
Create a command object.
public
static run(string|array<string|int, mixed>|ShellOptions $config) : ShellOutput
This begins executing a shell command, but then it's your responsibility to process the output (with read/readAll) and close the handle.
Note, proc_close() is automatically called on shutdown.
Use this for increased flexibility:
- pipe stdout/stderr directly to files or sockets
- pipe or wrote to the stdin
- read the output line by line and stream it somewhere
- read the exit code
- terminate a command
Parameters
- $config : string|array<string|int, mixed>|ShellOptions
-
- cmd -
string - args -
string[] - cwd -
string - env -
string[]
- cmd -
Return values
ShellOutputrunAsync()
Execute a command and yield each line.
public
static runAsync(string $dir, string $cmd, mixed ...$args) : Generator<string|int, string>
The function returns an iterable (generator) of output. Permitting one to loop over the output and procedurally do whatever they please with it.
Parameters
- $dir : string
- $cmd : string
- $args : mixed
Return values
Generator<string|int, string>runSync()
Execute a command as a whole block.
public
static runSync(string $dir, string $cmd, mixed ...$args) : string
This function returns the output as big fat string.
Parameters
- $dir : string
- $cmd : string
- $args : mixed