| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /***************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Title : Php Quick Profiler Display Class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Author : Created by Ryan Campbell | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * URL : http://particletree.com/features/php-quick-profiler/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Description : This is a hacky way of pushing profiling logic to the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  *  PQP HTML. This is great because it will just work in your project, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  *  but it is hard to maintain and read. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | *****************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Particletree\Pqp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class Display | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var  array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $output; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function setConsole(Console $console) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $console_data = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             'messages' => array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             'count'    => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                 'log'    => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                 'memory' => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 'error'  => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 'speed'  => 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         foreach ($console->getLogs() as $log) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             switch($log['type']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 case 'log': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                     $message = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                         'data' => print_r($log['data'], true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                         'type' => 'log' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                     $console_data['count']['log']++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 case 'memory': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     $message = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                         'name' => $log['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                         'data' => self::getReadableMemory($log['data']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                         'type' => 'memory' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     if (!empty($log['data_type'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                         $message['data_type'] = $log['data_type']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                     $console_data['count']['memory']++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 case 'error': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     $message = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                         'data' => $log['data'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                         'file' => $log['file'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                         'line' => $log['line'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                         'type' => 'error' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                     $console_data['count']['error']++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 case 'speed': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     $elapsedTime = $log['data'] - $this->startTime; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     $message = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                         'name' => $log['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                         'data' => self::getReadableTime($elapsedTime), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                         'type' => 'speed' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     $console_data['count']['speed']++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     $message = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                         'data' => "Unrecognized console log type: {$log['type']}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                         'type' => 'error' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     $console_data['count']['error']++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             array_push($console_data['messages'], $message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->output['console'] = $console_data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * Sets file data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function setFileData(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $fileData = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'fileList'   => array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             'fileTotals' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 'count'   => count($data), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 'size'    => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 'largest' => 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         foreach ($data as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             array_push($fileData['fileList'], array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 'name' => $file['name'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 'size' => self::getReadableMemory($file['size']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $fileData['fileTotals']['size'] += $file['size']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             if ($file['size'] > $fileData['fileTotals']['largest']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 $fileData['fileTotals']['largest'] = $file['size']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $fileData['fileTotals']['size'] = self::getReadableMemory($fileData['fileTotals']['size']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $fileData['fileTotals']['largest'] = self::getReadableMemory($fileData['fileTotals']['largest']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $this->output['files'] = $fileData['fileList']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $this->output['fileTotals'] = $fileData['fileTotals']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * Sets memory data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     public function setMemoryData(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $this->output['memory'] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             'used'    => self::getReadableMemory($data['used']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             'allowed' => $data['allowed'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     public function setQueryData(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $queryData = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             'queries'     => array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             'queryTotals' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 'count'   => count($data), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 'time'    => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         foreach ($data as $query) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             array_push($queryData['queries'], array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 'sql'     => $query['sql'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 'explain' => $query['explain'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 'time'    => self::getReadableTime($query['time']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             $queryData['queryTotals']['time'] += $query['time']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $queryData['queryTotals']['time'] = self::getReadableTime($queryData['queryTotals']['time']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->output['queries'] = $queryData['queries']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $this->output['queryTotals'] = $queryData['queryTotals']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * Sets speed data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     public function setSpeedData(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $this->output['speed'] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             'elapsed' => self::getReadableTime($data['elapsed']), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             'allowed' => self::getReadableTime($data['allowed'], 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * Static formatter for human-readable time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * Only handles time up to 60 minutes gracefully | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * @param double  $time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @param integer $decimals | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     public static function getReadableTime($time, $decimals = 3) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         $unit = 's'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         if ($time < 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             $time *= 1000; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             $unit = 'ms'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         } else if ($time > 60) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             $time /= 60; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             $unit = 'm'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         $time = number_format($time, $decimals); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         return "{$time} {$unit}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * Static formatter for human-readable memory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * @param double  $size | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * @param integer $decimals | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 200 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  |     public static function getReadableMemory($size, $decimals = 2) | 
            
                                                                        
                            
            
                                    
            
            
                | 202 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 203 |  |  |         $unitOptions = array('b', 'k', 'M', 'G'); | 
            
                                                                        
                            
            
                                    
            
            
                | 204 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 205 |  |  |         $base = log($size, 1024); | 
            
                                                                        
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 207 |  |  |         $memory = round(pow(1024, $base - floor($base)), $decimals); | 
            
                                                                        
                            
            
                                    
            
            
                | 208 |  |  |         $unit = $unitOptions[floor($base)]; | 
            
                                                                        
                            
            
                                    
            
            
                | 209 |  |  |         return "{$memory} {$unit}"; | 
            
                                                                        
                            
            
                                    
            
            
                | 210 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |   | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |     public function __invoke() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |         $output = $this->output; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |         require_once __DIR__ .'/../asset/display.tpl.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 217 |  |  | }	 | 
            
                                                        
            
                                    
            
            
                | 218 |  |  |  | 
            
                        
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: