MkFramework
 All Data Structures Functions
plugin_debug.php
1 <?php
2 /*
3 This file is part of Mkframework.
4 
5 Mkframework is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License.
8 
9 Mkframework is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public License
15 along with Mkframework. If not, see <http://www.gnu.org/licenses/>.
16 
17 */
24 
25  private $iStartMicrotime;
26  private $sHtml;
27 
28  private static $tSpy;
29  private static $tTime;
30  private static $tTimeById;
31 
32  public function __construct($sMicrotime){
33 
34  $this->iStartMicrotime=self::microtime($sMicrotime);
35 
36  $iEndTime=self::microtime();
37  self::$tTime[]=array('End',$iEndTime);
38 
39  $iDiff=($iEndTime-$this->iStartMicrotime);
40 
41  $this->add('Time',sprintf('%0.3f',$iDiff).'s');
42 
43  $this->addComplexTimes('times',self::$tTime);
44 
45  $this->addComplex('$_GET',print_r($_GET,1));
46 
47  if(isset($_POST)){
48  $this->addComplex('$_POST',print_r($_POST,1));
49  }
50 
51  if(isset($_SESSION)){
52  $this->addComplex('$_SESSION',print_r($_SESSION,1));
53  }
54 
55  if(isset($_SERVER)){
56  $this->addComplex('$_SERVER',print_r($_SERVER,1));
57  }
58 
59  $oRequest=_root::getRequest();
60 
61  $this->add('Module',$oRequest->getModule());
62  $this->add('Action',$oRequest->getAction());
63 
64  $oFileLog=new _file(_root::getConfigVar('path.log','data/log/').date('Y-m-d').'_log.csv');
65  if($oFileLog->exist()){
66  $oFileLog->load();
67  $sContentLog=$oFileLog->getContent();
68  $this->addFileLog('File log',$sContentLog);
69  }
70 
71  $sVarIniConfig=_root::getConfigVar('model.ini.var','db');
72  $tClassSgbd=_root::getConfigVar($sVarIniConfig);
73  $this->addComplexIni('Connexions',array($sVarIniConfig=>$tClassSgbd));
74 
75  $tConfigSection=array(
76  'path' ,
77  'cache' ,
78  'language',
79  'auth',
80  'acl',
81  'navigation',
82  'urlrewriting',
83  'security',
84  'log',
85  'check',
86  'path',
87  'model',
88  );
89  $tConfig=array();
90  foreach($tConfigSection as $sSection){
91  $tConfig[$sSection]=_root::getConfigVar($sSection);
92  }
93 
94  $this->addComplexIni('Config',$tConfig);
95 
96  if(self::$tSpy){
97  $this->addComplexSpy('Spy variables',self::$tSpy);
98  }
99 
100  $this->addAcl();
101  }
102 
109  public static function addSpy($uLabel,$uVar){
110  self::$tSpy[][$uLabel]=$uVar;
111  }
117  public static function addChrono($uLabel){
118  $iTime=self::microtime();
119  self::$tTime[]=array($uLabel,$iTime);
120  }
126  public static function startChrono($uLabel){
127  $iTime=self::microtime();
128  self::$tTimeById[$uLabel]['start']=$iTime;
129  }
135  public static function stopChrono($uLabel){
136  $iTime=self::microtime();
137  self::$tTimeById[$uLabel]['end']=$iTime;
138  }
139 
140 
141 
142 
143  public function display(){
144  echo '<script>
145  var activePopup=\'\';
146  function openPopupDebug(id){
147  closePopup();
148  var a=getById(id);
149  if(a){
150  a.style.display="block";
151  activePopup=id;
152  }
153  }
154  function closePopup(){
155  if(activePopup){
156  var b=getById(activePopup);
157  if(b){
158  b.style.display="none";
159  }
160  }
161  }
162  function showHideDebugBar(){
163  var a=getById(\'debugBar\');
164  if(a){
165  if(a.style.display==\'none\'){
166  a.style.display=\'block\';
167  var b=getById(\'debugBtn\');
168  if(b){
169  b.style.width=\'100%\';
170  }
171  }else{
172  a.style.display=\'none\';
173  var b=getById(\'debugBtn\');
174  if(b){
175  b.style.width=\'80px\';
176  }
177  }
178  }
179  }
180  </script>';
181  echo '<div id="debugBtn" ';
182  echo 'style="position:fixed;border:2px solid #444;background:#ddd;bottom:0px;left:0px;width:100%">';
183  echo '<div style="float:left"><input type="button" value="Masquer" onclick="showHideDebugBar()"/></div>';
184  echo '<div id="debugBar" style="width:100%">';
185  echo $this->sHtml;
186  echo '</div>';
187  echo '</div>';
188  }
189 
190  private function addComplex($key,$value){
191  $this->addHtml('<input type="button" value="'.$key.'" onclick="openPopupDebug(\'popupDebug'.$key.'\')" />');
192  $this->addSep();
193 
194  $this->addPopupPrintr($key,$value);
195  }
196  private function addComplexTimes($key,$value){
197  $this->addHtml('<input type="button" value="'.$key.'" onclick="openPopupDebug(\'popupDebug'.$key.'\')" />');
198  $this->addSep();
199 
200  $value=$this->parseTime($value);
201 
202  $this->addPopup($key,$value);
203  }
204  private function addComplexIni($key,$value){
205  $this->addHtml('<input type="button" value="'.$key.'" onclick="openPopupDebug(\'popupDebug'.$key.'\')" />');
206  $this->addSep();
207 
208  $value=$this->parseIni($value);
209 
210  $this->addPopup($key,$value);
211  }
212  private function addComplexSpy($key,$value){
213  $this->addHtml('<input type="button" value="'.$key.'" onclick="openPopupDebug(\'popupDebug'.$key.'\')" />');
214  $this->addSep();
215 
216  $sValue=$this->parseSpy($value);
217 
218  $this->addPopup($key,$sValue);
219  }
220 
221  private function addFileLog($key,$value){
222  $this->addHtml('<input type="button" value="'.$key.'" onclick="openPopupDebug(\'popupDebug'.$key.'\')" />');
223  $this->addSep();
224 
225  $value=$this->parseLog($value);
226 
227  $this->addPopup($key,$value);
228  }
229 
230  private function add($key,$value){
231  $this->addHtml('<strong>'.$key.'</strong>:<span style="padding:2px 4px;background:#fff">'.$value.'</span>');
232  $this->addSep();
233  }
234 
235  private function addPopupPrintr($key,$value){
236  $this->addHtml(
237  '<div id="popupDebug'.$key.'"
238  style="display:none;position:absolute;left:0px;bottom:0px;border:2px solid gray;background:white">
239  <p style="text-align:right;background:#ccc;margin:0px;"><a href="#" onclick="closePopup()">Fermer</a></p>
240  <div style="height:350px;width:400px;overflow:auto;padding:10px;">
241  <pre>'.customHtmlentities(print_r($value,1)).'</pre>
242  </div>
243  </div>');
244  }
245 
246  private function addPopup($key,$value,$width=800){
247  $this->addHtml(
248  '<div id="popupDebug'.$key.'"
249  style="display:none;position:absolute;left:0px;bottom:0px;border:2px solid gray;background:white">
250  <p style="text-align:right;background:#ccc;margin:0px;"><a href="#" onclick="closePopup()">Fermer</a></p>
251  <div style="height:350px;width:'.$width.'px;overflow:auto;padding:10px;">
252  '.$value.'
253  </div>
254  </div>');
255  }
256 
257  private function addAcl(){
258  $this->addHtml('<input type="button" value="Permissions" onclick="openPopupDebug(\'popupDebugACL\')" />');
259  $this->addSep();
260 
261  $sHtml='<div style="position:absolute">';
262  $sHtml.='<table style="background:white;border-collapse:collapse">
263  <tr>
264  <th style="border:1px solid gray;">Action</th>
265  <th style="border:1px solid gray;">Component</th>
266  </tr>';
267  $tab=_root::getConfigVar('tAskCan');
268  if($tab and is_array($tab)):
269  foreach($tab as $tVal):
270  $sHtml.='<tr>
271  <td style="border:1px solid gray;';
272  if($tVal[2]){
273  $sHtml.='color:green;';
274  }else{
275  $sHtml.='color:red';
276  }
277  $sHtml.='">'.$tVal[0].'</td>
278  <td style="border:1px solid gray;';
279  if($tVal[2]){
280  $sHtml.='color:green;';
281  }else{
282  $sHtml.='color:red';
283  }
284  $sHtml.='">'.$tVal[1].'</td>
285 
286  </tr>';
287  endforeach;
288  endif;
289  $sHtml.='</table>';
290 
291  $this->addPopup('ACL', $sHtml,300);
292  }
293 
294  private function addSep(){
295  $this->addHtml('&nbsp;&nbsp;&nbsp;&nbsp;');
296  }
297 
298  private function addHtml($sHtml){
299  $this->sHtml.=$sHtml;
300  }
301 
302  private function parseLog($value){
303  $sep=' | ';
304 
305  $tLine=explode("\n",$value);
306  $sHtml=null;
307 
308  $iMax=count($tLine)-1;
309  for($i=$iMax;$i>0;$i--){
310  $sLine=$tLine[$i];
311 
312  $tCase=explode(';',$sLine,4);
313  $sDate=null;
314  if(isset($tCase[0])){
315  $sDate=$tCase[0];
316  }
317  $sTime=null;
318  if(isset($tCase[1])){
319  $sTime=$tCase[1];
320  }
321  $sType=null;
322  if(isset($tCase[2])){
323  $sType=$tCase[2];
324  }
325  $sLog=null;
326  if(isset($tCase[3])){
327  $sLog=$tCase[3];
328  }
329 
330  if($sDate==null){
331  continue;
332  }
333 
334  $sHtml.='<p style="border-bottom:1px dotted gray">';
335 
336  $sHtml.='<span >'.$sDate.'</span> ';
337  $sHtml.='<span style="font-weight:bold">'.$sTime.'</span>';
338 
339  $sHtml.=$sep;
340 
341  $sHtml.='<span style="color:';
342  if($sType=='info'){ $sHtml.='gray';}
343  elseif($sType=='log'){ $sHtml.='darkblue';}
344  $sHtml.='">'.$sType.'</span>';
345 
346  $sHtml.=$sep;
347 
348  $sHtml.=$sLog;
349 
350  $sHtml.='</p>';
351 
352  if(preg_match('/module a appeler/',$sLog)){
353  $sHtml.='<p>&nbsp;</p>';
354  }
355 
356  }
357  return $sHtml;
358  }
359 
360  private function parseSpy($tValue){
361  $sHtml=null;
362  foreach($tValue as $tDetail){
363  foreach($tDetail as $ref => $value){
364  $sHtml.='<h2 style="border-bottom:1px solid black">'.$ref.'</h2>';
365  $sHtml.='<p><pre>'.customHtmlentities(print_r($value,1)).'</pre></p>';
366  }
367  }
368 
369  return $sHtml;
370  }
371 
372  private function parseIni($tValue){
373  $sHtml=null;
374  foreach($tValue as $sSection => $tDetail){
375  $sHtml.='<h2 style="border-bottom:1px solid black">'.$sSection.'</h2>';
376  foreach($tDetail as $sKey => $sValue){
377  $sHtml.='<p style="margin:0px;margin-left:10px;">';
378  $sHtml.='<strong>'.$sKey.'</strong> = <span style="color:darkgreen"> '.$sValue.'</span>';
379  $sHtml.='</p>';
380  }
381  }
382 
383  return $sHtml;
384  }
385  private function parseTime($tValue){
386  $sHtml=null;
387  $iPreviousTime=$this->iStartMicrotime;
388  $sPreviousStep='Start';
389  foreach($tValue as $tDetail){
390  list($sLabel,$iTime)=$tDetail;
391  $iDelta=($iTime-$iPreviousTime);
392  $sHtml.='<p><strong>'.$sPreviousStep.' &gt;&gt; '.$sLabel.'</strong> : '.sprintf('%0.3f',$iDelta).'s</p>';
393 
394  $iPreviousTime=$iTime;
395  $sPreviousStep=$sLabel;
396  }
397 
398  $sHtml.='<p style="border-top:1px solid gray">';
399  $sHtml.='<strong>Total</strong> '.sprintf('%0.3f',($iTime-$this->iStartMicrotime)).'s';
400  $sHtml.='</p>';
401 
402  if(self::$tTimeById){
403 
404  $sHtml.='<p>&nbsp;</p>';
405 
406  foreach(self::$tTimeById as $sLabel => $tValue){
407 
408  if(isset($tValue['end']) and isset($tValue['start'])){
409  $iDelta=($tValue['end']-$tValue['start']);
410 
411  $sHtml.='<p><strong>'.$sLabel.' </strong> : '.sprintf('%0.3f',$iDelta).'s</p>';
412  }else{
413  $sHtml.='<p><strong>'.$sLabel.' </strong> : <span style="color:red">';
414  $sHtml.=' Erreur il manque startChrono ou stopChrono</span></p>';
415  }
416 
417  }
418 
419  }
420 
421  return $sHtml;
422  }
423 
424  public static function microtime($sMicrotime=null){
425  if($sMicrotime==null){ $sMicrotime=microtime(); }
426  $tMicrotime = explode(" ", $sMicrotime);
427  return ((float)$tMicrotime[0] + (float)$tMicrotime[1]);
428  }
429 
430 }
static getConfigVar($sCatAndVar, $uDefaut=null)
Definition: class_root.php:654
static addChrono($uLabel)
static getRequest()
Definition: class_root.php:541
static stopChrono($uLabel)
static startChrono($uLabel)
static addSpy($uLabel, $uVar)