1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Process;
13
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);
15
16use Symfony\Component\Process\Exception\InvalidArgumentException;
17use Symfony\Component\Process\Exception\LogicException;
18
19/**
20 * @author Kris Wallsmith <kris@symfony.com>
21 *
22 * @deprecated since version 3.4, to be removed in 4.0. Use the Process class instead.
23 */
24class ProcessBuilder
25{
26    private $arguments;
27    private $cwd;
28    private $env = [];
29    private $input;
30    private $timeout = 60;
31    private $options;
32    private $inheritEnv = true;
33    private $prefix = [];
34    private $outputDisabled = false;
35
36    /**
37     * @param string[] $arguments An array of arguments
38     */
39    public function __construct(array $arguments = [])
40    {
41        $this->arguments = $arguments;
42    }
43
44    /**
45     * Creates a process builder instance.
46     *
47     * @param string[] $arguments An array of arguments
48     *
49     * @return static
50     */
51    public static function create(array $arguments = [])
52    {
53        return new static($arguments);
54    }
55
56    /**
57     * Adds an unescaped argument to the command string.
58     *
59     * @param string $argument A command argument
60     *
61     * @return $this
62     */
63    public function add($argument)
64    {
65        $this->arguments[] = $argument;
66
67        return $this;
68    }
69
70    /**
71     * Adds a prefix to the command string.
72     *
73     * The prefix is preserved when resetting arguments.
74     *
75     * @param string|array $prefix A command prefix or an array of command prefixes
76     *
77     * @return $this
78     */
79    public function setPrefix($prefix)
80    {
81        $this->prefix = \is_array($prefix) ? $prefix : [$prefix];
82
83        return $this;
84    }
85
86    /**
87     * Sets the arguments of the process.
88     *
89     * Arguments must not be escaped.
90     * Previous arguments are removed.
91     *
92     * @param string[] $arguments
93     *
94     * @return $this
95     */
96    public function setArguments(array $arguments)
97    {
98        $this->arguments = $arguments;
99
100        return $this;
101    }
102
103    /**
104     * Sets the working directory.
105     *
106     * @param string|null $cwd The working directory
107     *
108     * @return $this
109     */
110    public function setWorkingDirectory($cwd)
111    {
112        $this->cwd = $cwd;
113
114        return $this;
115    }
116
117    /**
118     * Sets whether environment variables will be inherited or not.
119     *
120     * @param bool $inheritEnv
121     *
122     * @return $this
123     */
124    public function inheritEnvironmentVariables($inheritEnv = true)
125    {
126        $this->inheritEnv = $inheritEnv;
127
128        return $this;
129    }
130
131    /**
132     * Sets an environment variable.
133     *
134     * Setting a variable overrides its previous value. Use `null` to unset a
135     * defined environment variable.
136     *
137     * @param string      $name  The variable name
138     * @param string|null $value The variable value
139     *
140     * @return $this
141     */
142    public function setEnv($name, $value)
143    {
144        $this->env[$name] = $value;
145
146        return $this;
147    }
148
149    /**
150     * Adds a set of environment variables.
151     *
152     * Already existing environment variables with the same name will be
153     * overridden by the new values passed to this method. Pass `null` to unset
154     * a variable.
155     *
156     * @param array $variables The variables
157     *
158     * @return $this
159     */
160    public function addEnvironmentVariables(array $variables)
161    {
162        $this->env = array_replace($this->env, $variables);
163
164        return $this;
165    }
166
167    /**
168     * Sets the input of the process.
169     *
170     * @param resource|string|int|float|bool|\Traversable|null $input The input content
171     *
172     * @return $this
173     *
174     * @throws InvalidArgumentException In case the argument is invalid
175     */
176    public function setInput($input)
177    {
178        $this->input = ProcessUtils::validateInput(__METHOD__, $input);
179
180        return $this;
181    }
182
183    /**
184     * Sets the process timeout.
185     *
186     * To disable the timeout, set this value to null.
187     *
188     * @param float|null $timeout
189     *
190     * @return $this
191     *
192     * @throws InvalidArgumentException
193     */
194    public function setTimeout($timeout)
195    {
196        if (null === $timeout) {
197            $this->timeout = null;
198
199            return $this;
200        }
201
202        $timeout = (float) $timeout;
203
204        if ($timeout < 0) {
205            throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
206        }
207
208        $this->timeout = $timeout;
209
210        return $this;
211    }
212
213    /**
214     * Adds a proc_open option.
215     *
216     * @param string $name  The option name
217     * @param string $value The option value
218     *
219     * @return $this
220     */
221    public function setOption($name, $value)
222    {
223        $this->options[$name] = $value;
224
225        return $this;
226    }
227
228    /**
229     * Disables fetching output and error output from the underlying process.
230     *
231     * @return $this
232     */
233    public function disableOutput()
234    {
235        $this->outputDisabled = true;
236
237        return $this;
238    }
239
240    /**
241     * Enables fetching output and error output from the underlying process.
242     *
243     * @return $this
244     */
245    public function enableOutput()
246    {
247        $this->outputDisabled = false;
248
249        return $this;
250    }
251
252    /**
253     * Creates a Process instance and returns it.
254     *
255     * @return Process
256     *
257     * @throws LogicException In case no arguments have been provided
258     */
259    public function getProcess()
260    {
261        if (0 === \count($this->prefix) && 0 === \count($this->arguments)) {
262            throw new LogicException('You must add() command arguments before calling getProcess().');
263        }
264
265        $arguments = array_merge($this->prefix, $this->arguments);
266        $process = new Process($arguments, $this->cwd, $this->env, $this->input, $this->timeout, $this->options);
267        // to preserve the BC with symfony <3.3, we convert the array structure
268        // to a string structure to avoid the prefixing with the exec command
269        $process->setCommandLine($process->getCommandLine());
270
271        if ($this->inheritEnv) {
272            $process->inheritEnvironmentVariables();
273        }
274        if ($this->outputDisabled) {
275            $process->disableOutput();
276        }
277
278        return $process;
279    }
280}
281