|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Classes/Debug.php. |
| 4 | + * |
| 5 | + * The Debug class is responsible for providing a log of relevant debugging information. |
| 6 | + * It has functionality to generate a log file as it goes allowing you to print it at any |
| 7 | + * given point in the script. It also acts as a portal for writing to a console output |
| 8 | + * using FirePHP. |
| 9 | + * |
| 10 | + * @version 0.9 |
| 11 | + * |
| 12 | + * @author Joey Kimsey <joeyk4816@gmail.com> |
| 13 | + * |
| 14 | + * @link https://github.com/JoeyK4816/tempus-project-core |
| 15 | + * |
| 16 | + * @license https://opensource.org/licenses/MIT [MIT LICENSE] |
| 17 | + */ |
| 18 | + |
| 19 | +namespace TempusProjectCore\Classes; |
| 20 | + |
| 21 | +use \FirePHP as FirePHP; |
| 22 | +class Debug |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Toggle Debugging mode on or off. |
| 26 | + * |
| 27 | + * @var bool |
| 28 | + */ |
| 29 | + private static $debug_status = false; |
| 30 | + |
| 31 | + /** |
| 32 | + * Very Important, this will enable the firebug console output. |
| 33 | + * It only applies when debugging is enabled, or the config cannot |
| 34 | + * be found as a safety net. |
| 35 | + * |
| 36 | + * @var bool |
| 37 | + */ |
| 38 | + private static $console = false; |
| 39 | + private static $error_trace = false; |
| 40 | + private static $group = false; |
| 41 | + private static $fire = null; |
| 42 | + private static $debug_log = null; |
| 43 | + |
| 44 | + /** |
| 45 | + * Acts as a constructor. |
| 46 | + */ |
| 47 | + private static function _start() |
| 48 | + { |
| 49 | + if (Self::$console) { |
| 50 | + ob_start(); |
| 51 | + Self::$fire = FirePHP::getInstance(true);; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns the current Debug Status;. |
| 57 | + * |
| 58 | + * @return bool |
| 59 | + */ |
| 60 | + public static function status() |
| 61 | + { |
| 62 | + return Self::$debug_status; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * This is the interface that writes to our log file/console depending on input type. |
| 68 | + * |
| 69 | + * @param string $type - Debugging type. |
| 70 | + * @param string $data - Debugging data. |
| 71 | + * |
| 72 | + * @todo make a case statement |
| 73 | + */ |
| 74 | + private static function put($type, $data = null, $params = null) |
| 75 | + { |
| 76 | + if (!Self::$debug_status) { |
| 77 | + return; |
| 78 | + } |
| 79 | + if (strlen(Self::$debug_log) > 50000) { |
| 80 | + Self::$fire->log('Error log too large, possible loop.'); |
| 81 | + Self::$debug_status = false; |
| 82 | + return; |
| 83 | + } |
| 84 | + if (!is_object($data)) { |
| 85 | + Self::$debug_log .= var_export($data, true); |
| 86 | + Self::$debug_log .= '\n'; |
| 87 | + } else { |
| 88 | + Self::$debug_log .= 'cannot save objects'; |
| 89 | + Self::$debug_log .= '\n'; |
| 90 | + } |
| 91 | + if (!Self::$console) { |
| 92 | + return; |
| 93 | + } |
| 94 | + if (!Self::$fire) { |
| 95 | + Self::_start(); |
| 96 | + } |
| 97 | + switch ($type) { |
| 98 | + case 'variable': |
| 99 | + Self::$fire->info($data, $params); |
| 100 | + break; |
| 101 | + |
| 102 | + case 'group_end': |
| 103 | + Self::$fire->groupEnd(); |
| 104 | + break; |
| 105 | + |
| 106 | + case 'trace': |
| 107 | + Self::$fire->trace($data); |
| 108 | + break; |
| 109 | + |
| 110 | + case 'group': |
| 111 | + if ($params) { |
| 112 | + Self::$fire->group($data, $params); |
| 113 | + } else { |
| 114 | + Self::$fire->group($data); |
| 115 | + } |
| 116 | + break; |
| 117 | + |
| 118 | + case 'info': |
| 119 | + Self::$fire->$type('color: #1452ff', '%c' . $data); |
| 120 | + break; |
| 121 | + |
| 122 | + default: |
| 123 | + Self::$fire->$type($data); |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Ends a group. |
| 130 | + */ |
| 131 | + public static function gend() |
| 132 | + { |
| 133 | + Self::$group = false; |
| 134 | + Self::put('group_end'); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Creates a group divider into the console output. |
| 139 | + * |
| 140 | + * @param string $data name of the group |
| 141 | + * @param wild $collapsed if anything is present the group will be collapsed by default |
| 142 | + */ |
| 143 | + public static function group($data, $collapsed = null) |
| 144 | + { |
| 145 | + if (!empty($collapsed)) { |
| 146 | + $params = array('Collapsed' => true); |
| 147 | + Self::put('group', $data, $params); |
| 148 | + } else { |
| 149 | + Self::put('group', $data); |
| 150 | + } |
| 151 | + Self::$group = true; |
| 152 | + } |
| 153 | + /** |
| 154 | + * Allows you to print the contents of any variable into the console. |
| 155 | + * |
| 156 | + * @param WILD $var - The variable you wish to read. |
| 157 | + * @param string $data - Optional name for the variable output. |
| 158 | + */ |
| 159 | + public static function v($var, $data = null) |
| 160 | + { |
| 161 | + if (!isset($data)) { |
| 162 | + $data = 'Default Variable label'; |
| 163 | + } |
| 164 | + Self::put('variable', $var, $data); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Socket function for a basic debugging log. |
| 169 | + * |
| 170 | + * @param string $data - The debug data. |
| 171 | + */ |
| 172 | + public static function log($data, $params = null) |
| 173 | + { |
| 174 | + Self::put('log', $data); |
| 175 | + if (!empty($params)) { |
| 176 | + Self::gend(); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Provides a stack trace from the current calling spot. |
| 182 | + * |
| 183 | + * @param string $data the name of the trace |
| 184 | + */ |
| 185 | + public static function trace($data = 'Default Trace') |
| 186 | + { |
| 187 | + Self::group("$data", 1); |
| 188 | + Self::put('trace', $data); |
| 189 | + Self::gend(); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Socket function for debugging info. |
| 194 | + * |
| 195 | + * @param string $data - The debug data. |
| 196 | + */ |
| 197 | + public static function info($data, $params = null) |
| 198 | + { |
| 199 | + Self::put('info', $data); |
| 200 | + if (!empty($params)) { |
| 201 | + Self::gend(); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Socket function for a debugging warning. |
| 207 | + * |
| 208 | + * @param string $data - The debug data. |
| 209 | + */ |
| 210 | + public static function warn($data, $params = null) |
| 211 | + { |
| 212 | + Self::put('warn', $data); |
| 213 | + if (!empty($params)) { |
| 214 | + Self::gend(); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Socket function for a debugging error. |
| 220 | + * |
| 221 | + * @param string $data - The debug data. |
| 222 | + */ |
| 223 | + public static function error($data, $params = null) |
| 224 | + { |
| 225 | + Self::put('error', $data); |
| 226 | + if (Self::$error_trace) { |
| 227 | + Self::trace(); |
| 228 | + } |
| 229 | + if (!empty($params)) { |
| 230 | + Self::gend(); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * This returns the contents of the debug log. |
| 236 | + * |
| 237 | + * @return string - Returns the entire debug log. |
| 238 | + */ |
| 239 | + public static function dump() |
| 240 | + { |
| 241 | + return var_dump(Self::$debug_log); |
| 242 | + } |
| 243 | +} |
0 commit comments