annotate vendor/symfony/process/ProcessBuilder.php @ 19:fa3358dc1485 tip

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