|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bref\Cli\Tinker; |
| 4 | + |
| 5 | +use Bref\Cli\Commands\Command; |
| 6 | +use GuzzleHttp\Exception\ClientException; |
| 7 | +use Psy\Exception\BreakException; |
| 8 | +use Psy\Exception\ThrowUpException; |
| 9 | +use Psy\ExecutionClosure; |
| 10 | +use Psy\ExecutionLoop\AbstractListener; |
| 11 | +use Psy\Shell; |
| 12 | +use Symfony\Component\Console\Input\ArgvInput; |
| 13 | +use Symfony\Component\Console\Input\StringInput; |
| 14 | +use Symfony\Component\Console\Output\BufferedOutput; |
| 15 | + |
| 16 | +class BrefTinkerLoopListener extends AbstractListener |
| 17 | +{ |
| 18 | + protected string $commandInput; |
| 19 | + |
| 20 | + public function __construct(string $commandInput) |
| 21 | + { |
| 22 | + $this->commandInput = $commandInput; |
| 23 | + } |
| 24 | + |
| 25 | + public static function isSupported(): bool |
| 26 | + { |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param BrefTinkerShell $shell |
| 32 | + * @throws BreakException |
| 33 | + * @throws ThrowUpException |
| 34 | + */ |
| 35 | + public function onExecute(Shell $shell, string $code) |
| 36 | + { |
| 37 | + if ($code == '\Psy\Exception\BreakException::exitShell();') { |
| 38 | + return $code; |
| 39 | + } |
| 40 | + |
| 41 | + $vars = $shell->getScopeVariables(false); |
| 42 | + $context = $vars['_context'] ?? base64_encode(serialize(["_" => null])); |
| 43 | + // Evaluate the current code buffer |
| 44 | + try { |
| 45 | + $command = new Command(); |
| 46 | + $args = join(" ", [ |
| 47 | + 'bref:tinker', |
| 48 | + '--execute=\"'.base64_encode($code).'\"', |
| 49 | + '--context=\"'.$context.'\"', |
| 50 | + ]); |
| 51 | + $output = new BufferedOutput(); |
| 52 | + $input = new ArgvInput(array_merge((new StringInput($this->commandInput))->getRawTokens(), [$args])); |
| 53 | + |
| 54 | + $resultCode = $command->run($input, $output); |
| 55 | + $resultOutput = $output->fetch(); |
| 56 | + if ($resultCode !== 0) { |
| 57 | + throw new BreakException("The remote tinker shell returned an error (code $resultCode)."); |
| 58 | + } |
| 59 | + |
| 60 | + $extractedOutput = $shell->extractContextData($resultOutput); |
| 61 | + if (is_null($extractedOutput)) { |
| 62 | + $shell->rawOutput->writeln(' <info> INFO </info> Please upgrade <string>laravel-bridge</string> package to latest version.'); |
| 63 | + throw new BreakException("The remote tinker shell returned an invalid payload"); |
| 64 | + } |
| 65 | + |
| 66 | + if ([$output, $context, $return] = $extractedOutput) { |
| 67 | + if (!empty($output)) { |
| 68 | + $shell->rawOutput->writeln($output); |
| 69 | + } |
| 70 | + if (!empty($return)) { |
| 71 | + $shell->rawOutput->writeln($return); |
| 72 | + } |
| 73 | + if (!empty($context)) { |
| 74 | + // Extract _context into shell's scope variables for next code execution |
| 75 | + // Return NoValue as output and return value were printed out |
| 76 | + return "extract(['_context' => '{$context}']); return new \Psy\CodeCleaner\NoReturnValue();"; |
| 77 | + } else { |
| 78 | + // Return NoValue as output and return value were printed out |
| 79 | + return "return new \Psy\CodeCleaner\NoReturnValue();"; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return ExecutionClosure::NOOP_INPUT; |
| 84 | + } catch (ClientException $_e) { |
| 85 | + throw new BreakException($_e->getMessage()); |
| 86 | + } catch (BreakException $breakException) { |
| 87 | + throw $breakException; |
| 88 | + } catch (\Throwable $throwable) { |
| 89 | + throw new ThrowUpException($throwable); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments