FusionDirectory
class_FusionDirectoryError.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2019-2020 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
25 {
26  protected $htmlMessage;
27 
28  public function __construct (string $htmlMessage = '', int $code = 0, Throwable $previous = NULL)
29  {
30  $this->htmlMessage = $htmlMessage;
31  parent::__construct(htmlunescape(strip_tags($htmlMessage)), $code, $previous);
32  }
33 
34  public function getHtmlMessage ()
35  {
36  return $this->htmlMessage;
37  }
38 
39  public function toArray (): array
40  {
41  return [
42  'class' => get_class($this),
43  'message' => $this->getMessage(),
44  'line' => $this->getLine(),
45  'file' => $this->getFile(),
46  ];
47  }
48 
49  public function __toString ()
50  {
51  return $this->getMessage();
52  }
53 
54  public function computeMsgDialogParameters (): array
55  {
56  return [_('Error'), $this->htmlMessage, ERROR_DIALOG, static::formatTrace($this)];
57  }
58 
59  public function display ()
60  {
61  msg_dialog::display(...$this->computeMsgDialogParameters());
62  }
63 
64  public static function formatTrace (Throwable $throwable): array
65  {
66  $trace = $throwable->getTrace();
67 
68  foreach ($trace as &$traceStep) {
69  if (isset($traceStep['function']) && isset($traceStep['class']) && isset($traceStep['type'])) {
70  $traceStep['function'] = $traceStep['class'].$traceStep['type'].$traceStep['function'];
71  unset($traceStep['class']);
72  unset($traceStep['type']);
73  }
74  }
75  unset($traceStep);
76 
77  array_unshift(
78  $trace,
79  [
80  'file' => $throwable->getFile(),
81  'line' => $throwable->getLine(),
82  ]
83  );
84 
85  if ($previous = $throwable->getPrevious()) {
86  $trace[] = static::formatTrace($previous);
87  }
88 
89  return $trace;
90  }
91 }
static display($title, string $message, int $type=INFO_DIALOG, array $trace=[])
Display a message dialog.
Parent class for all errors in FusionDirectory.
htmlunescape(string $html)
Unescape string for HTML output, reverse of htmlescape.
Definition: php_setup.inc:40