initial commit
[namibia] / module / Utility / src / Utility / Debug.php
1 <?php
2 namespace Utility;
3
4
5
6 /**
7  * Some useful debugging functionality.
8  * @author andre.fourie
9  *
10  */
11 class Debug
12 {
13
14         static private $mode = 'html';
15
16         /**
17          * Set output mode.
18          * @param string $mode
19          */
20         static public function mode($mode = 'html')
21         {
22                 self::$mode = $mode;
23         }
24
25         /**
26          * Show output and exit script.
27          * @param array|string $output
28          */
29         static public function dieWith($output)
30         {
31                 if ('html' == self::$mode) {
32                         echo '<pre>';
33                         var_dump($output);
34                         echo '</pre>';
35                 } else {
36                         error_log(var_dump($output));
37                 }
38                 exit();
39         }
40
41         /**
42          * On true evaluation show output and exit script.
43          * @param unknown $evaluation
44          * @param array|string $output
45          */
46         static public function dieIf($evaluation, $output)
47         {
48                 if ($evaluation) {
49                         self::dieWith($output);
50                 }
51         }
52
53         /**
54          * Log something.
55          * @param string $label
56          * @param array|string $output
57          */
58         static public function log($label, $output)
59         {
60                 if ('html' == self::$mode) {
61                         echo "$label:<br />";
62                         echo '<pre>';
63                         var_dump($output);
64                         echo '</pre>';
65                 } else {
66                         error_log("$label:");
67                         error_log(print_r($output, true));
68                 }
69         }
70
71         /**
72          * Log to php error log.
73          * @param string $label
74          * @param array|string $output
75          */
76         static public function errorLog($label, $output)
77         {
78                 error_log("$label: ".print_r($output, true));
79         }
80
81         /**
82          * Recursively strip doctrine entities from stacked array.
83          * @param array $data
84          * @return array
85          */
86         static public function stripEntities(array $data)
87         {
88                 foreach ($data as $key => $value)
89                 {
90                         if (is_object($value) && strpos(get_class($value), 'Entity') !== false)
91                         {
92                                 $data[$key] = self::stripEntities($value->toArray());
93                         }
94                         if (is_array($value))
95                         {
96                                 $data[$key] = self::stripEntities($value);
97                         }
98                 }
99                 return $data;
100         }
101
102         /**
103          * Log to php error log with doctrine entities stripped to arrays.
104          * @param string $label
105          * @param array|string $output
106          */
107         static public function errorLogStripped($label, $output)
108         {
109                 if (is_object($output) && strpos(get_class($output), 'Entity') !== false)
110                 {
111                         $output = $output->toArray();
112                 }
113                 if (is_array($output))
114                 {
115                         foreach ($output as $key => $value)
116                         {
117                                 if (is_object($value) && strpos(get_class($value), 'Entity') !== false)
118                                 {
119                                         $output[$key] = self::stripEntities($value->toArray());
120                                 }
121                                 if (is_array($value))
122                                 {
123                                         $output[$key] = self::stripEntities($value);
124                                 }
125                         }
126                 }
127                 error_log("$label: ".print_r($output, true));
128         }
129
130         /**
131          * Log a debug backtrace to php error log.
132          */
133         static public function logBacktrace()
134         {
135                 error_log(print_r(debug_backtrace(), true));
136         }
137
138         /**
139          * Output php array as html table.
140          * @param array $array
141          * @param string $title
142          * @return string
143          */
144         static public function arrayToTable(array $array, $title = null)
145         {
146                 $html = '
147                 <table border="1">';
148                 if (!empty($title)) {
149                         $html .= '
150                         <thead>
151                         <tr>
152                         <th colspan="2">' . htmlentities($title) . '</th>
153                         </tr>
154                         </thead>';
155                 }
156                 $html .= '
157                 <tbody>';
158                 foreach ($array as $key => $val) {
159                         $type = '';
160                         if (is_object($val)) {
161                                 $type = "\n" . '(Class: ' . get_class($val) . ')';
162                                 $val = $val->toArray();
163                         }
164                         $html .= '
165                         <tr>';
166                         if (is_array($val)) {
167                                 $val = self::arrayToTable($val, $type);
168                         } else {
169                                 $val = htmlentities($val);
170                         }
171                         $html .= "
172                         <td><b>" . htmlentities($key) . ":</b></td>
173                         <td>" . $val . " </td>";
174                         $html .= '
175                         </tr>';
176                 }
177
178                 $html .= '
179                 </tbody>
180                 </table>';
181                 return $html;
182         }
183
184 }