| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Ui; |
| 6: | |
| 7: | use Atk4\Core\DebugTrait; |
| 8: | use Atk4\Core\TraitUtil; |
| 9: | use Atk4\Ui\Js\JsBlock; |
| 10: | use Atk4\Ui\Js\JsExpressionable; |
| 11: | use Psr\Log\LoggerInterface; |
| 12: | use Psr\Log\LogLevel; |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | class Console extends View implements LoggerInterface |
| 19: | { |
| 20: | public $ui = 'inverted black segment'; |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | public $event = true; |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | protected $sseInProgress = false; |
| 41: | |
| 42: | |
| 43: | public $sse; |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | protected $_outputBypass = false; |
| 52: | |
| 53: | |
| 54: | public $lastExitCode; |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | #[\Override] |
| 80: | public function set($fx = null, $event = null) |
| 81: | { |
| 82: | if (!$fx instanceof \Closure) { |
| 83: | throw new \TypeError('$fx must be of type Closure'); |
| 84: | } |
| 85: | |
| 86: | if ($event !== null) { |
| 87: | $this->event = $event; |
| 88: | } |
| 89: | |
| 90: | if (!$this->sse) { |
| 91: | $this->sse = JsSse::addTo($this); |
| 92: | } |
| 93: | |
| 94: | $this->sse->set(function () use ($fx) { |
| 95: | $this->sseInProgress = true; |
| 96: | $oldLogger = $this->getApp()->logger; |
| 97: | $this->getApp()->logger = $this; |
| 98: | try { |
| 99: | ob_start(function (string $content) { |
| 100: | if ($this->_outputBypass || $content === '' ) { |
| 101: | return $content; |
| 102: | } |
| 103: | |
| 104: | $output = ''; |
| 105: | $this->sse->echoFunction = static function (string $str) use (&$output) { |
| 106: | $output .= $str; |
| 107: | }; |
| 108: | $this->output($content); |
| 109: | $this->sse->echoFunction = false; |
| 110: | |
| 111: | return $output; |
| 112: | }, 1); |
| 113: | |
| 114: | try { |
| 115: | $fx($this); |
| 116: | } catch (\Throwable $e) { |
| 117: | $this->outputHtmlWithoutPre('<div class="ui segment">{0}</div>', [$this->getApp()->renderExceptionHtml($e)]); |
| 118: | } |
| 119: | } finally { |
| 120: | $this->sseInProgress = false; |
| 121: | $this->getApp()->logger = $oldLogger; |
| 122: | } |
| 123: | }); |
| 124: | |
| 125: | if ($this->event) { |
| 126: | $this->js($this->event, $this->jsExecute()); |
| 127: | } |
| 128: | |
| 129: | return $this; |
| 130: | } |
| 131: | |
| 132: | public function jsExecute(): JsBlock |
| 133: | { |
| 134: | return $this->sse->jsExecute(); |
| 135: | } |
| 136: | |
| 137: | private function escapeOutputHtml(string $message): string |
| 138: | { |
| 139: | $res = $this->getApp()->encodeHtml($message); |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | $res = preg_replace('~\r\n?|\n~s', "\n", $res); |
| 169: | $res = preg_replace('~^$|(?<!^)(\n+)$~s', "$1\n", $res); |
| 170: | |
| 171: | return $res; |
| 172: | } |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | public function output(string $message, array $context = []) |
| 180: | { |
| 181: | $this->outputHtml($this->escapeOutputHtml($message), $context); |
| 182: | |
| 183: | return $this; |
| 184: | } |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | public function outputHtml(string $messageHtml, array $context = []) |
| 192: | { |
| 193: | $this->outputHtmlWithoutPre($this->getApp()->getTag('div', ['style' => 'font-family: monospace; white-space: pre;'], [$messageHtml]), $context); |
| 194: | |
| 195: | return $this; |
| 196: | } |
| 197: | |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | protected function outputHtmlWithoutPre(string $messageHtml, array $context = []) |
| 204: | { |
| 205: | $messageHtml = preg_replace_callback('~{([\w]+)}~', static function ($matches) use ($context) { |
| 206: | if (isset($context[$matches[1]])) { |
| 207: | return $context[$matches[1]]; |
| 208: | } |
| 209: | |
| 210: | return $matches[0]; |
| 211: | }, $messageHtml); |
| 212: | |
| 213: | $this->_outputBypass = true; |
| 214: | try { |
| 215: | $this->sse->send($this->js()->append($messageHtml)); |
| 216: | } finally { |
| 217: | $this->_outputBypass = false; |
| 218: | } |
| 219: | |
| 220: | return $this; |
| 221: | } |
| 222: | |
| 223: | #[\Override] |
| 224: | protected function renderView(): void |
| 225: | { |
| 226: | $this->setStyle('overflow-x', 'auto'); |
| 227: | |
| 228: | parent::renderView(); |
| 229: | } |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | public function send(JsExpressionable $js) |
| 237: | { |
| 238: | $this->_outputBypass = true; |
| 239: | try { |
| 240: | $this->sse->send($js); |
| 241: | } finally { |
| 242: | $this->_outputBypass = false; |
| 243: | } |
| 244: | |
| 245: | return $this; |
| 246: | } |
| 247: | |
| 248: | |
| 249: | |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | |
| 260: | |
| 261: | public function exec(string $command, array $args = []): ?bool |
| 262: | { |
| 263: | if (!$this->sseInProgress) { |
| 264: | $this->set(function () use ($command, $args) { |
| 265: | $this->output( |
| 266: | '--[ Executing ' . $command |
| 267: | . ($args ? ' with ' . count($args) . ' arguments' : '') |
| 268: | . ' ]--------------' |
| 269: | ); |
| 270: | |
| 271: | $this->exec($command, $args); |
| 272: | |
| 273: | $this->output('--[ Exit code: ' . $this->lastExitCode . ' ]------------'); |
| 274: | }); |
| 275: | |
| 276: | return null; |
| 277: | } |
| 278: | |
| 279: | [$proc, $pipes] = $this->execRaw($command, $args); |
| 280: | |
| 281: | stream_set_blocking($pipes[1], false); |
| 282: | stream_set_blocking($pipes[2], false); |
| 283: | |
| 284: | while ($pipes) { |
| 285: | $read = $pipes; |
| 286: | $j1 = null; |
| 287: | $j2 = null; |
| 288: | if (stream_select($read, $j1, $j2, 2) === false) { |
| 289: | throw new Exception('Unexpected stream_select() result'); |
| 290: | } |
| 291: | |
| 292: | $status = proc_get_status($proc); |
| 293: | if (!$status['running']) { |
| 294: | proc_close($proc); |
| 295: | |
| 296: | break; |
| 297: | } |
| 298: | |
| 299: | foreach ($read as $f) { |
| 300: | $data = rtrim((string) fgets($f)); |
| 301: | if ($data === '') { |
| 302: | |
| 303: | |
| 304: | continue; |
| 305: | |
| 306: | } |
| 307: | |
| 308: | if ($f === $pipes[2]) { |
| 309: | $this->warning($data); |
| 310: | } else { |
| 311: | $this->output($data); |
| 312: | } |
| 313: | } |
| 314: | } |
| 315: | |
| 316: | $this->lastExitCode = $status['exitcode']; |
| 317: | |
| 318: | return $this->lastExitCode ? false : true; |
| 319: | } |
| 320: | |
| 321: | |
| 322: | |
| 323: | |
| 324: | protected function execRaw(string $command, array $args = []) |
| 325: | { |
| 326: | $args = array_map(static fn ($v) => escapeshellarg($v), $args); |
| 327: | |
| 328: | $spec = [1 => ['pipe', 'w'], 2 => ['pipe', 'w']]; |
| 329: | $pipes = null; |
| 330: | $proc = proc_open($command . ' ' . implode(' ', $args), $spec, $pipes); |
| 331: | if (!is_resource($proc)) { |
| 332: | throw (new Exception('Command failed to execute')) |
| 333: | ->addMoreInfo('command', $command) |
| 334: | ->addMoreInfo('args', $args); |
| 335: | } |
| 336: | |
| 337: | return [$proc, $pipes]; |
| 338: | } |
| 339: | |
| 340: | |
| 341: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: | |
| 349: | |
| 350: | |
| 351: | |
| 352: | |
| 353: | |
| 354: | |
| 355: | |
| 356: | |
| 357: | |
| 358: | |
| 359: | |
| 360: | |
| 361: | |
| 362: | |
| 363: | |
| 364: | |
| 365: | public function runMethod($object, string $method, array $args = []) |
| 366: | { |
| 367: | if (!$this->sseInProgress) { |
| 368: | $this->set(function () use ($object, $method, $args) { |
| 369: | $this->runMethod($object, $method, $args); |
| 370: | }); |
| 371: | |
| 372: | return $this; |
| 373: | } |
| 374: | |
| 375: | if (is_object($object)) { |
| 376: | |
| 377: | if (TraitUtil::hasAppScopeTrait($object) && $object->issetApp()) { |
| 378: | $loggerBak = $object->getApp()->logger; |
| 379: | $object->getApp()->logger = $this; |
| 380: | } |
| 381: | if (TraitUtil::hasTrait($object, DebugTrait::class)) { |
| 382: | $debugBak = $object->debug; |
| 383: | $object->debug = true; |
| 384: | } |
| 385: | |
| 386: | $this->output('--[ Executing ' . get_class($object) . '->' . $method . ' ]--------------'); |
| 387: | |
| 388: | try { |
| 389: | $result = $object->{$method}(...$args); |
| 390: | } finally { |
| 391: | if (TraitUtil::hasAppScopeTrait($object) && $object->issetApp()) { |
| 392: | $object->getApp()->logger = $loggerBak; |
| 393: | } |
| 394: | if (TraitUtil::hasTrait($object, DebugTrait::class)) { |
| 395: | $object->debug = $debugBak; |
| 396: | } |
| 397: | } |
| 398: | } else { |
| 399: | $this->output('--[ Executing ' . $object . '::' . $method . ' ]--------------'); |
| 400: | |
| 401: | $result = $object::{$method}(...$args); |
| 402: | } |
| 403: | $this->output('--[ Result: ' . $this->getApp()->encodeJson($result) . ' ]------------'); |
| 404: | |
| 405: | return $this; |
| 406: | } |
| 407: | |
| 408: | #[\Override] |
| 409: | public function emergency($message, array $context = []): void |
| 410: | { |
| 411: | $this->outputHtml('<font color="pink">' . $this->escapeOutputHtml($message) . '</font>', $context); |
| 412: | } |
| 413: | |
| 414: | #[\Override] |
| 415: | public function alert($message, array $context = []): void |
| 416: | { |
| 417: | $this->outputHtml('<font color="pink">' . $this->escapeOutputHtml($message) . '</font>', $context); |
| 418: | } |
| 419: | |
| 420: | #[\Override] |
| 421: | public function critical($message, array $context = []): void |
| 422: | { |
| 423: | $this->outputHtml('<font color="pink">' . $this->escapeOutputHtml($message) . '</font>', $context); |
| 424: | } |
| 425: | |
| 426: | #[\Override] |
| 427: | public function error($message, array $context = []): void |
| 428: | { |
| 429: | $this->outputHtml('<font color="pink">' . $this->escapeOutputHtml($message) . '</font>', $context); |
| 430: | } |
| 431: | |
| 432: | #[\Override] |
| 433: | public function warning($message, array $context = []): void |
| 434: | { |
| 435: | $this->outputHtml('<font color="pink">' . $this->escapeOutputHtml($message) . '</font>', $context); |
| 436: | } |
| 437: | |
| 438: | #[\Override] |
| 439: | public function notice($message, array $context = []): void |
| 440: | { |
| 441: | $this->outputHtml('<font color="yellow">' . $this->escapeOutputHtml($message) . '</font>', $context); |
| 442: | } |
| 443: | |
| 444: | #[\Override] |
| 445: | public function info($message, array $context = []): void |
| 446: | { |
| 447: | $this->outputHtml('<font color="gray">' . $this->escapeOutputHtml($message) . '</font>', $context); |
| 448: | } |
| 449: | |
| 450: | #[\Override] |
| 451: | public function debug($message, array $context = []): void |
| 452: | { |
| 453: | $this->outputHtml('<font color="cyan">' . $this->escapeOutputHtml($message) . '</font>', $context); |
| 454: | } |
| 455: | |
| 456: | |
| 457: | |
| 458: | |
| 459: | #[\Override] |
| 460: | public function log($level, $message, array $context = []): void |
| 461: | { |
| 462: | $this->{$level}($message, $context); |
| 463: | } |
| 464: | } |
| 465: | |