This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Process/ProcessBuilder.php

139 lines
2.9 KiB
PHP
Raw Normal View History

<?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;
2012-08-30 20:25:16 +01:00
use Symfony\Component\Process\Exception\InvalidArgumentException;
use Symfony\Component\Process\Exception\LogicException;
/**
* Process builder.
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class ProcessBuilder
{
private $arguments;
private $cwd;
2012-02-22 13:38:43 +00:00
private $env;
private $stdin;
private $timeout;
private $options;
private $inheritEnv;
public function __construct(array $arguments = array())
{
$this->arguments = $arguments;
$this->timeout = 60;
$this->options = array();
2012-02-22 13:38:43 +00:00
$this->env = array();
$this->inheritEnv = true;
}
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
*/
public function add($argument)
{
$this->arguments[] = $argument;
return $this;
}
public function setWorkingDirectory($cwd)
{
$this->cwd = $cwd;
return $this;
}
public function inheritEnvironmentVariables($inheritEnv = true)
{
$this->inheritEnv = $inheritEnv;
return $this;
}
public function setEnv($name, $value)
{
$this->env[$name] = $value;
return $this;
}
public function setInput($stdin)
{
$this->stdin = $stdin;
return $this;
}
/**
* Sets the process timeout.
*
* To disable the timeout, set this value to null.
*
* @param integer|null
*/
public function setTimeout($timeout)
{
if (null === $timeout) {
$this->timeout = null;
return $this;
}
$timeout = (integer) $timeout;
if ($timeout < 0) {
2012-08-30 20:25:16 +01:00
throw new InvalidArgumentException('The timeout value must be a valid positive integer.');
}
$this->timeout = $timeout;
return $this;
}
public function setOption($name, $value)
{
$this->options[$name] = $value;
return $this;
}
public function getProcess()
{
if (!count($this->arguments)) {
2012-08-30 20:25:16 +01:00
throw new LogicException('You must add() command arguments before calling getProcess().');
}
$options = $this->options;
2012-03-18 12:07:34 +00:00
$script = implode(' ', array_map('escapeshellarg', $this->arguments));
2012-02-22 13:10:14 +00:00
if ($this->inheritEnv) {
$env = $this->env ? $this->env + $_ENV : null;
2012-02-22 13:10:14 +00:00
} else {
$env = $this->env;
}
return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
}
}