receive(); switch ($signal) { case self::TERMINATE: Loop::stop(); return; case self::KILL: exit(1); } } catch (ChannelException $exception) { // Channel closed unexpectedly, ignore. } }); } public static function execute(Channel $channel, string $path, string $arguments): int { try { if (!\is_file($path)) { throw new \Error(\sprintf("No script found at '%s' (be sure to provide the full path to the script)", $path)); } try { // Protect current scope by requiring script within another function. $callable = (function () use ($path): callable { return require $path; })(); } catch (\TypeError $exception) { throw new \Error(\sprintf("Script '%s' did not return a callable function", $path), 0, $exception); } catch (\ParseError $exception) { throw new \Error(\sprintf("Script '%s' contains a parse error", $path), 0, $exception); } $arguments = self::unserializeArguments($arguments); $result = new ExitSuccess(Promise\wait(call($callable, $channel, ...$arguments))); } catch (\Throwable $exception) { $result = new ExitFailure($exception); } try { Promise\wait(call(function () use ($channel, $result) { try { yield $channel->send($result); } catch (SerializationException $exception) { // Serializing the result failed. Send the reason why. yield $channel->send(new ExitFailure($exception)); } })); } catch (\Throwable $exception) { \trigger_error("Could not send result to parent; be sure to shutdown the child before ending the parent", E_USER_ERROR); return 1; } return 0; } }