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