public function testKillSignals() { \Amp\run(function () { $process = new Process(self::CMD_PROCESS); $promise = $process->exec(); $process->kill(); $return = (yield $promise); $this->assertObjectHasAttribute('signal', $return); $this->assertObjectHasAttribute('exit', $return); $this->assertInternalType('int', $return->signal); $this->assertInternalType('int', $return->exit); $this->assertEquals(15, $return->signal); $this->assertEquals(-1, $return->exit); }); }
public function testWatch() { \Amp\run(function () { $process = new Process(self::CMD_PROCESS); $this->assertNull($process->pid()); $promise = $process->exec(); $msg = ""; $promise->watch(function ($update) use(&$msg) { list($type, $partMsg) = $update; $this->assertSame("out", $type); $msg .= $partMsg; }); (yield $promise); $this->assertSame("foo\n", $msg); }); }
<?php use Amp\Process; include __DIR__ . "/../vendor/autoload.php"; \Amp\run(function () { $proc = new Process('read ; echo "$REPLY"'); $promise = $proc->exec(Process::BUFFER_ALL); /* send to stdin */ $proc->write("abc\n"); /* wait for process end */ $result = (yield $promise); var_dump($result->stdout); // "abc" });
/** * @param array $certificate certificate configuration * @param string $server server to use for issuance * @param string $storage storage directory * @return \Generator * @throws AcmeException if something does wrong */ private function checkAndIssue(array $certificate, $server, $storage) { $domainPathMap = $this->toDomainPathMap($certificate["paths"]); $domains = array_keys($domainPathMap); $commonName = reset($domains); $args = [PHP_BINARY, $GLOBALS["argv"][0], "check", "--server", $server, "--storage", $storage, "--name", $commonName, "--names", implode(",", $domains)]; $command = implode(" ", array_map("escapeshellarg", $args)); $process = new Process($command); $result = (yield $process->exec(Process::BUFFER_ALL)); if ($result->exit === 0) { // No need for renewal (yield new CoroutineResult(self::STATUS_NO_CHANGE)); return; } if ($result->exit === 1) { // Renew certificate $args = [PHP_BINARY, $GLOBALS["argv"][0], "issue", "--server", $server, "--storage", $storage, "--domains", implode(",", $domains), "--path", implode(PATH_SEPARATOR, array_values($domainPathMap))]; if (isset($certificate["user"])) { $args[] = "--user"; $args[] = $certificate["user"]; } if (isset($certificate["bits"])) { $args[] = "--bits"; $args[] = $certificate["bits"]; } $command = implode(" ", array_map("escapeshellarg", $args)); $process = new Process($command); $result = (yield $process->exec(Process::BUFFER_ALL)); if ($result->exit !== 0) { throw new AcmeException("Unexpected exit code ({$result->exit}) for '{$command}'." . PHP_EOL . $result->stdout . PHP_EOL . PHP_EOL . $result->stderr); } (yield new CoroutineResult(self::STATUS_RENEWED)); return; } throw new AcmeException("Unexpected exit code ({$result->exit}) for '{$command}'." . PHP_EOL . $result->stdout . PHP_EOL . PHP_EOL . $result->stderr); }
<?php use Amp\Process; include __DIR__ . "/../vendor/autoload.php"; \Amp\run(function () { $proc = new Process("echo 1"); $result = (yield $proc->exec(Process::BUFFER_ALL)); var_dump($result->stdout); // "1" });
<?php use Amp\Process; include __DIR__ . "/../vendor/autoload.php"; \Amp\run(function () { $proc = new Process("echo 1; sleep 1; echo 2; sleep 1; echo 3"); $promise = $proc->exec(); $promise->watch(function ($data) { // $data[0] is either "out" or "err", $data[1] the actual data list($type, $msg) = $data; // "1" ... 2 seconds ... "2" ... 2 seconds ... "3" print "{$type}: {$msg}"; }); $result = (yield $promise); // we aren't buffering by default (Process::BUFFER_NONE is default) ... so only exit code present and eventually the killing signal var_dump($result); });