@@ -14,227 +14,227 @@ |
||
| 14 | 14 | class Display |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** @var array */ |
|
| 18 | - protected $defaults = array( |
|
| 19 | - 'script_path' => 'asset/script.js', |
|
| 20 | - 'style_path' => 'asset/style.css' |
|
| 21 | - ); |
|
| 22 | - |
|
| 23 | - /** @var array */ |
|
| 24 | - protected $options; |
|
| 25 | - |
|
| 26 | - /** @var array */ |
|
| 27 | - protected $output; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @param array $options |
|
| 31 | - */ |
|
| 32 | - public function __construct(array $options = array()) |
|
| 33 | - { |
|
| 34 | - $options = array_intersect_key($options, $this->defaults); |
|
| 35 | - $this->options = array_replace($this->defaults, $options); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - public function setConsole(Console $console) |
|
| 39 | - { |
|
| 40 | - $console_data = array( |
|
| 41 | - 'messages' => array(), |
|
| 42 | - 'meta' => array( |
|
| 43 | - 'log' => 0, |
|
| 44 | - 'memory' => 0, |
|
| 45 | - 'error' => 0, |
|
| 46 | - 'speed' => 0 |
|
| 47 | - ) |
|
| 48 | - ); |
|
| 49 | - foreach ($console->getLogs() as $log) { |
|
| 50 | - switch($log['type']) { |
|
| 51 | - case 'log': |
|
| 52 | - $message = array( |
|
| 53 | - 'message' => print_r($log['data'], true), |
|
| 54 | - 'type' => 'log' |
|
| 55 | - ); |
|
| 56 | - $console_data['meta']['log']++; |
|
| 57 | - break; |
|
| 58 | - case 'memory': |
|
| 59 | - $message = array( |
|
| 60 | - 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
| 61 | - 'data' => self::getReadableMemory($log['data']), |
|
| 62 | - 'type' => 'memory' |
|
| 63 | - ); |
|
| 64 | - $console_data['meta']['memory']++; |
|
| 65 | - break; |
|
| 66 | - case 'error': |
|
| 67 | - $message = array( |
|
| 68 | - 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
| 69 | - 'type' => 'error' |
|
| 70 | - ); |
|
| 71 | - $console_data['meta']['error']++; |
|
| 72 | - break; |
|
| 73 | - case 'speed': |
|
| 74 | - $elapsedTime = $log['data'] - $this->startTime; |
|
| 75 | - $message = array( |
|
| 76 | - 'message' => $log['name'], |
|
| 77 | - 'data' => self::getReadableTime($elapsedTime), |
|
| 78 | - 'type' => 'speed' |
|
| 79 | - ); |
|
| 80 | - $console_data['meta']['speed']++; |
|
| 81 | - break; |
|
| 82 | - default: |
|
| 83 | - $message = array( |
|
| 84 | - 'message' => "Unrecognized console log type: {$log['type']}", |
|
| 85 | - 'type' => 'error' |
|
| 86 | - ); |
|
| 87 | - $console_data['meta']['error']++; |
|
| 88 | - break; |
|
| 89 | - } |
|
| 90 | - array_push($console_data['messages'], $message); |
|
| 91 | - } |
|
| 92 | - $this->output['console'] = $console_data; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Sets file data |
|
| 97 | - * |
|
| 98 | - * @param array $data |
|
| 99 | - */ |
|
| 100 | - public function setFileData(array $data) |
|
| 101 | - { |
|
| 102 | - $fileData = array( |
|
| 103 | - 'fileList' => array(), |
|
| 104 | - 'fileTotals' => array( |
|
| 105 | - 'count' => count($data), |
|
| 106 | - 'size' => 0, |
|
| 107 | - 'largest' => 0 |
|
| 108 | - ) |
|
| 109 | - ); |
|
| 110 | - |
|
| 111 | - foreach ($data as $file) { |
|
| 112 | - array_push($fileData['fileList'], array( |
|
| 113 | - 'name' => $file['name'], |
|
| 114 | - 'size' => self::getReadableMemory($file['size']) |
|
| 115 | - )); |
|
| 116 | - |
|
| 117 | - $fileData['fileTotals']['size'] += $file['size']; |
|
| 118 | - if ($file['size'] > $fileData['fileTotals']['largest']) { |
|
| 119 | - $fileData['fileTotals']['largest'] = $file['size']; |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - $fileData['fileTotals']['size'] = self::getReadableMemory($fileData['fileTotals']['size']); |
|
| 124 | - $fileData['fileTotals']['largest'] = self::getReadableMemory($fileData['fileTotals']['largest']); |
|
| 125 | - |
|
| 126 | - $this->output['files'] = $fileData['fileList']; |
|
| 127 | - $this->output['fileTotals'] = $fileData['fileTotals']; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Sets memory data |
|
| 132 | - * |
|
| 133 | - * @param array $data |
|
| 134 | - */ |
|
| 135 | - public function setMemoryData(array $data) |
|
| 136 | - { |
|
| 137 | - $this->output['memory'] = array( |
|
| 138 | - 'used' => self::getReadableMemory($data['used']), |
|
| 139 | - 'allowed' => $data['allowed'] |
|
| 140 | - ); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - public function setQueryData(array $data) |
|
| 144 | - { |
|
| 145 | - $queryData = array( |
|
| 146 | - 'queries' => array(), |
|
| 147 | - 'queryTotals' => array( |
|
| 148 | - 'count' => count($data), |
|
| 149 | - 'time' => 0, |
|
| 150 | - ) |
|
| 151 | - ); |
|
| 152 | - |
|
| 153 | - foreach ($data as $query) { |
|
| 154 | - array_push($queryData['queries'], array( |
|
| 155 | - 'sql' => $query['sql'], |
|
| 156 | - 'explain' => $query['explain'], |
|
| 157 | - 'time' => self::getReadableTime($query['time']) |
|
| 158 | - )); |
|
| 159 | - |
|
| 160 | - $queryData['queryTotals']['time'] += $query['time']; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $queryData['queryTotals']['time'] = self::getReadableTime($queryData['queryTotals']['time']); |
|
| 164 | - |
|
| 165 | - $this->output['queries'] = $queryData['queries']; |
|
| 166 | - $this->output['queryTotals'] = $queryData['queryTotals']; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * Sets speed data |
|
| 171 | - * |
|
| 172 | - * @param array $data |
|
| 173 | - */ |
|
| 174 | - public function setSpeedData(array $data) |
|
| 175 | - { |
|
| 176 | - $this->output['speed'] = array( |
|
| 177 | - 'elapsed' => self::getReadableTime($data['elapsed']), |
|
| 178 | - 'allowed' => self::getReadableTime($data['allowed'], 0) |
|
| 179 | - ); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * Static formatter for human-readable time |
|
| 184 | - * Only handles time up to 60 minutes gracefully |
|
| 185 | - * |
|
| 186 | - * @param double $time |
|
| 187 | - * @param integer $decimals |
|
| 188 | - * @return string |
|
| 189 | - */ |
|
| 190 | - public static function getReadableTime($time, $decimals = 3) |
|
| 191 | - { |
|
| 192 | - $unit = 's'; |
|
| 193 | - if ($time < 1) { |
|
| 194 | - $time *= 1000; |
|
| 195 | - $unit = 'ms'; |
|
| 196 | - } else if ($time > 60) { |
|
| 197 | - $time /= 60; |
|
| 198 | - $unit = 'm'; |
|
| 199 | - } |
|
| 200 | - $time = number_format($time, $decimals); |
|
| 201 | - return "{$time} {$unit}"; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Static formatter for human-readable memory |
|
| 206 | - * |
|
| 207 | - * @param double $size |
|
| 208 | - * @param integer $decimals |
|
| 209 | - */ |
|
| 210 | - public static function getReadableMemory($size, $decimals = 2) |
|
| 211 | - { |
|
| 212 | - $unitOptions = array('b', 'k', 'M', 'G'); |
|
| 213 | - |
|
| 214 | - $base = log($size, 1024); |
|
| 215 | - |
|
| 216 | - $memory = round(pow(1024, $base - floor($base)), $decimals); |
|
| 217 | - $unit = $unitOptions[floor($base)]; |
|
| 218 | - return "{$memory} {$unit}"; |
|
| 219 | - } |
|
| 17 | + /** @var array */ |
|
| 18 | + protected $defaults = array( |
|
| 19 | + 'script_path' => 'asset/script.js', |
|
| 20 | + 'style_path' => 'asset/style.css' |
|
| 21 | + ); |
|
| 22 | + |
|
| 23 | + /** @var array */ |
|
| 24 | + protected $options; |
|
| 25 | + |
|
| 26 | + /** @var array */ |
|
| 27 | + protected $output; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @param array $options |
|
| 31 | + */ |
|
| 32 | + public function __construct(array $options = array()) |
|
| 33 | + { |
|
| 34 | + $options = array_intersect_key($options, $this->defaults); |
|
| 35 | + $this->options = array_replace($this->defaults, $options); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + public function setConsole(Console $console) |
|
| 39 | + { |
|
| 40 | + $console_data = array( |
|
| 41 | + 'messages' => array(), |
|
| 42 | + 'meta' => array( |
|
| 43 | + 'log' => 0, |
|
| 44 | + 'memory' => 0, |
|
| 45 | + 'error' => 0, |
|
| 46 | + 'speed' => 0 |
|
| 47 | + ) |
|
| 48 | + ); |
|
| 49 | + foreach ($console->getLogs() as $log) { |
|
| 50 | + switch($log['type']) { |
|
| 51 | + case 'log': |
|
| 52 | + $message = array( |
|
| 53 | + 'message' => print_r($log['data'], true), |
|
| 54 | + 'type' => 'log' |
|
| 55 | + ); |
|
| 56 | + $console_data['meta']['log']++; |
|
| 57 | + break; |
|
| 58 | + case 'memory': |
|
| 59 | + $message = array( |
|
| 60 | + 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
| 61 | + 'data' => self::getReadableMemory($log['data']), |
|
| 62 | + 'type' => 'memory' |
|
| 63 | + ); |
|
| 64 | + $console_data['meta']['memory']++; |
|
| 65 | + break; |
|
| 66 | + case 'error': |
|
| 67 | + $message = array( |
|
| 68 | + 'message' => "Line {$log['line']}: {$log['data']} in {$log['file']}", |
|
| 69 | + 'type' => 'error' |
|
| 70 | + ); |
|
| 71 | + $console_data['meta']['error']++; |
|
| 72 | + break; |
|
| 73 | + case 'speed': |
|
| 74 | + $elapsedTime = $log['data'] - $this->startTime; |
|
| 75 | + $message = array( |
|
| 76 | + 'message' => $log['name'], |
|
| 77 | + 'data' => self::getReadableTime($elapsedTime), |
|
| 78 | + 'type' => 'speed' |
|
| 79 | + ); |
|
| 80 | + $console_data['meta']['speed']++; |
|
| 81 | + break; |
|
| 82 | + default: |
|
| 83 | + $message = array( |
|
| 84 | + 'message' => "Unrecognized console log type: {$log['type']}", |
|
| 85 | + 'type' => 'error' |
|
| 86 | + ); |
|
| 87 | + $console_data['meta']['error']++; |
|
| 88 | + break; |
|
| 89 | + } |
|
| 90 | + array_push($console_data['messages'], $message); |
|
| 91 | + } |
|
| 92 | + $this->output['console'] = $console_data; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Sets file data |
|
| 97 | + * |
|
| 98 | + * @param array $data |
|
| 99 | + */ |
|
| 100 | + public function setFileData(array $data) |
|
| 101 | + { |
|
| 102 | + $fileData = array( |
|
| 103 | + 'fileList' => array(), |
|
| 104 | + 'fileTotals' => array( |
|
| 105 | + 'count' => count($data), |
|
| 106 | + 'size' => 0, |
|
| 107 | + 'largest' => 0 |
|
| 108 | + ) |
|
| 109 | + ); |
|
| 110 | + |
|
| 111 | + foreach ($data as $file) { |
|
| 112 | + array_push($fileData['fileList'], array( |
|
| 113 | + 'name' => $file['name'], |
|
| 114 | + 'size' => self::getReadableMemory($file['size']) |
|
| 115 | + )); |
|
| 116 | + |
|
| 117 | + $fileData['fileTotals']['size'] += $file['size']; |
|
| 118 | + if ($file['size'] > $fileData['fileTotals']['largest']) { |
|
| 119 | + $fileData['fileTotals']['largest'] = $file['size']; |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + $fileData['fileTotals']['size'] = self::getReadableMemory($fileData['fileTotals']['size']); |
|
| 124 | + $fileData['fileTotals']['largest'] = self::getReadableMemory($fileData['fileTotals']['largest']); |
|
| 125 | + |
|
| 126 | + $this->output['files'] = $fileData['fileList']; |
|
| 127 | + $this->output['fileTotals'] = $fileData['fileTotals']; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Sets memory data |
|
| 132 | + * |
|
| 133 | + * @param array $data |
|
| 134 | + */ |
|
| 135 | + public function setMemoryData(array $data) |
|
| 136 | + { |
|
| 137 | + $this->output['memory'] = array( |
|
| 138 | + 'used' => self::getReadableMemory($data['used']), |
|
| 139 | + 'allowed' => $data['allowed'] |
|
| 140 | + ); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + public function setQueryData(array $data) |
|
| 144 | + { |
|
| 145 | + $queryData = array( |
|
| 146 | + 'queries' => array(), |
|
| 147 | + 'queryTotals' => array( |
|
| 148 | + 'count' => count($data), |
|
| 149 | + 'time' => 0, |
|
| 150 | + ) |
|
| 151 | + ); |
|
| 152 | + |
|
| 153 | + foreach ($data as $query) { |
|
| 154 | + array_push($queryData['queries'], array( |
|
| 155 | + 'sql' => $query['sql'], |
|
| 156 | + 'explain' => $query['explain'], |
|
| 157 | + 'time' => self::getReadableTime($query['time']) |
|
| 158 | + )); |
|
| 159 | + |
|
| 160 | + $queryData['queryTotals']['time'] += $query['time']; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $queryData['queryTotals']['time'] = self::getReadableTime($queryData['queryTotals']['time']); |
|
| 164 | + |
|
| 165 | + $this->output['queries'] = $queryData['queries']; |
|
| 166 | + $this->output['queryTotals'] = $queryData['queryTotals']; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * Sets speed data |
|
| 171 | + * |
|
| 172 | + * @param array $data |
|
| 173 | + */ |
|
| 174 | + public function setSpeedData(array $data) |
|
| 175 | + { |
|
| 176 | + $this->output['speed'] = array( |
|
| 177 | + 'elapsed' => self::getReadableTime($data['elapsed']), |
|
| 178 | + 'allowed' => self::getReadableTime($data['allowed'], 0) |
|
| 179 | + ); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * Static formatter for human-readable time |
|
| 184 | + * Only handles time up to 60 minutes gracefully |
|
| 185 | + * |
|
| 186 | + * @param double $time |
|
| 187 | + * @param integer $decimals |
|
| 188 | + * @return string |
|
| 189 | + */ |
|
| 190 | + public static function getReadableTime($time, $decimals = 3) |
|
| 191 | + { |
|
| 192 | + $unit = 's'; |
|
| 193 | + if ($time < 1) { |
|
| 194 | + $time *= 1000; |
|
| 195 | + $unit = 'ms'; |
|
| 196 | + } else if ($time > 60) { |
|
| 197 | + $time /= 60; |
|
| 198 | + $unit = 'm'; |
|
| 199 | + } |
|
| 200 | + $time = number_format($time, $decimals); |
|
| 201 | + return "{$time} {$unit}"; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Static formatter for human-readable memory |
|
| 206 | + * |
|
| 207 | + * @param double $size |
|
| 208 | + * @param integer $decimals |
|
| 209 | + */ |
|
| 210 | + public static function getReadableMemory($size, $decimals = 2) |
|
| 211 | + { |
|
| 212 | + $unitOptions = array('b', 'k', 'M', 'G'); |
|
| 213 | + |
|
| 214 | + $base = log($size, 1024); |
|
| 215 | + |
|
| 216 | + $memory = round(pow(1024, $base - floor($base)), $decimals); |
|
| 217 | + $unit = $unitOptions[floor($base)]; |
|
| 218 | + return "{$memory} {$unit}"; |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - public function __invoke() |
|
| 222 | - { |
|
| 223 | - $output = $this->output; |
|
| 224 | - $header = array( |
|
| 225 | - 'console' => count($output['console']['messages']), |
|
| 226 | - 'speed' => $output['speed']['elapsed'], |
|
| 227 | - 'query' => $output['queryTotals']['count'], |
|
| 228 | - 'memory' => $output['memory']['used'], |
|
| 229 | - 'files' => count($output['files']) |
|
| 230 | - ); |
|
| 231 | - |
|
| 232 | - $console = $output['console']; |
|
| 233 | - |
|
| 234 | - // todo is this really the best way to load these? |
|
| 235 | - $styles = file_get_contents(__DIR__ . "./../{$this->options['style_path']}"); |
|
| 236 | - $script = file_get_contents(__DIR__ . "./../{$this->options['script_path']}"); |
|
| 237 | - |
|
| 238 | - require_once __DIR__ .'/../asset/display.tpl.php'; |
|
| 239 | - } |
|
| 221 | + public function __invoke() |
|
| 222 | + { |
|
| 223 | + $output = $this->output; |
|
| 224 | + $header = array( |
|
| 225 | + 'console' => count($output['console']['messages']), |
|
| 226 | + 'speed' => $output['speed']['elapsed'], |
|
| 227 | + 'query' => $output['queryTotals']['count'], |
|
| 228 | + 'memory' => $output['memory']['used'], |
|
| 229 | + 'files' => count($output['files']) |
|
| 230 | + ); |
|
| 231 | + |
|
| 232 | + $console = $output['console']; |
|
| 233 | + |
|
| 234 | + // todo is this really the best way to load these? |
|
| 235 | + $styles = file_get_contents(__DIR__ . "./../{$this->options['style_path']}"); |
|
| 236 | + $script = file_get_contents(__DIR__ . "./../{$this->options['script_path']}"); |
|
| 237 | + |
|
| 238 | + require_once __DIR__ .'/../asset/display.tpl.php'; |
|
| 239 | + } |
|
| 240 | 240 | } |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | ) |
| 48 | 48 | ); |
| 49 | 49 | foreach ($console->getLogs() as $log) { |
| 50 | - switch($log['type']) { |
|
| 50 | + switch ($log['type']) { |
|
| 51 | 51 | case 'log': |
| 52 | 52 | $message = array( |
| 53 | 53 | 'message' => print_r($log['data'], true), |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | break; |
| 58 | 58 | case 'memory': |
| 59 | 59 | $message = array( |
| 60 | - 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '') . $log['name'], |
|
| 60 | + 'message' => (!empty($log['data_type']) ? "{$log['data_type']}: " : '').$log['name'], |
|
| 61 | 61 | 'data' => self::getReadableMemory($log['data']), |
| 62 | 62 | 'type' => 'memory' |
| 63 | 63 | ); |
@@ -232,9 +232,9 @@ discard block |
||
| 232 | 232 | $console = $output['console']; |
| 233 | 233 | |
| 234 | 234 | // todo is this really the best way to load these? |
| 235 | - $styles = file_get_contents(__DIR__ . "./../{$this->options['style_path']}"); |
|
| 236 | - $script = file_get_contents(__DIR__ . "./../{$this->options['script_path']}"); |
|
| 235 | + $styles = file_get_contents(__DIR__."./../{$this->options['style_path']}"); |
|
| 236 | + $script = file_get_contents(__DIR__."./../{$this->options['script_path']}"); |
|
| 237 | 237 | |
| 238 | - require_once __DIR__ .'/../asset/display.tpl.php'; |
|
| 238 | + require_once __DIR__.'/../asset/display.tpl.php'; |
|
| 239 | 239 | } |
| 240 | 240 | } |