PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Associates/Whoops/vendor/filp/whoops/src/Whoops/Handler/PlainTextHandler.php

https://gitlab.com/fiesta-framework/Documentation
PHP | 375 lines | 199 code | 48 blank | 128 comment | 29 complexity | 6dced13b1b99b4fa5f15809366104f45 MD5 | raw file
  1. <?php
  2. /**
  3. * Whoops - php errors for cool kids
  4. * @author Filipe Dobreira <http://github.com/filp>
  5. * Plaintext handler for command line and logs.
  6. * @author Pierre-Yves Landuré <https://howto.biapy.com/>
  7. */
  8. namespace Whoops\Handler;
  9. use Exception;
  10. use InvalidArgumentException;
  11. use Psr\Log\LoggerInterface;
  12. use Whoops\Exception\Frame;
  13. /**
  14. * Handler outputing plaintext error messages. Can be used
  15. * directly, or will be instantiated automagically by Whoops\Run
  16. * if passed to Run::pushHandler
  17. */
  18. class PlainTextHandler extends Handler
  19. {
  20. const VAR_DUMP_PREFIX = ' | ';
  21. /**
  22. * @var Psr\Log\LoggerInterface
  23. */
  24. protected $logger;
  25. public $msg;
  26. /**
  27. * @var bool
  28. */
  29. private $addTraceToOutput = true;
  30. /**
  31. * @var bool|integer
  32. */
  33. private $addTraceFunctionArgsToOutput = false;
  34. /**
  35. * @var integer
  36. */
  37. private $traceFunctionArgsOutputLimit = 1024;
  38. /**
  39. * @var bool
  40. */
  41. private $onlyForCommandLine = false;
  42. /**
  43. * @var bool
  44. */
  45. private $outputOnlyIfCommandLine = true;
  46. /**
  47. * @var bool
  48. */
  49. private $loggerOnly = false;
  50. /**
  51. * Constructor.
  52. * @throws InvalidArgumentException If argument is not null or a LoggerInterface
  53. * @param Psr\Log\LoggerInterface|null $logger
  54. */
  55. public function __construct($logger = null)
  56. {
  57. $this->setLogger($logger);
  58. }
  59. /**
  60. * Set the output logger interface.
  61. * @throws InvalidArgumentException If argument is not null or a LoggerInterface
  62. * @param Psr\Log\LoggerInterface|null $logger
  63. */
  64. public function setLogger($logger = null)
  65. {
  66. if (! (is_null($logger)
  67. || $logger instanceof LoggerInterface)) {
  68. throw new InvalidArgumentException(
  69. 'Argument to ' . __METHOD__ .
  70. " must be a valid Logger Interface (aka. Monolog), " .
  71. get_class($logger) . ' given.'
  72. );
  73. }
  74. $this->logger = $logger;
  75. }
  76. /**
  77. * @return Psr\Log\LoggerInterface|null
  78. */
  79. public function getLogger()
  80. {
  81. return $this->logger;
  82. }
  83. /**
  84. * Add error trace to output.
  85. * @param bool|null $addTraceToOutput
  86. * @return bool|$this
  87. */
  88. public function addTraceToOutput($addTraceToOutput = null)
  89. {
  90. if (func_num_args() == 0) {
  91. return $this->addTraceToOutput;
  92. }
  93. $this->addTraceToOutput = (bool) $addTraceToOutput;
  94. return $this;
  95. }
  96. /**
  97. * Add error trace function arguments to output.
  98. * Set to True for all frame args, or integer for the n first frame args.
  99. * @param bool|integer|null $addTraceFunctionArgsToOutput
  100. * @return null|bool|integer
  101. */
  102. public function addTraceFunctionArgsToOutput($addTraceFunctionArgsToOutput = null)
  103. {
  104. if (func_num_args() == 0) {
  105. return $this->addTraceFunctionArgsToOutput;
  106. }
  107. if (! is_integer($addTraceFunctionArgsToOutput)) {
  108. $this->addTraceFunctionArgsToOutput = (bool) $addTraceFunctionArgsToOutput;
  109. } else {
  110. $this->addTraceFunctionArgsToOutput = $addTraceFunctionArgsToOutput;
  111. }
  112. }
  113. /**
  114. * Set the size limit in bytes of frame arguments var_dump output.
  115. * If the limit is reached, the var_dump output is discarded.
  116. * Prevent memory limit errors.
  117. * @var integer
  118. */
  119. public function setTraceFunctionArgsOutputLimit($traceFunctionArgsOutputLimit)
  120. {
  121. $this->traceFunctionArgsOutputLimit = (integer) $traceFunctionArgsOutputLimit;
  122. }
  123. /**
  124. * Get the size limit in bytes of frame arguments var_dump output.
  125. * If the limit is reached, the var_dump output is discarded.
  126. * Prevent memory limit errors.
  127. * @return integer
  128. */
  129. public function getTraceFunctionArgsOutputLimit()
  130. {
  131. return $this->traceFunctionArgsOutputLimit;
  132. }
  133. /**
  134. * Restrict error handling to command line calls.
  135. * @param bool|null $onlyForCommandLine
  136. * @return null|bool
  137. */
  138. public function onlyForCommandLine($onlyForCommandLine = null)
  139. {
  140. if (func_num_args() == 0) {
  141. return $this->onlyForCommandLine;
  142. }
  143. $this->onlyForCommandLine = (bool) $onlyForCommandLine;
  144. }
  145. /**
  146. * Output the error message only if using command line.
  147. * else, output to logger if available.
  148. * Allow to safely add this handler to web pages.
  149. * @param bool|null $outputOnlyIfCommandLine
  150. * @return null|bool
  151. */
  152. public function outputOnlyIfCommandLine($outputOnlyIfCommandLine = null)
  153. {
  154. if (func_num_args() == 0) {
  155. return $this->outputOnlyIfCommandLine;
  156. }
  157. $this->outputOnlyIfCommandLine = (bool) $outputOnlyIfCommandLine;
  158. }
  159. /**
  160. * Only output to logger.
  161. * @param bool|null $loggerOnly
  162. * @return null|bool
  163. */
  164. public function loggerOnly($loggerOnly = null)
  165. {
  166. if (func_num_args() == 0) {
  167. return $this->loggerOnly;
  168. }
  169. $this->loggerOnly = (bool) $loggerOnly;
  170. }
  171. /**
  172. * Check, if possible, that this execution was triggered by a command line.
  173. * @return bool
  174. */
  175. private function isCommandLine()
  176. {
  177. return PHP_SAPI == 'cli';
  178. }
  179. /**
  180. * Test if handler can process the exception..
  181. * @return bool
  182. */
  183. private function canProcess()
  184. {
  185. return $this->isCommandLine() || !$this->onlyForCommandLine();
  186. }
  187. /**
  188. * Test if handler can output to stdout.
  189. * @return bool
  190. */
  191. private function canOutput()
  192. {
  193. return ($this->isCommandLine() || ! $this->outputOnlyIfCommandLine())
  194. && ! $this->loggerOnly();
  195. }
  196. /**
  197. * Get the frame args var_dump.
  198. * @param \Whoops\Exception\Frame $frame [description]
  199. * @param integer $line [description]
  200. * @return string
  201. */
  202. private function getFrameArgsOutput(Frame $frame, $line)
  203. {
  204. if ($this->addTraceFunctionArgsToOutput() === false
  205. || $this->addTraceFunctionArgsToOutput() < $line) {
  206. return '';
  207. }
  208. // Dump the arguments:
  209. ob_start();
  210. var_dump($frame->getArgs());
  211. if (ob_get_length() > $this->getTraceFunctionArgsOutputLimit()) {
  212. // The argument var_dump is to big.
  213. // Discarded to limit memory usage.
  214. ob_clean();
  215. return sprintf(
  216. "\n%sArguments dump length greater than %d Bytes. Discarded.",
  217. self::VAR_DUMP_PREFIX,
  218. $this->getTraceFunctionArgsOutputLimit()
  219. );
  220. }
  221. return sprintf("\n%s",
  222. preg_replace('/^/m', self::VAR_DUMP_PREFIX, ob_get_clean())
  223. );
  224. }
  225. /**
  226. * Get the exception trace as plain text.
  227. * @return string
  228. */
  229. private function getTraceOutput()
  230. {
  231. if (! $this->addTraceToOutput()) {
  232. return '';
  233. }
  234. $inspector = $this->getInspector();
  235. $frames = $inspector->getFrames();
  236. $response = "\nStack trace:";
  237. $line = 1;
  238. foreach ($frames as $frame) {
  239. /** @var Frame $frame */
  240. $class = $frame->getClass();
  241. $template = "\n%3d. %s->%s() %s:%d%s";
  242. if (! $class) {
  243. // Remove method arrow (->) from output.
  244. $template = "\n%3d. %s%s() %s:%d%s";
  245. }
  246. $response .= sprintf(
  247. $template,
  248. $line,
  249. $class,
  250. $frame->getFunction(),
  251. $frame->getFile(),
  252. $frame->getLine(),
  253. $this->getFrameArgsOutput($frame, $line)
  254. );
  255. $line++;
  256. }
  257. return $response;
  258. }
  259. /**
  260. * @return int
  261. */
  262. public function handle()
  263. {
  264. if (! $this->canProcess()) {
  265. return Handler::DONE;
  266. }
  267. $exception = $this->getException();
  268. // $response = sprintf("%s: %s in file %s on line %d%s\n",
  269. // get_class($exception),
  270. // $exception->getMessage(),
  271. // $exception->getFile(),
  272. // $exception->getLine(),
  273. // // $this->getTraceOutput()
  274. // null
  275. // );
  276. //var_dump($exception);
  277. // echo "1";
  278. if ($this->getLogger()) {
  279. $this->getLogger()->error($response);
  280. }
  281. if(! is_null ($exception))
  282. {
  283. $msg=$this->msg;
  284. //include_once '/core/Access/ErrorDisplayer/simple.php';
  285. ?>
  286. <head>
  287. <meta charset="utf-8"/>
  288. <title><?php echo $msg ?></title>
  289. <style type="text/css">
  290. body
  291. {
  292. background: #e9e9e9;
  293. margin: 0px;
  294. padding: 0px;
  295. }
  296. div
  297. {
  298. border:1px solid gray;
  299. border-radius:5px;
  300. display: inline-block;
  301. padding:30px;
  302. font-size: 16px;
  303. font: 20px Georgia, "Times New Roman", Times, serif;
  304. width: 460px;
  305. margin: 60px auto;
  306. display: block;
  307. background: white;
  308. }
  309. </style>
  310. </head>
  311. <body>
  312. <div><?php echo $msg ?></div>
  313. </body>
  314. <?php
  315. }
  316. return Handler::QUIT;
  317. if (! $this->canOutput()) {
  318. return Handler::DONE;
  319. }
  320. if (class_exists('\Whoops\Util\Misc')
  321. && \Whoops\Util\Misc::canSendHeaders()) {
  322. //header('Content-Type: html/text');
  323. }
  324. //if(! is_null ($exception)) echo "fff";
  325. return Handler::QUIT;
  326. }
  327. }