|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Spiral\RoadRunnerLaravel\Dumper; |
| 6 | + |
| 7 | +use Illuminate\Support\Env; |
| 8 | +use Symfony\Component\VarDumper\VarDumper; |
| 9 | +use Symfony\Component\VarDumper\Cloner\VarCloner; |
| 10 | + |
| 11 | +class Dumper |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var Stack\StackInterface |
| 15 | + */ |
| 16 | + protected Stack\StackInterface $stack; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var Stoppers\StopperInterface |
| 20 | + */ |
| 21 | + protected Stoppers\StopperInterface $stopper; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var VarCloner |
| 25 | + */ |
| 26 | + protected VarCloner $cloner; |
| 27 | + |
| 28 | + /** |
| 29 | + * RR constructor. |
| 30 | + * |
| 31 | + * @param Stack\StackInterface $stack |
| 32 | + * @param Stoppers\StopperInterface $stopper |
| 33 | + */ |
| 34 | + public function __construct(Stack\StackInterface $stack, Stoppers\StopperInterface $stopper) |
| 35 | + { |
| 36 | + $this->stopper = $stopper; |
| 37 | + $this->stack = $stack; |
| 38 | + $this->cloner = new VarCloner(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Dump passed values. |
| 43 | + * |
| 44 | + * @param mixed $var |
| 45 | + * @param mixed ...$vars |
| 46 | + * |
| 47 | + * @return void |
| 48 | + * |
| 49 | + * @throws \ErrorException On variables cloner errors |
| 50 | + */ |
| 51 | + public function dump($var, ...$vars): void |
| 52 | + { |
| 53 | + $ran_using_cli = $this->ranUsingCLI(); |
| 54 | + |
| 55 | + foreach ([$var, ...$vars] as $item) { |
| 56 | + if ($ran_using_cli) { |
| 57 | + // @codeCoverageIgnoreStart |
| 58 | + VarDumper::dump($item); |
| 59 | + // @codeCoverageIgnoreEnd |
| 60 | + } else { |
| 61 | + $this->stack->push($this->cloner->cloneVar($item)); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Dump passed values and stop the execution. |
| 68 | + * |
| 69 | + * @param mixed ...$vars |
| 70 | + * |
| 71 | + * @return void |
| 72 | + * |
| 73 | + * @throws Exceptions\DumperException On execution in non-CLI context |
| 74 | + * @throws \ErrorException On variables cloner errors |
| 75 | + */ |
| 76 | + public function dd(...$vars) |
| 77 | + { |
| 78 | + if ($this->ranUsingCLI()) { |
| 79 | + // @codeCoverageIgnoreStart |
| 80 | + try { |
| 81 | + foreach ($vars as $item) { |
| 82 | + VarDumper::dump($item); |
| 83 | + } |
| 84 | + } finally { |
| 85 | + $this->stopper->stop(); |
| 86 | + } |
| 87 | + // @codeCoverageIgnoreEnd |
| 88 | + } else { |
| 89 | + $stack = new Stack\FixedArrayStack(); |
| 90 | + |
| 91 | + foreach ($vars as $item) { |
| 92 | + $stack->push($this->cloner->cloneVar($item)); |
| 93 | + } |
| 94 | + |
| 95 | + throw Exceptions\DumperException::withStack($stack); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return bool |
| 101 | + */ |
| 102 | + protected function ranUsingCLI(): bool |
| 103 | + { |
| 104 | + /** @link https://roadrunner.dev/docs/php-environment */ |
| 105 | + if (Env::get('RR_MODE') !== null && Env::get('RR_RPC') !== null && Env::get('RR_RELAY') !== null) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + /** @see VarDumper::register() */ |
| 110 | + return \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true); |
| 111 | + } |
| 112 | +} |
0 commit comments