Dashboard sipadu mbip
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Writer.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * This file is part of Collision.
  4. *
  5. * (c) Nuno Maduro <enunomaduro@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace NunoMaduro\Collision;
  11. use Whoops\Exception\Frame;
  12. use Whoops\Exception\Inspector;
  13. use Symfony\Component\Console\Output\ConsoleOutput;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use NunoMaduro\Collision\Contracts\Writer as WriterContract;
  16. use NunoMaduro\Collision\Contracts\Highlighter as HighlighterContract;
  17. use NunoMaduro\Collision\Contracts\ArgumentFormatter as ArgumentFormatterContract;
  18. /**
  19. * This is an Collision Writer implementation.
  20. *
  21. * @author Nuno Maduro <enunomaduro@gmail.com>
  22. */
  23. class Writer implements WriterContract
  24. {
  25. /**
  26. * The number of frames if no verbosity is specified.
  27. */
  28. const VERBOSITY_NORMAL_FRAMES = 1;
  29. /**
  30. * Holds an instance of the Output.
  31. *
  32. * @var \Symfony\Component\Console\Output\OutputInterface
  33. */
  34. protected $output;
  35. /**
  36. * Holds an instance of the Argument Formatter.
  37. *
  38. * @var \NunoMaduro\Collision\Contracts\ArgumentFormatter
  39. */
  40. protected $argumentFormatter;
  41. /**
  42. * Holds an instance of the Highlighter.
  43. *
  44. * @var \NunoMaduro\Collision\Contracts\Highlighter
  45. */
  46. protected $highlighter;
  47. /**
  48. * Ignores traces where the file string matches one
  49. * of the provided regex expressions.
  50. *
  51. * @var string[]
  52. */
  53. protected $ignore = [];
  54. /**
  55. * Declares whether or not the trace should appear.
  56. *
  57. * @var bool
  58. */
  59. protected $showTrace = true;
  60. /**
  61. * Declares whether or not the editor should appear.
  62. *
  63. * @var bool
  64. */
  65. protected $showEditor = true;
  66. /**
  67. * Creates an instance of the writer.
  68. *
  69. * @param \Symfony\Component\Console\Output\OutputInterface|null $output
  70. * @param \NunoMaduro\Collision\Contracts\ArgumentFormatter|null $argumentFormatter
  71. * @param \NunoMaduro\Collision\Contracts\Highlighter|null $highlighter
  72. */
  73. public function __construct(
  74. OutputInterface $output = null,
  75. ArgumentFormatterContract $argumentFormatter = null,
  76. HighlighterContract $highlighter = null
  77. ) {
  78. $this->output = $output ?: new ConsoleOutput;
  79. $this->argumentFormatter = $argumentFormatter ?: new ArgumentFormatter;
  80. $this->highlighter = $highlighter ?: new Highlighter;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function write(Inspector $inspector): void
  86. {
  87. $this->renderTitle($inspector);
  88. $frames = $this->getFrames($inspector);
  89. $editorFrame = array_shift($frames);
  90. if ($this->showEditor && $editorFrame !== null) {
  91. $this->renderEditor($editorFrame);
  92. }
  93. if ($this->showTrace && ! empty($frames)) {
  94. $this->renderTrace($frames);
  95. } else {
  96. $this->output->writeln('');
  97. }
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function ignoreFilesIn(array $ignore): WriterContract
  103. {
  104. $this->ignore = $ignore;
  105. return $this;
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function showTrace(bool $show): WriterContract
  111. {
  112. $this->showTrace = $show;
  113. return $this;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function showEditor(bool $show): WriterContract
  119. {
  120. $this->showEditor = $show;
  121. return $this;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function setOutput(OutputInterface $output): WriterContract
  127. {
  128. $this->output = $output;
  129. return $this;
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function getOutput(): OutputInterface
  135. {
  136. return $this->output;
  137. }
  138. /**
  139. * Returns pertinent frames.
  140. *
  141. * @param \Whoops\Exception\Inspector $inspector
  142. *
  143. * @return array
  144. */
  145. protected function getFrames(Inspector $inspector): array
  146. {
  147. return $inspector->getFrames()
  148. ->filter(
  149. function ($frame) {
  150. foreach ($this->ignore as $ignore) {
  151. if (preg_match($ignore, $frame->getFile())) {
  152. return false;
  153. }
  154. }
  155. return true;
  156. }
  157. )
  158. ->getArray();
  159. }
  160. /**
  161. * Renders the title of the exception.
  162. *
  163. * @param \Whoops\Exception\Inspector $inspector
  164. *
  165. * @return \NunoMaduro\Collision\Contracts\Writer
  166. */
  167. protected function renderTitle(Inspector $inspector): WriterContract
  168. {
  169. $exception = $inspector->getException();
  170. $message = $exception->getMessage();
  171. $class = $inspector->getExceptionName();
  172. $this->render("<bg=red;options=bold> $class </> : <comment>$message</>");
  173. return $this;
  174. }
  175. /**
  176. * Renders the editor containing the code that was the
  177. * origin of the exception.
  178. *
  179. * @param \Whoops\Exception\Frame $frame
  180. *
  181. * @return \NunoMaduro\Collision\Contracts\Writer
  182. */
  183. protected function renderEditor(Frame $frame): WriterContract
  184. {
  185. $this->render('at <fg=green>'.$frame->getFile().'</>'.':<fg=green>'.$frame->getLine().'</>');
  186. $content = $this->highlighter->highlight((string) $frame->getFileContents(), (int) $frame->getLine());
  187. $this->output->writeln($content);
  188. return $this;
  189. }
  190. /**
  191. * Renders the trace of the exception.
  192. *
  193. * @param array $frames
  194. *
  195. * @return \NunoMaduro\Collision\Contracts\Writer
  196. */
  197. protected function renderTrace(array $frames): WriterContract
  198. {
  199. $this->render('<comment>Exception trace:</comment>');
  200. foreach ($frames as $i => $frame) {
  201. if ($i > static::VERBOSITY_NORMAL_FRAMES && $this->output->getVerbosity(
  202. ) < OutputInterface::VERBOSITY_VERBOSE) {
  203. $this->render('<info>Please use the argument <fg=red>-v</> to see more details.</info>');
  204. break;
  205. }
  206. $file = $frame->getFile();
  207. $line = $frame->getLine();
  208. $class = empty($frame->getClass()) ? '' : $frame->getClass().'::';
  209. $function = $frame->getFunction();
  210. $args = $this->argumentFormatter->format($frame->getArgs());
  211. $pos = str_pad((int) $i + 1, 4, ' ');
  212. $this->render("<comment><fg=cyan>$pos</>$class$function($args)</comment>");
  213. $this->render(" <fg=green>$file</>:<fg=green>$line</>", false);
  214. }
  215. return $this;
  216. }
  217. /**
  218. * Renders an message into the console.
  219. *
  220. * @param string $message
  221. * @param bool $break
  222. *
  223. * @return $this
  224. */
  225. protected function render(string $message, bool $break = true): WriterContract
  226. {
  227. if ($break) {
  228. $this->output->writeln('');
  229. }
  230. $this->output->writeln(" $message");
  231. return $this;
  232. }
  233. }