Completed
Pull Request — 1.0 (#1053)
by
unknown
02:28 queued 23s
created

ProcessBuilder::setEnv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/*
3
 * This file is part of the Symfony package.
4
 *
5
 * (c) Fabien Potencier <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Liip\ImagineBundle\Compat;
11
use Symfony\Component\Process\ProcessUtils;
12
use Symfony\Component\Process\Process;
13
use Symfony\Component\Process\Exception\InvalidArgumentException;
14
use Symfony\Component\Process\Exception\LogicException;
15
//Compat layer for deprecated, removed later, ProcessBuilder
16
/**
17
 * Process builder compat for symfony >= 3.4
18
 *
19
 * @author Kris Wallsmith <[email protected]>
20
 *
21
 * @deprecated since version 3.4, to be removed in 4.0. Use the Process class instead.
22
 */
23
class ProcessBuilder
24
{
25
    private $arguments;
26
    private $cwd;
27
    private $env = array();
28
    private $input;
29
    private $timeout = 60;
30
    private $options;
31
    private $inheritEnv = true;
32
    private $prefix = array();
33
    private $outputDisabled = false;
34
    /**
35
     * Constructor.
36
     *
37
     * @param string[] $arguments An array of arguments
38
     */
39
    public function __construct(array $arguments = array())
40
    {
41
        $this->arguments = $arguments;
42
    }
43
    /**
44
     * Creates a process builder instance.
45
     *
46
     * @param string[] $arguments An array of arguments
47
     *
48
     * @return static
49
     */
50
    public static function create(array $arguments = array())
51
    {
52
        return new static($arguments);
0 ignored issues
show
Deprecated Code introduced by
The class Liip\ImagineBundle\Compat\ProcessBuilder has been deprecated with message: since version 3.4, to be removed in 4.0. Use the Process class instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
53
    }
54
    /**
55
     * Adds an unescaped argument to the command string.
56
     *
57
     * @param string $argument A command argument
58
     *
59
     * @return $this
60
     */
61
    public function add($argument)
62
    {
63
        $this->arguments[] = $argument;
64
        return $this;
65
    }
66
    /**
67
     * Adds a prefix to the command string.
68
     *
69
     * The prefix is preserved when resetting arguments.
70
     *
71
     * @param string|array $prefix A command prefix or an array of command prefixes
72
     *
73
     * @return $this
74
     */
75
    public function setPrefix($prefix)
76
    {
77
        $this->prefix = is_array($prefix) ? $prefix : array($prefix);
78
        return $this;
79
    }
80
    /**
81
     * Sets the arguments of the process.
82
     *
83
     * Arguments must not be escaped.
84
     * Previous arguments are removed.
85
     *
86
     * @param string[] $arguments
87
     *
88
     * @return $this
89
     */
90
    public function setArguments(array $arguments)
91
    {
92
        $this->arguments = $arguments;
93
        return $this;
94
    }
95
    /**
96
     * Sets the working directory.
97
     *
98
     * @param null|string $cwd The working directory
99
     *
100
     * @return $this
101
     */
102
    public function setWorkingDirectory($cwd)
103
    {
104
        $this->cwd = $cwd;
105
        return $this;
106
    }
107
    /**
108
     * Sets whether environment variables will be inherited or not.
109
     *
110
     * @param bool $inheritEnv
111
     *
112
     * @return $this
113
     */
114
    public function inheritEnvironmentVariables($inheritEnv = true)
115
    {
116
        $this->inheritEnv = $inheritEnv;
117
        return $this;
118
    }
119
    /**
120
     * Sets an environment variable.
121
     *
122
     * Setting a variable overrides its previous value. Use `null` to unset a
123
     * defined environment variable.
124
     *
125
     * @param string      $name  The variable name
126
     * @param null|string $value The variable value
127
     *
128
     * @return $this
129
     */
130
    public function setEnv($name, $value)
131
    {
132
        $this->env[$name] = $value;
133
        return $this;
134
    }
135
    /**
136
     * Adds a set of environment variables.
137
     *
138
     * Already existing environment variables with the same name will be
139
     * overridden by the new values passed to this method. Pass `null` to unset
140
     * a variable.
141
     *
142
     * @param array $variables The variables
143
     *
144
     * @return $this
145
     */
146
    public function addEnvironmentVariables(array $variables)
147
    {
148
        $this->env = array_replace($this->env, $variables);
149
        return $this;
150
    }
151
    /**
152
     * Sets the input of the process.
153
     *
154
     * @param resource|scalar|\Traversable|null $input The input content
155
     *
156
     * @return $this
157
     *
158
     * @throws InvalidArgumentException In case the argument is invalid
159
     */
160
    public function setInput($input)
161
    {
162
        $this->input = ProcessUtils::validateInput(__METHOD__, $input);
163
        return $this;
164
    }
165
    /**
166
     * Sets the process timeout.
167
     *
168
     * To disable the timeout, set this value to null.
169
     *
170
     * @param float|null $timeout
171
     *
172
     * @return $this
173
     *
174
     * @throws InvalidArgumentException
175
     */
176
    public function setTimeout($timeout)
177
    {
178
        if (null === $timeout) {
179
            $this->timeout = null;
180
            return $this;
181
        }
182
        $timeout = (float) $timeout;
183
        if ($timeout < 0) {
184
            throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.');
185
        }
186
        $this->timeout = $timeout;
0 ignored issues
show
Documentation Bug introduced by
The property $timeout was declared of type integer, but $timeout is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
187
        return $this;
188
    }
189
    /**
190
     * Adds a proc_open option.
191
     *
192
     * @param string $name  The option name
193
     * @param string $value The option value
194
     *
195
     * @return $this
196
     */
197
    public function setOption($name, $value)
198
    {
199
        $this->options[$name] = $value;
200
        return $this;
201
    }
202
    /**
203
     * Disables fetching output and error output from the underlying process.
204
     *
205
     * @return $this
206
     */
207
    public function disableOutput()
208
    {
209
        $this->outputDisabled = true;
210
        return $this;
211
    }
212
    /**
213
     * Enables fetching output and error output from the underlying process.
214
     *
215
     * @return $this
216
     */
217
    public function enableOutput()
218
    {
219
        $this->outputDisabled = false;
220
        return $this;
221
    }
222
    /**
223
     * Creates a Process instance and returns it.
224
     *
225
     * @return Process
226
     *
227
     * @throws LogicException In case no arguments have been provided
228
     */
229
    public function getProcess()
230
    {
231
        if (0 === count($this->prefix) && 0 === count($this->arguments)) {
232
            throw new LogicException('You must add() command arguments before calling getProcess().');
233
        }
234
        $arguments = array_merge($this->prefix, $this->arguments);
235
        $process = new Process($arguments, $this->cwd, $this->env, $this->input, $this->timeout, $this->options);
236
        if ($this->inheritEnv) {
237
            $process->inheritEnvironmentVariables();
238
        }
239
        if ($this->outputDisabled) {
240
            $process->disableOutput();
241
        }
242
        return $process;
243
    }
244
}
245