Git Repository Public Repository

namibia

URLs

Copy to Clipboard
 
df0489e1eeeeab5a9bd44e1d84fce49924fe1bac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
namespace Utility;



/**
 * Some useful debugging functionality.
 * @author andre.fourie
 *
 */
class Debug
{

	static private $mode = 'html';

	/**
	 * Set output mode.
	 * @param string $mode
	 */
	static public function mode($mode = 'html')
	{
		self::$mode = $mode;
	}

	/**
	 * Show output and exit script.
	 * @param array|string $output
	 */
	static public function dieWith($output)
	{
		if ('html' == self::$mode) {
			echo '<pre>';
			var_dump($output);
			echo '</pre>';
		} else {
			error_log(var_dump($output));
		}
		exit();
	}

	/**
	 * On true evaluation show output and exit script.
	 * @param unknown $evaluation
	 * @param array|string $output
	 */
	static public function dieIf($evaluation, $output)
	{
		if ($evaluation) {
			self::dieWith($output);
		}
	}

	/**
	 * Log something.
	 * @param string $label
	 * @param array|string $output
	 */
	static public function log($label, $output)
	{
		if ('html' == self::$mode) {
			echo "$label:<br />";
			echo '<pre>';
			var_dump($output);
			echo '</pre>';
		} else {
			error_log("$label:");
			error_log(print_r($output, true));
		}
	}

	/**
	 * Log to php error log.
	 * @param string $label
	 * @param array|string $output
	 */
	static public function errorLog($label, $output)
	{
		error_log("$label: ".print_r($output, true));
	}

	/**
	 * Recursively strip doctrine entities from stacked array.
	 * @param array $data
	 * @return array
	 */
	static public function stripEntities(array $data)
	{
		foreach ($data as $key => $value)
		{
			if (is_object($value) && strpos(get_class($value), 'Entity') !== false)
			{
				$data[$key] = self::stripEntities($value->toArray());
			}
			if (is_array($value))
			{
				$data[$key] = self::stripEntities($value);
			}
		}
		return $data;
	}

	/**
	 * Log to php error log with doctrine entities stripped to arrays.
	 * @param string $label
	 * @param array|string $output
	 */
	static public function errorLogStripped($label, $output)
	{
		if (is_object($output) && strpos(get_class($output), 'Entity') !== false)
		{
			$output = $output->toArray();
		}
		if (is_array($output))
		{
			foreach ($output as $key => $value)
			{
				if (is_object($value) && strpos(get_class($value), 'Entity') !== false)
				{
					$output[$key] = self::stripEntities($value->toArray());
				}
				if (is_array($value))
				{
					$output[$key] = self::stripEntities($value);
				}
			}
		}
		error_log("$label: ".print_r($output, true));
	}

	/**
	 * Log a debug backtrace to php error log.
	 */
	static public function logBacktrace()
	{
		error_log(print_r(debug_backtrace(), true));
	}

	/**
	 * Output php array as html table.
	 * @param array $array
	 * @param string $title
	 * @return string
	 */
	static public function arrayToTable(array $array, $title = null)
	{
		$html = '
		<table border="1">';
		if (!empty($title)) {
			$html .= '
			<thead>
			<tr>
			<th colspan="2">' . htmlentities($title) . '</th>
			</tr>
			</thead>';
		}
		$html .= '
		<tbody>';
		foreach ($array as $key => $val) {
			$type = '';
			if (is_object($val)) {
				$type = "\n" . '(Class: ' . get_class($val) . ')';
				$val = $val->toArray();
			}
			$html .= '
			<tr>';
			if (is_array($val)) {
				$val = self::arrayToTable($val, $type);
			} else {
				$val = htmlentities($val);
			}
			$html .= "
			<td><b>" . htmlentities($key) . ":</b></td>
			<td>" . $val . " </td>";
			$html .= '
			</tr>';
		}

		$html .= '
		</tbody>
		</table>';
		return $html;
	}

}

Commits for namibiamodule/Utility/src/Utility/Debug.php

Diff revisions: vs.
Revision Author Commited Message
df0489 ... Mark Fri 14 Oct, 2016 10:01:00 +0000

initial commit