Download the PHP package kamermans/command without Composer
On this page you can find all versions of the php package kamermans/command. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kamermans/command
More information about kamermans/command
Files in kamermans/command
Package command
Short Description External command runner / executor for PHP
License BSD
Informations about the package command
kamermans/command
External command runner / executor for PHP. This is an object oriented, robust replacement for exec
, shell_exec
, the backtick operator and the like.
This package does not work reliably in Windows due to a lack of complete proc_open()
support in PHP
This package is inspired by http://pollinimini.net/blog/php-command-runner/.
Running Commands
At its simplest form, you can execute commands like this:
Adding Arguments and Options
Here we are safely adding arguments:
Using a Callback for Incremental Updates
Normally all command output is buffered and once the command completes you can access it. By using a callback, the output is buffered until the desired number of bytes is received (see Command::setReadBuffer(int $bytes)
), then it is passed to your callback function:
Alternately, you can set the second argument for Command::run(string $stdin, bool $lines)
to true
to execute your callback once for every line of output:
Streaming large command output
The STDOUT and STDERR is collected inside PHP by default. If you have a large amount of data to pass into the command, you should stream it in (see STDIN from a stream below). If you have a large amount of output from the command, you should stream it out using a callback:
Running a Command without Escaping
By default, the command passed to Command::factory(string $command, bool $escape)
is escaped, so characters like |
and >
will replaced with \|
and \>
respectively. To prevent the command factory from escaping your command, you can pass true
as the second argument:
Outputting to STDERR
To output content to your STDERR
there is a helper function Command::echoStdErr(string $content)
:
Using STDIN
You can provide data for STDIN using a string or a stream resource (like a file handle)
STDIN from a String
STDIN from a Stream
Your system's STDIN
is also a stream, so you can accept input that is typed on the command line or piped into your script as well:
Some more features:
StdIn
data can be provided to the process as a parameter torun()
- Set environment variables for the process with
setEnv()
- Second argument to
option()
and argument toargument()
are automatically escaped. - Options separator is white space by default, it can be changed by manually setting it as third argument to
option()
or setting a new default withsetOptionSeparator()
. - The
proc_open
wrapper is exposed as a static method for your convenienceCommand::exec()