2015-08-18 00:00:26 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Process ;
2018-11-23 12:29:20 +00:00
@ trigger_error ( sprintf ( 'The %s class is deprecated since Symfony 3.4 and will be removed in 4.0. Use the Process class instead.' , ProcessBuilder :: class ), E_USER_DEPRECATED );
2015-08-18 00:00:26 +00:00
use Symfony\Component\Process\Exception\InvalidArgumentException ;
use Symfony\Component\Process\Exception\LogicException ;
/**
* @ author Kris Wallsmith < kris @ symfony . com >
2018-11-23 12:29:20 +00:00
*
* @ deprecated since version 3.4 , to be removed in 4.0 . Use the Process class instead .
2015-08-18 00:00:26 +00:00
*/
class ProcessBuilder
{
private $arguments ;
private $cwd ;
private $env = array ();
private $input ;
private $timeout = 60 ;
2018-11-23 12:29:20 +00:00
private $options ;
2015-08-18 00:00:26 +00:00
private $inheritEnv = true ;
private $prefix = array ();
private $outputDisabled = false ;
/**
* @ param string [] $arguments An array of arguments
*/
public function __construct ( array $arguments = array ())
{
$this -> arguments = $arguments ;
}
/**
* Creates a process builder instance .
*
* @ param string [] $arguments An array of arguments
*
2017-02-03 00:28:38 +00:00
* @ return static
2015-08-18 00:00:26 +00:00
*/
public static function create ( array $arguments = array ())
{
return new static ( $arguments );
}
/**
* Adds an unescaped argument to the command string .
*
* @ param string $argument A command argument
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*/
public function add ( $argument )
{
$this -> arguments [] = $argument ;
return $this ;
}
/**
* Adds a prefix to the command string .
*
* The prefix is preserved when resetting arguments .
*
* @ param string | array $prefix A command prefix or an array of command prefixes
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*/
public function setPrefix ( $prefix )
{
2018-11-23 12:29:20 +00:00
$this -> prefix = \is_array ( $prefix ) ? $prefix : array ( $prefix );
2015-08-18 00:00:26 +00:00
return $this ;
}
/**
* Sets the arguments of the process .
*
* Arguments must not be escaped .
* Previous arguments are removed .
*
* @ param string [] $arguments
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*/
public function setArguments ( array $arguments )
{
$this -> arguments = $arguments ;
return $this ;
}
/**
* Sets the working directory .
*
2018-11-23 12:29:20 +00:00
* @ param string | null $cwd The working directory
2015-08-18 00:00:26 +00:00
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*/
public function setWorkingDirectory ( $cwd )
{
$this -> cwd = $cwd ;
return $this ;
}
/**
* Sets whether environment variables will be inherited or not .
*
* @ param bool $inheritEnv
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*/
public function inheritEnvironmentVariables ( $inheritEnv = true )
{
$this -> inheritEnv = $inheritEnv ;
return $this ;
}
/**
* Sets an environment variable .
*
* Setting a variable overrides its previous value . Use `null` to unset a
* defined environment variable .
*
* @ param string $name The variable name
2018-11-23 12:29:20 +00:00
* @ param string | null $value The variable value
2015-08-18 00:00:26 +00:00
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*/
public function setEnv ( $name , $value )
{
$this -> env [ $name ] = $value ;
return $this ;
}
/**
* Adds a set of environment variables .
*
* Already existing environment variables with the same name will be
* overridden by the new values passed to this method . Pass `null` to unset
* a variable .
*
* @ param array $variables The variables
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*/
public function addEnvironmentVariables ( array $variables )
{
$this -> env = array_replace ( $this -> env , $variables );
return $this ;
}
/**
* Sets the input of the process .
*
2018-11-23 12:29:20 +00:00
* @ param resource | string | int | float | bool | \Traversable | null $input The input content
2015-08-18 00:00:26 +00:00
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*
* @ throws InvalidArgumentException In case the argument is invalid
*/
public function setInput ( $input )
{
2017-02-03 00:28:38 +00:00
$this -> input = ProcessUtils :: validateInput ( __METHOD__ , $input );
2015-08-18 00:00:26 +00:00
return $this ;
}
/**
* Sets the process timeout .
*
* To disable the timeout , set this value to null .
*
* @ param float | null $timeout
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*
* @ throws InvalidArgumentException
*/
public function setTimeout ( $timeout )
{
if ( null === $timeout ) {
$this -> timeout = null ;
return $this ;
}
$timeout = ( float ) $timeout ;
if ( $timeout < 0 ) {
throw new InvalidArgumentException ( 'The timeout value must be a valid positive integer or float number.' );
}
$this -> timeout = $timeout ;
return $this ;
}
/**
* Adds a proc_open option .
*
* @ param string $name The option name
* @ param string $value The option value
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*/
public function setOption ( $name , $value )
{
$this -> options [ $name ] = $value ;
return $this ;
}
/**
* Disables fetching output and error output from the underlying process .
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*/
public function disableOutput ()
{
$this -> outputDisabled = true ;
return $this ;
}
/**
* Enables fetching output and error output from the underlying process .
*
2017-02-03 00:28:38 +00:00
* @ return $this
2015-08-18 00:00:26 +00:00
*/
public function enableOutput ()
{
$this -> outputDisabled = false ;
return $this ;
}
/**
* Creates a Process instance and returns it .
*
* @ return Process
*
* @ throws LogicException In case no arguments have been provided
*/
public function getProcess ()
{
2018-11-23 12:29:20 +00:00
if ( 0 === \count ( $this -> prefix ) && 0 === \count ( $this -> arguments )) {
2015-08-18 00:00:26 +00:00
throw new LogicException ( 'You must add() command arguments before calling getProcess().' );
}
$arguments = array_merge ( $this -> prefix , $this -> arguments );
2018-11-23 12:29:20 +00:00
$process = new Process ( $arguments , $this -> cwd , $this -> env , $this -> input , $this -> timeout , $this -> options );
// to preserve the BC with symfony <3.3, we convert the array structure
// to a string structure to avoid the prefixing with the exec command
$process -> setCommandLine ( $process -> getCommandLine ());
2015-08-18 00:00:26 +00:00
if ( $this -> inheritEnv ) {
2018-11-23 12:29:20 +00:00
$process -> inheritEnvironmentVariables ();
2015-08-18 00:00:26 +00:00
}
if ( $this -> outputDisabled ) {
$process -> disableOutput ();
}
return $process ;
}
}