|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bref\Cli\Tinker; |
| 4 | + |
| 5 | +use Bref\Cli\BrefCloudClient; |
| 6 | +use Bref\Cli\Cli\IO; |
| 7 | +use Bref\Cli\Cli\Styles; |
| 8 | +use Psy\Exception\BreakException; |
| 9 | +use Psy\Exception\ThrowUpException; |
| 10 | +use Psy\ExecutionClosure; |
| 11 | +use Psy\ExecutionLoop\AbstractListener; |
| 12 | +use Psy\Shell; |
| 13 | +use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; |
| 14 | +use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; |
| 15 | +use function Amp\delay; |
| 16 | + |
| 17 | +class BrefTinkerLoopListener extends AbstractListener |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var array{appName: string, environmentName: string, team: string} |
| 21 | + */ |
| 22 | + protected array $brefConfig; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var array{ |
| 26 | + * id: int, |
| 27 | + * name: string, |
| 28 | + * region: string|null, |
| 29 | + * url: string|null, |
| 30 | + * outputs: array<string, string>, |
| 31 | + * app: array{id: int, name: string}, |
| 32 | + * } |
| 33 | + */ |
| 34 | + protected array $environment; |
| 35 | + |
| 36 | + protected BrefCloudClient $brefCloudClient; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param array<string, string> $brefConfig |
| 40 | + * @throws ExceptionInterface |
| 41 | + * @throws HttpExceptionInterface |
| 42 | + */ |
| 43 | + public function __construct(array $brefConfig) |
| 44 | + { |
| 45 | + $this->brefConfig = $brefConfig; |
| 46 | + [ |
| 47 | + 'appName' => $appName, |
| 48 | + 'environmentName' => $environmentName, |
| 49 | + 'team' => $team, |
| 50 | + ] = $brefConfig; |
| 51 | + $this->brefCloudClient = new BrefCloudClient; |
| 52 | + $this->environment = $this->brefCloudClient->findEnvironment($team, $appName, $environmentName); |
| 53 | + } |
| 54 | + |
| 55 | + public static function isSupported(): bool |
| 56 | + { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param BrefTinkerShell $shell |
| 62 | + * @throws BreakException |
| 63 | + * @throws ThrowUpException |
| 64 | + */ |
| 65 | + public function onExecute(Shell $shell, string $code) |
| 66 | + { |
| 67 | + if ($code == '\Psy\Exception\BreakException::exitShell();') { |
| 68 | + return $code; |
| 69 | + } |
| 70 | + |
| 71 | + $vars = $shell->getScopeVariables(false); |
| 72 | + $context = $vars['_context'] ?? base64_encode(serialize(["_" => null])); |
| 73 | + // Evaluate the current code buffer |
| 74 | + try { |
| 75 | + [$resultCode, $resultOutput] = $this->evaluateCode($code, $context); |
| 76 | + if ($resultCode !== 0) { |
| 77 | + $shell->rawOutput->writeln($resultOutput); |
| 78 | + throw new BreakException("The remote tinker shell returned an error (code $resultCode)."); |
| 79 | + } |
| 80 | + |
| 81 | + $extractedOutput = $shell->extractContextData($resultOutput); |
| 82 | + if (is_null($extractedOutput)) { |
| 83 | + $shell->rawOutput->writeln(' <info> INFO </info> Please upgrade <string>laravel-bridge</string> package to latest version.'); |
| 84 | + throw new BreakException("The remote tinker shell returned an invalid payload"); |
| 85 | + } |
| 86 | + |
| 87 | + if ([$output, $context, $return] = $extractedOutput) { |
| 88 | + if (!empty($output)) { |
| 89 | + $shell->rawOutput->writeln($output); |
| 90 | + } |
| 91 | + if (!empty($return)) { |
| 92 | + $shell->rawOutput->writeln($return); |
| 93 | + } |
| 94 | + if (!empty($context)) { |
| 95 | + // Extract _context into shell's scope variables for next code execution |
| 96 | + // Return NoValue as output and return value were printed out |
| 97 | + return "extract(['_context' => '{$context}']); return new \Psy\CodeCleaner\NoReturnValue();"; |
| 98 | + } else { |
| 99 | + // Return NoValue as output and return value were printed out |
| 100 | + return "return new \Psy\CodeCleaner\NoReturnValue();"; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return ExecutionClosure::NOOP_INPUT; |
| 105 | + } catch (\Throwable $_e) { |
| 106 | + throw new BreakException($_e->getMessage()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @return array{0: int, 1: string} [exitCode, output] |
| 112 | + * @throws ExceptionInterface |
| 113 | + * @throws HttpExceptionInterface |
| 114 | + */ |
| 115 | + protected function evaluateCode(string $code, string $context): array |
| 116 | + { |
| 117 | + $command = implode(" ", [ |
| 118 | + 'bref:tinker', |
| 119 | + '--execute=\"'.base64_encode($code).'\"', |
| 120 | + '--context=\"'.$context.'\"', |
| 121 | + ]); |
| 122 | + $id = $this->brefCloudClient->startCommand($this->environment['id'], $command); |
| 123 | + |
| 124 | + // Timeout after 2 minutes and 10 seconds |
| 125 | + $timeout = 130; |
| 126 | + $startTime = time(); |
| 127 | + |
| 128 | + while (true) { |
| 129 | + $invocation = $this->brefCloudClient->getCommand($id); |
| 130 | + |
| 131 | + if ($invocation['status'] === 'success') { |
| 132 | + return [0, $invocation['output']]; |
| 133 | + } |
| 134 | + |
| 135 | + if ($invocation['status'] === 'failed') { |
| 136 | + return [1, $invocation['output']]; |
| 137 | + } |
| 138 | + |
| 139 | + if ((time() - $startTime) > $timeout) { |
| 140 | + IO::writeln(Styles::red('Timed out')); |
| 141 | + IO::writeln(Styles::gray('The execution timed out after 2 minutes, the command might still be running')); |
| 142 | + return [1, 'Timed out']; |
| 143 | + } |
| 144 | + |
| 145 | + delay(0.5); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments