Terminal.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace Dcat\Admin\Widgets;
  3. use Dcat\Admin\Support\StringOutput;
  4. use Illuminate\Support\Facades\Artisan;
  5. class Terminal extends Widget
  6. {
  7. protected static $style = '.dump info{color: #21b978;}.dump warning{color: #ffcc80}.dump comment{color: rgba(255, 189, 74, .8);}.dump error{color: #ff5b5b}';
  8. protected $content;
  9. public function __construct($content = null)
  10. {
  11. $this->content($content);
  12. $this->class('dump');
  13. }
  14. /**
  15. * @param string $command
  16. * @param array $parameters
  17. *
  18. * @return static
  19. */
  20. public static function call(string $command, array $parameters = [])
  21. {
  22. $output = new StringOutput();
  23. Artisan::call($command, $parameters, $output);
  24. return static::make($output);
  25. }
  26. public function dark()
  27. {
  28. return $this->style('background:#333;color:#fff;');
  29. }
  30. public function transparent()
  31. {
  32. return $this->style('background:transparent!important;color:#fff;');
  33. }
  34. public function content($content)
  35. {
  36. if ($content instanceof StringOutput) {
  37. $content = $content->getContent();
  38. }
  39. $this->content = &$content;
  40. return $this;
  41. }
  42. public function render()
  43. {
  44. $style = static::$style;
  45. static::$style = null;
  46. return <<<EOF
  47. <style>{$style}</style><pre {$this->formatHtmlAttributes()}>{$this->content}</pre>
  48. EOF;
  49. }
  50. }