Lines Matching refs:this

9  * file that was distributed with this source code.
49 public const ITER_NON_BLOCKING = 1; // By default, iterating over outputs is a blocking call, use this flag to make it non-blocking
50 public const ITER_KEEP_OUTPUT = 2; // By default, outputs are cleared while iterating, use this flag to keep them in memory
51 public const ITER_SKIP_OUT = 4; // Use this flag to skip STDOUT while iterating
52 public const ITER_SKIP_ERR = 8; // Use this flag to skip STDERR while iterating
149 $this->commandline = $command;
150 $this->cwd = $cwd;
156 if (null === $this->cwd && (\defined('ZEND_THREAD_SAFE') || '\\' === \DIRECTORY_SEPARATOR)) {
157 $this->cwd = getcwd();
160 $this->setEnv($env);
163 $this->setInput($input);
164 $this->setTimeout($timeout);
165 $this->useFileHandles = '\\' === \DIRECTORY_SEPARATOR;
166 $this->pty = false;
173 * This allows using e.g. pipes or conditional execution. In this mode, signals are sent to the
215 if ($this->options['create_new_console'] ?? false) {
216 $this->processPipes->close();
218 $this->stop(0);
224 $this->resetProcessData();
252 $this->start($callback, $env);
254 return $this->wait();
263 * @return $this
271 if (0 !== $this->run($callback, $env)) {
272 throw new ProcessFailedException($this);
275 return $this;
299 if ($this->isRunning()) {
303 $this->resetProcessData();
304 $this->starttime = $this->lastOutputTime = microtime(true);
305 $this->callback = $this->buildCallback($callback);
306 $this->hasCallback = null !== $callback;
307 $descriptors = $this->getDescriptors();
309 if ($this->env) {
310 $env += '\\' === \DIRECTORY_SEPARATOR ? array_diff_ukey($this->env, $env, 'strcasecmp') : $this->env;
313 $env += '\\' === \DIRECTORY_SEPARATOR ? array_diff_ukey($this->getDefaultEnv(), $env, 'strcasecmp') : $this->getDefaultEnv();
315 if (\is_array($commandline = $this->commandline)) {
316 $commandline = implode(' ', array_map([$this, 'escapeArgument'], $commandline));
323 $commandline = $this->replacePlaceholders($commandline, $env);
327 $commandline = $this->prepareWindowsCommandLine($commandline, $env);
328 } elseif (!$this->useFileHandles && $this->isSigchildEnabled()) {
348 if (!is_dir($this->cwd)) {
349 throw new RuntimeException(sprintf('The provided cwd "%s" does not exist.', $this->cwd));
352 $this->process = @proc_open($commandline, $descriptors, $this->processPipes->pipes, $this->cwd, $envPairs, $this->options);
354 if (!\is_resource($this->process)) {
357 $this->status = self::STATUS_STARTED;
360 $this->fallbackStatus['pid'] = (int) fgets($this->processPipes->pipes[3]);
363 if ($this->tty) {
367 $this->updateStatus(false);
368 $this->checkTimeout();
390 if ($this->isRunning()) {
394 $process = clone $this;
417 $this->requireProcessIsStarted(__FUNCTION__);
419 $this->updateStatus(false);
422 if (!$this->processPipes->haveReadSupport()) {
423 $this->stop(0);
426 $this->callback = $this->buildCallback($callback);
430 $this->checkTimeout();
431 $running = '\\' === \DIRECTORY_SEPARATOR ? $this->isRunning() : $this->processPipes->areOpen();
432 $this->readPipes($running, '\\' !== \DIRECTORY_SEPARATOR || !$running);
435 while ($this->isRunning()) {
436 $this->checkTimeout();
440 if ($this->processInformation['signaled'] && $this->processInformation['termsig'] !== $this->latestSignal) {
441 throw new ProcessSignaledException($this);
444 return $this->exitcode;
460 $this->requireProcessIsStarted(__FUNCTION__);
461 $this->updateStatus(false);
463 if (!$this->processPipes->haveReadSupport()) {
464 $this->stop(0);
467 $callback = $this->buildCallback($callback);
471 $this->checkTimeout();
472 $running = '\\' === \DIRECTORY_SEPARATOR ? $this->isRunning() : $this->processPipes->areOpen();
473 $output = $this->processPipes->readAndWrite($running, '\\' !== \DIRECTORY_SEPARATOR || !$running);
478 } elseif (!isset($this->fallbackStatus['signaled'])) {
479 $this->fallbackStatus['exitcode'] = (int) $data;
500 return $this->isRunning() ? $this->processInformation['pid'] : null;
508 * @return $this
516 $this->doSignal($signal, true);
518 return $this;
524 * @return $this
531 if ($this->isRunning()) {
534 if (null !== $this->idleTimeout) {
538 $this->outputDisabled = true;
540 return $this;
546 * @return $this
552 if ($this->isRunning()) {
556 $this->outputDisabled = false;
558 return $this;
568 return $this->outputDisabled;
581 $this->readPipesForOutput(__FUNCTION__);
583 if (false === $ret = stream_get_contents($this->stdout, -1, 0)) {
594 * output, this one returns the new output since the last call.
603 $this->readPipesForOutput(__FUNCTION__);
605 $latest = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
606 $this->incrementalOutputOffset = ftell($this->stdout);
628 $this->readPipesForOutput(__FUNCTION__, false);
635 while (null !== $this->callback || ($yieldOut && !feof($this->stdout)) || ($yieldErr && !feof($this->stderr))) {
637 $out = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
641 $this->clearOutput();
643 $this->incrementalOutputOffset = ftell($this->stdout);
651 $err = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
655 $this->clearErrorOutput();
657 $this->incrementalErrorOutputOffset = ftell($this->stderr);
668 $this->checkTimeout();
669 $this->readPipesForOutput(__FUNCTION__, $blocking);
676 * @return $this
680 ftruncate($this->stdout, 0);
681 fseek($this->stdout, 0);
682 $this->incrementalOutputOffset = 0;
684 return $this;
697 $this->readPipesForOutput(__FUNCTION__);
699 if (false === $ret = stream_get_contents($this->stderr, -1, 0)) {
710 * whole error output, this one returns the new error output since the last
720 $this->readPipesForOutput(__FUNCTION__);
722 $latest = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
723 $this->incrementalErrorOutputOffset = ftell($this->stderr);
735 * @return $this
739 ftruncate($this->stderr, 0);
740 fseek($this->stderr, 0);
741 $this->incrementalErrorOutputOffset = 0;
743 return $this;
753 $this->updateStatus(false);
755 return $this->exitcode;
771 if (null === $exitcode = $this->getExitCode()) {
785 return 0 === $this->getExitCode();
799 $this->requireProcessIsTerminated(__FUNCTION__);
801 return $this->processInformation['signaled'];
816 $this->requireProcessIsTerminated(__FUNCTION__);
818 if ($this->isSigchildEnabled() && -1 === $this->processInformation['termsig']) {
822 return $this->processInformation['termsig'];
836 $this->requireProcessIsTerminated(__FUNCTION__);
838 return $this->processInformation['stopped'];
852 $this->requireProcessIsTerminated(__FUNCTION__);
854 return $this->processInformation['stopsig'];
864 if (self::STATUS_STARTED !== $this->status) {
868 $this->updateStatus(false);
870 return $this->processInformation['running'];
880 return self::STATUS_READY != $this->status;
890 $this->updateStatus(false);
892 return self::STATUS_TERMINATED == $this->status;
904 $this->updateStatus(false);
906 return $this->status;
920 if ($this->isRunning()) {
922 $this->doSignal(15, false);
925 } while ($this->isRunning() && microtime(true) < $timeoutMicro);
927 if ($this->isRunning()) {
929 // after this line. In any case, let's silently discard the error, we cannot do anything.
930 $this->doSignal($signal ?: 9, false);
934 if ($this->isRunning()) {
935 if (isset($this->fallbackStatus['pid'])) {
936 unset($this->fallbackStatus['pid']);
938 return $this->stop(0, $signal);
940 $this->close();
943 return $this->exitcode;
953 $this->lastOutputTime = microtime(true);
955 fseek($this->stdout, 0, \SEEK_END);
956 fwrite($this->stdout, $line);
957 fseek($this->stdout, $this->incrementalOutputOffset);
967 $this->lastOutputTime = microtime(true);
969 fseek($this->stderr, 0, \SEEK_END);
970 fwrite($this->stderr, $line);
971 fseek($this->stderr, $this->incrementalErrorOutputOffset);
979 return $this->lastOutputTime;
989 return \is_array($this->commandline) ? implode(' ', array_map([$this, 'escapeArgument'], $this->commandline)) : $this->commandline;
999 return $this->timeout;
1009 return $this->idleTimeout;
1015 * To disable the timeout, set this value to null.
1017 * @return $this
1023 $this->timeout = $this->validateTimeout($timeout);
1025 return $this;
1031 * To disable the timeout, set this value to null.
1033 * @return $this
1040 if (null !== $timeout && $this->outputDisabled) {
1044 $this->idleTimeout = $this->validateTimeout($timeout);
1046 return $this;
1052 * @return $this
1066 $this->tty = $tty;
1068 return $this;
1078 return $this->tty;
1084 * @return $this
1088 $this->pty = $bool;
1090 return $this;
1100 return $this->pty;
1110 if (null === $this->cwd) {
1116 return $this->cwd;
1122 * @return $this
1126 $this->cwd = $cwd;
1128 return $this;
1138 return $this->env;
1146 * @return $this
1150 $this->env = $env;
1152 return $this;
1162 return $this->input;
1172 * @return $this
1178 if ($this->isRunning()) {
1182 $this->input = ProcessUtils::validateInput(__METHOD__, $input);
1184 return $this;
1191 * trigger this method regularly to ensure the process timeout
1197 if (self::STATUS_STARTED !== $this->status) {
1201 if (null !== $this->timeout && $this->timeout < microtime(true) - $this->starttime) {
1202 $this->stop(0);
1204 throw new ProcessTimedOutException($this, ProcessTimedOutException::TYPE_GENERAL);
1207 if (null !== $this->idleTimeout && $this->idleTimeout < microtime(true) - $this->lastOutputTime) {
1208 $this->stop(0);
1210 throw new ProcessTimedOutException($this, ProcessTimedOutException::TYPE_IDLE);
1219 if (!$this->isStarted()) {
1223 return $this->starttime;
1236 if ($this->isRunning()) {
1240 $defaultOptions = $this->options;
1245 $this->options = $defaultOptions;
1248 $this->options[$key] = $value;
1291 if ($this->input instanceof \Iterator) {
1292 $this->input->rewind();
1295 $this->processPipes = new WindowsPipes($this->input, !$this->outputDisabled || $this->hasCallback);
1297 $this->processPipes = new UnixPipes($this->isTty(), $this->isPty(), $this->input, !$this->outputDisabled || $this->hasCallback);
1300 return $this->processPipes->getDescriptors();
1315 if ($this->outputDisabled) {
1325 $this->addOutput($data);
1327 $this->addErrorOutput($data);
1341 if (self::STATUS_STARTED !== $this->status) {
1345 $this->processInformation = proc_get_status($this->process);
1346 $running = $this->processInformation['running'];
1348 $this->readPipes($running && $blocking, '\\' !== \DIRECTORY_SEPARATOR || !$running);
1350 if ($this->fallbackStatus && $this->isSigchildEnabled()) {
1351 $this->processInformation = $this->fallbackStatus + $this->processInformation;
1355 $this->close();
1390 if ($this->outputDisabled) {
1394 $this->requireProcessIsStarted($caller);
1396 $this->updateStatus($blocking);
1425 $result = $this->processPipes->readAndWrite($blocking, $close);
1427 $callback = $this->callback;
1431 } elseif (!isset($this->fallbackStatus['signaled'])) {
1432 $this->fallbackStatus['exitcode'] = (int) $data;
1444 $this->processPipes->close();
1445 if (\is_resource($this->process)) {
1446 proc_close($this->process);
1448 $this->exitcode = $this->processInformation['exitcode'];
1449 $this->status = self::STATUS_TERMINATED;
1451 if (-1 === $this->exitcode) {
1452 if ($this->processInformation['signaled'] && 0 < $this->processInformation['termsig']) {
1454 $this->exitcode = 128 + $this->processInformation['termsig'];
1455 } elseif ($this->isSigchildEnabled()) {
1456 $this->processInformation['signaled'] = true;
1457 $this->processInformation['termsig'] = -1;
1464 $this->callback = null;
1466 return $this->exitcode;
1474 $this->starttime = null;
1475 $this->callback = null;
1476 $this->exitcode = null;
1477 $this->fallbackStatus = [];
1478 $this->processInformation = null;
1479 $this->stdout = fopen('php://temp/maxmemory:'.(1024 * 1024), 'w+');
1480 $this->stderr = fopen('php://temp/maxmemory:'.(1024 * 1024), 'w+');
1481 $this->process = null;
1482 $this->latestSignal = null;
1483 $this->status = self::STATUS_READY;
1484 $this->incrementalOutputOffset = 0;
1485 $this->incrementalErrorOutputOffset = 0;
1500 if (null === $pid = $this->getPid()) {
1510 if ($exitCode && $this->isRunning()) {
1518 if (!$this->isSigchildEnabled()) {
1519 $ok = @proc_terminate($this->process, $signal);
1534 $this->latestSignal = $signal;
1535 $this->fallbackStatus['signaled'] = true;
1536 $this->fallbackStatus['exitcode'] = -1;
1537 $this->fallbackStatus['termsig'] = $this->latestSignal;
1581 foreach ($this->processPipes->getFiles() as $offset => $filename) {
1595 if (!$this->isStarted()) {
1607 if (!$this->isTerminated()) {
1641 return $this->escapeArgument($env[$matches[1]]);