Integrate "Symfony Process" component [5.3.0-B1]
The Symfony Process Component (see http://symfony.com/doc/current/components/process.html) allows to manipulate processes with ease, which includes, but not limited to:
- create new process
- running processes in parallel
- getting error output of the process
- getting exit code of the process
- get output from process in real time (useful for long running commands)
One side effect from is increasing In-Portal minimal needed version from 5.3.2 to 5.3.9 because the mentioned component needs this.
Solution
- connect the "symfony/process" package in "composer.json"
- create "ProcessHelper" class with "getProcess(...)" method, that would create Symfony Process class instance
- increase minimum PHP version dependency to 5.3.9
Documentation
The usual working cycle with ProcessBuilder
class looks like this:
- create
ProcessBuilder
class instance - set necessary parameters to the builder object
- get the
Process
class instance back - do whatever needed with the process (e.g. run it and get output or something else)
// 1. creates process builder instance /** @var ProcessBuilder $process_builder */ $process_builder = $this->Application->makeClass('ProcessBuilder'); // 2. sets necessary parameters via dedicated method $process_builder->... // 3. creates the process (don't execute the actual command yet) $process = $process_builder->build(); // 4. starts the process $process->mustRun(); // 5. gets the output echo $process->getOutput();
Then there are 2 ways for starting process configuration:
- specify full command to be executed (including arguments) via "
setCommand
" method call - specify executable via "
setExecutable
" method and then add arguments to it via "add
" method calls
The later approach is preferred, because it automatically escapes argument values to prevent code injections (e.g. if somebody adds "; rm -Rf /" as one of arguments, then it won't allow "rm" command to be executed). Here are the example of both approaches:
// Example with "setCommand" $process = $process_builder->setCommand('ls -la')->build(); // Example with "setExecutable" and "add" $process = $process_builder->setExecutable('ls')->add('-la')->build();
In above examples the "build
" method is used to immediately build the process with specified configuration.
These are other configuration possibilities:
setWorkingDirectory()
method changes working directory of the process (before it's started)setEnv()
method allows to add/remove environment variables that would be accessible to the processsetInput()
method allows to set input stream for the process to make it interactive (e.g. ask user confirmation)setOption()
method allows to set "proc_open" function specific options (the "proc_open" function is used to start the process)setTimeout()
method allows to set timeout after which process will be terminated automatically (defaults to 60 seconds)
Thanks to fluent interface support the process can be configured with just one line of code (as seen in above examples).