|
8 | 8 | require 'library/FileHandler.php';
|
9 | 9 |
|
10 | 10 | // TODO: Errorhandling:
|
11 |
| -// No files, outputdir not writabel |
| 11 | +// No files, outputdir not writable |
12 | 12 |
|
13 | 13 | set_time_limit(0);
|
14 | 14 |
|
15 | 15 | // Make sure we have a timezone for date functions.
|
16 | 16 | if (ini_get('date.timezone') == '')
|
17 | 17 | date_default_timezone_set( Webgrind_Config::$defaultTimezone );
|
18 | 18 |
|
| 19 | +try { |
| 20 | + switch(get('op')){ |
| 21 | + case 'file_list': |
| 22 | + echo json_encode(Webgrind_FileHandler::getInstance()->getTraceList()); |
| 23 | + break; |
| 24 | + case 'function_list': |
| 25 | + $dataFile = get('dataFile'); |
| 26 | + if($dataFile=='0'){ |
| 27 | + $files = Webgrind_FileHandler::getInstance()->getTraceList(); |
| 28 | + $dataFile = $files[0]['filename']; |
| 29 | + } |
| 30 | + $reader = Webgrind_FileHandler::getInstance()->getTraceReader($dataFile, get('costFormat', Webgrind_Config::$defaultCostformat)); |
| 31 | + $functions = array(); |
| 32 | + $shownTotal = 0; |
| 33 | + $breakdown = array('internal' => 0, 'procedural' => 0, 'class' => 0, 'include' => 0); |
19 | 34 |
|
20 |
| -switch(get('op')){ |
21 |
| - case 'file_list': |
22 |
| - echo json_encode(Webgrind_FileHandler::getInstance()->getTraceList()); |
23 |
| - break; |
24 |
| - case 'function_list': |
25 |
| - $dataFile = get('dataFile'); |
26 |
| - if($dataFile=='0'){ |
27 |
| - $files = Webgrind_FileHandler::getInstance()->getTraceList(); |
28 |
| - $dataFile = $files[0]['filename']; |
29 |
| - } |
30 |
| - $reader = Webgrind_FileHandler::getInstance()->getTraceReader($dataFile, get('costFormat', Webgrind_Config::$defaultCostformat)); |
31 |
| - $functions = array(); |
32 |
| - $shownTotal = 0; |
33 |
| - $breakdown = array('internal' => 0, 'user' => 0, 'class' => 0, 'include' => 0); |
34 |
| - |
35 |
| - for($i=0;$i<$reader->getFunctionCount();$i++) { |
36 |
| - $functionInfo = $reader->getFunctionInfo($i); |
| 35 | + for($i=0;$i<$reader->getFunctionCount();$i++) { |
| 36 | + $functionInfo = $reader->getFunctionInfo($i); |
37 | 37 |
|
38 | 38 |
|
39 |
| - if (false !== strpos($functionInfo['functionName'], 'php::')) { |
40 |
| - $breakdown['internal'] += $functionInfo['summedSelfCost']; |
41 |
| - $humanKind = 'internal'; |
42 |
| - $kind = 'blue'; |
43 |
| - } elseif (false !== strpos($functionInfo['functionName'], 'require_once::') || |
44 |
| - false !== strpos($functionInfo['functionName'], 'require::') || |
45 |
| - false !== strpos($functionInfo['functionName'], 'include_once::') || |
46 |
| - false !== strpos($functionInfo['functionName'], 'include::')) { |
47 |
| - $breakdown['include'] += $functionInfo['summedSelfCost']; |
48 |
| - $humanKind = 'include'; |
49 |
| - $kind = 'grey'; |
50 |
| - } else { |
51 |
| - if (false !== strpos($functionInfo['functionName'], '->') || false !== strpos($functionInfo['functionName'], '::')) { |
52 |
| - $breakdown['class'] += $functionInfo['summedSelfCost']; |
53 |
| - $humanKind = 'class'; |
54 |
| - $kind = 'green'; |
55 |
| - } else { |
56 |
| - $breakdown['user'] += $functionInfo['summedSelfCost']; |
57 |
| - $humanKind = 'procedural'; |
58 |
| - $kind = 'orange'; |
59 |
| - } |
60 |
| - } |
61 |
| - if (!(int)get('hideInternals', 0) || strpos($functionInfo['functionName'], 'php::') === false) { |
62 |
| - $shownTotal += $functionInfo['summedSelfCost']; |
63 |
| - $functions[$i] = $functionInfo; |
64 |
| - $functions[$i]['nr'] = $i; |
65 |
| - $functions[$i]['kind'] = $kind; |
66 |
| - $functions[$i]['humanKind'] = $humanKind; |
67 |
| - } |
| 39 | + if (false !== strpos($functionInfo['functionName'], 'php::')) { |
| 40 | + $breakdown['internal'] += $functionInfo['summedSelfCost']; |
| 41 | + $humanKind = 'internal'; |
| 42 | + } elseif (false !== strpos($functionInfo['functionName'], 'require_once::') || |
| 43 | + false !== strpos($functionInfo['functionName'], 'require::') || |
| 44 | + false !== strpos($functionInfo['functionName'], 'include_once::') || |
| 45 | + false !== strpos($functionInfo['functionName'], 'include::')) { |
| 46 | + $breakdown['include'] += $functionInfo['summedSelfCost']; |
| 47 | + $humanKind = 'include'; |
| 48 | + } else { |
| 49 | + if (false !== strpos($functionInfo['functionName'], '->') || false !== strpos($functionInfo['functionName'], '::')) { |
| 50 | + $breakdown['class'] += $functionInfo['summedSelfCost']; |
| 51 | + $humanKind = 'class'; |
| 52 | + } else { |
| 53 | + $breakdown['procedural'] += $functionInfo['summedSelfCost']; |
| 54 | + $humanKind = 'procedural'; |
| 55 | + } |
| 56 | + } |
| 57 | + if (!(int)get('hideInternals', 0) || strpos($functionInfo['functionName'], 'php::') === false) { |
| 58 | + $shownTotal += $functionInfo['summedSelfCost']; |
| 59 | + $functions[$i] = $functionInfo; |
| 60 | + $functions[$i]['nr'] = $i; |
| 61 | + $functions[$i]['humanKind'] = $humanKind; |
| 62 | + } |
68 | 63 |
|
69 |
| - } |
70 |
| - usort($functions,'costCmp'); |
| 64 | + } |
| 65 | + usort($functions,'costCmp'); |
71 | 66 |
|
72 |
| - $remainingCost = $shownTotal*get('showFraction'); |
| 67 | + $remainingCost = $shownTotal*get('showFraction'); |
73 | 68 |
|
74 |
| - $result['functions'] = array(); |
75 |
| - foreach($functions as $function){ |
| 69 | + $result['functions'] = array(); |
| 70 | + foreach($functions as $function){ |
76 | 71 |
|
77 |
| - $remainingCost -= $function['summedSelfCost']; |
| 72 | + $remainingCost -= $function['summedSelfCost']; |
78 | 73 | $function['file'] = urlencode($function['file']);
|
79 |
| - $result['functions'][] = $function; |
80 |
| - if($remainingCost<0) |
81 |
| - break; |
82 |
| - } |
83 |
| - $result['summedInvocationCount'] = $reader->getFunctionCount(); |
84 |
| - $result['summedRunTime'] = $reader->formatCost($reader->getHeader('summary'), 'msec'); |
85 |
| - $result['dataFile'] = $dataFile; |
86 |
| - $result['invokeUrl'] = $reader->getHeader('cmd'); |
87 |
| - $result['runs'] = $reader->getHeader('runs'); |
88 |
| - $result['breakdown'] = $breakdown; |
89 |
| - $result['mtime'] = date(Webgrind_Config::$dateFormat,filemtime(Webgrind_Config::xdebugOutputDir().$dataFile)); |
90 |
| - echo json_encode($result); |
91 |
| - break; |
92 |
| - case 'callinfo_list': |
93 |
| - $reader = Webgrind_FileHandler::getInstance()->getTraceReader(get('file'), get('costFormat', Webgrind_Config::$defaultCostformat)); |
94 |
| - $functionNr = get('functionNr'); |
95 |
| - $function = $reader->getFunctionInfo($functionNr); |
| 74 | + $result['functions'][] = $function; |
| 75 | + if($remainingCost<0) |
| 76 | + break; |
| 77 | + } |
| 78 | + $result['summedInvocationCount'] = $reader->getFunctionCount(); |
| 79 | + $result['summedRunTime'] = $reader->formatCost($reader->getHeader('summary'), 'msec'); |
| 80 | + $result['dataFile'] = $dataFile; |
| 81 | + $result['invokeUrl'] = $reader->getHeader('cmd'); |
| 82 | + $result['runs'] = $reader->getHeader('runs'); |
| 83 | + $result['breakdown'] = $breakdown; |
| 84 | + $result['mtime'] = date(Webgrind_Config::$dateFormat,filemtime(Webgrind_Config::xdebugOutputDir().$dataFile)); |
| 85 | + echo json_encode($result); |
| 86 | + break; |
| 87 | + case 'callinfo_list': |
| 88 | + $reader = Webgrind_FileHandler::getInstance()->getTraceReader(get('file'), get('costFormat', Webgrind_Config::$defaultCostformat)); |
| 89 | + $functionNr = get('functionNr'); |
| 90 | + $function = $reader->getFunctionInfo($functionNr); |
96 | 91 |
|
97 | 92 | $result = array('calledFrom'=>array(), 'subCalls'=>array());
|
98 | 93 | $foundInvocations = 0;
|
|
106 | 101 | }
|
107 | 102 | $result['calledByHost'] = ($foundInvocations<$function['invocationCount']);
|
108 | 103 |
|
109 |
| - for($i=0;$i<$function['subCallInfoCount'];$i++){ |
| 104 | + for($i=0;$i<$function['subCallInfoCount'];$i++){ |
110 | 105 | $invo = $reader->getSubCallInfo($functionNr, $i);
|
111 | 106 | $callInfo = $reader->getFunctionInfo($invo['functionNr']);
|
112 | 107 | $invo['file'] = urlencode($function['file']); // Sub call to $callInfo['file'] but from $function['file']
|
113 | 108 | $invo['callerFunctionName'] = $callInfo['functionName'];
|
114 | 109 | $result['subCalls'][] = $invo;
|
115 | 110 | }
|
116 |
| - echo json_encode($result); |
117 |
| - |
118 |
| - break; |
119 |
| - case 'fileviewer': |
120 |
| - $file = get('file'); |
121 |
| - $line = get('line'); |
| 111 | + echo json_encode($result); |
| 112 | + break; |
| 113 | + case 'fileviewer': |
| 114 | + $file = get('file'); |
| 115 | + $line = get('line'); |
122 | 116 |
|
123 |
| - if($file && $file!=''){ |
124 |
| - $message = ''; |
125 |
| - if(!file_exists($file)){ |
126 |
| - $message = $file.' does not exist.'; |
127 |
| - } else if(!is_readable($file)){ |
128 |
| - $message = $file.' is not readable.'; |
129 |
| - } else if(is_dir($file)){ |
130 |
| - $message = $file.' is a directory.'; |
131 |
| - } |
132 |
| - } else { |
133 |
| - $message = 'No file to view'; |
134 |
| - } |
135 |
| - require 'templates/fileviewer.phtml'; |
| 117 | + if($file && $file!=''){ |
| 118 | + $message = ''; |
| 119 | + if(!file_exists($file)){ |
| 120 | + $message = $file.' does not exist.'; |
| 121 | + } else if(!is_readable($file)){ |
| 122 | + $message = $file.' is not readable.'; |
| 123 | + } else if(is_dir($file)){ |
| 124 | + $message = $file.' is a directory.'; |
| 125 | + } |
| 126 | + } else { |
| 127 | + $message = 'No file to view'; |
| 128 | + } |
| 129 | + require 'templates/fileviewer.phtml'; |
136 | 130 |
|
137 |
| - break; |
138 |
| - case 'version_info': |
139 |
| - $response = @file_get_contents('http://jokke.dk/webgrindupdate.json?version='.Webgrind_Config::$webgrindVersion); |
140 |
| - echo $response; |
141 |
| - break; |
142 |
| - default: |
143 |
| - require 'templates/index.phtml'; |
| 131 | + break; |
| 132 | + case 'version_info': |
| 133 | + $response = @file_get_contents('http://jokke.dk/webgrindupdate.json?version='.Webgrind_Config::$webgrindVersion); |
| 134 | + echo $response; |
| 135 | + break; |
| 136 | + default: |
| 137 | + $welcome = ''; |
| 138 | + if (!file_exists(Webgrind_Config::storageDir()) || !is_writable(Webgrind_Config::storageDir())) { |
| 139 | + $welcome .= 'Webgrind $storageDir does not exist or is not writeable: "'.Webgrind_Config::storageDir().'"<br>'; |
| 140 | + } |
| 141 | + if (!file_exists(Webgrind_Config::xdebugOutputDir()) || !is_readable(Webgrind_Config::xdebugOutputDir())) { |
| 142 | + $welcome .= 'Webgrind $profilerDir does not exist or is not readable: "'.Webgrind_Config::xdebugOutputDir().'"<br>'; |
| 143 | + } |
| 144 | + |
| 145 | + if ($welcome == '') { |
| 146 | + $welcome = 'Select a cachegrind file above'; |
| 147 | + } |
| 148 | + require 'templates/index.phtml'; |
| 149 | + } |
| 150 | +} catch (Exception $e) { |
| 151 | + echo json_encode(array('error' => $e->getMessage().'<br>'.$e->getFile().', line '.$e->getLine())); |
| 152 | + return; |
144 | 153 | }
|
145 | 154 |
|
146 |
| - |
147 | 155 | function get($param, $default=false){
|
148 | 156 | return (isset($_GET[$param])? $_GET[$param] : $default);
|
149 | 157 | }
|
|
0 commit comments