PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/vendor/firephp.php

https://github.com/rodrigodk/KO3_FirePHP
PHP | 485 lines | 386 code | 47 blank | 52 comment | 43 complexity | fc08fe1c13dc4b984a9bfd06ded3ad87 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * KO3 FirePHP and Profiler
  4. * Version 0.3
  5. * Last changed: 2010-11-8
  6. * Based on:
  7. * Fire_Profiler by dlib: http://learn.kohanaphp.com/2008/07/21/introducing-fire-profiler/
  8. * KO3 conversion by ralf: http://code.goldenzazu.de/fireprofiler.php
  9. */
  10. // Grab the vendor api library
  11. require Kohana::find_file('vendor', 'FirePHP/FirePHP', $ext = 'class.php');
  12. /**
  13. * Main Class
  14. */
  15. class Vendor_FirePHP extends FirePHP {
  16. protected $_config = array();
  17. protected $_queries = array();
  18. protected $_authorized = NULL;
  19. public function __construct(array $config=Null)
  20. {
  21. //parent::__construct();
  22. // just in case we're referenced while initializing...
  23. self::$instance = $this;
  24. $this->_init($config);
  25. }
  26. // Shouldn't need more than one instance of this...
  27. final private function __clone() {}
  28. protected function _init(array $config=Null)
  29. {
  30. // disable while initializing...
  31. $this->enabled = false;
  32. $this->_config = Kohana::config('firephp.default');
  33. $this->_config = (isset($config)) ? Arr::merge($this->_config, $config) : $this->_config;
  34. $this->setOptions($this->get_config('firephp', $this->getOptions()));
  35. $this->enabled = $this->get_enabled();
  36. $this->info('FirePHP Initialized...');
  37. }
  38. public function get_config($key, $default = NULL)
  39. {
  40. return Arr::path($this->_config, $key, $default);
  41. }
  42. public function set_config($key, $value=NULL)
  43. {
  44. // only configure when enabled...
  45. if ($this->enabled)
  46. {
  47. if ( ! empty($key))
  48. {
  49. if (is_array($key)) {
  50. $value = $key;
  51. } else {
  52. // Convert dot-noted key string to an array
  53. $keys = explode('.', rtrim($key, '.'));
  54. // This will set a value even if it didn't previously exist
  55. do {
  56. $key = array_pop($keys);
  57. $value = array($key => $value);
  58. } while ($keys);
  59. }
  60. $this->_config = Arr::merge($this->_config, $value);
  61. $this->setOptions($this->get_config('firephp', $this->getOptions()));
  62. $this->enabled = $this->get_enabled();
  63. }
  64. }
  65. return $this;
  66. }
  67. // Override parent method...
  68. public function setEnabled($Enabled)
  69. {
  70. $this->set_enabled($Enabled);
  71. }
  72. public function set_enabled(bool $enabled)
  73. {
  74. return $this->set_config('enabled', $enabled);
  75. }
  76. // Override parent method...
  77. public function getEnabled() {
  78. return $this->get_enabled();
  79. }
  80. public function get_enabled()
  81. {
  82. return ($this->is_authorized() AND $this->get_config('enabled', FALSE));
  83. }
  84. public function is_authorized()
  85. {
  86. if (isset($this->_authorized)) return $this->_authorized;
  87. // Return TRUE if Auth is disabled
  88. if ($this->get_config('auth.enabled', FALSE) === FALSE)
  89. return $this->_authorized = TRUE;
  90. // Check to see if the Auth class is available
  91. if (class_exists('Auth'))
  92. {
  93. $logged_in = $this->get_config('auth.logged_in', TRUE);
  94. if (($logged_in === TRUE) AND Auth::instance()->logged_in())
  95. return $this->_authorized = TRUE;
  96. if (is_array($logged_in))
  97. {
  98. foreach($logged_in as $role => $val)
  99. {
  100. if (($val === TRUE) AND Auth::instance()->logged_in($role))
  101. return $this->_authorized = TRUE;
  102. }
  103. }
  104. }
  105. // Not Authorized...
  106. return $this->_authorized = FALSE;
  107. }
  108. // Disable error and exception handling... KO3 does it for us...
  109. public function registerErrorHandler($throwErrorExceptions=true) {}
  110. public function registerExceptionHandler() {}
  111. // Disable assertion handler until I can verify it doesn't break anything...
  112. public function registerAssertionHandler($convertAssertionErrorsToExceptions=true, $throwAssertionExceptions=false) {}
  113. public function phpversion()
  114. {
  115. return $this->info(phpversion(), 'Current PHP version: ');
  116. }
  117. public function superglobals()
  118. {
  119. return $this->instance()
  120. ->group('PHP Superglobals')
  121. ->server()
  122. ->get()
  123. ->files()
  124. ->post()
  125. ->cookie()
  126. ->session()
  127. ->request()
  128. ->env()
  129. ->groupEnd();
  130. }
  131. public function server()
  132. {
  133. return (isset($_SERVER)) ? $this->variable_table($_SERVER, '$_SERVER') : $this->info('[empty]', '$_SERVER');
  134. }
  135. public function get()
  136. {
  137. return (isset($_GET)) ? $this->variable_table($_GET, '$_GET') : $this->info('[empty]', '$_GET');
  138. }
  139. public function files()
  140. {
  141. return (isset($_FILES)) ? $this->variable_table($_FILES, '$_FILES') : $this->info('[empty]', '$_FILES');
  142. }
  143. public function post()
  144. {
  145. // check for the validation object...
  146. if (is_object($_POST)) {
  147. $this->info($_POST, '$_POST');
  148. }
  149. return (isset($_POST)) ? $this->variable_table((array)$_POST, '$_POST') : $this->info('[empty]', '$_POST');
  150. }
  151. public function cookie()
  152. {
  153. return (isset($_COOKIE)) ? $this->variable_table($_COOKIE, '$_COOKIE') : $this->info('[empty]', '$_COOKIE');
  154. }
  155. public function session()
  156. {
  157. return (isset($_SESSION)) ? $this->variable_table($_SESSION, '$_SESSION') : $this->info('[empty]', '$_SESSION');
  158. }
  159. public function request()
  160. {
  161. return (isset($_REQUEST)) ? $this->variable_table($_REQUEST, '$_REQUEST') : $this->info('[empty]', '$_REQUEST');
  162. }
  163. public function env()
  164. {
  165. return (isset($_ENV)) ? $this->variable_table($_ENV, '$_ENV') : $this->info('[empty]', '$_ENV');
  166. }
  167. /**
  168. * Benchmark times and memory usage from the Benchmark library.
  169. */
  170. public function database()
  171. {
  172. if ($this->enabled)
  173. {
  174. // Check to see if the Database class is available
  175. if (class_exists('Database'))
  176. {
  177. if ( ! empty($this->_queries))
  178. {
  179. $this->group(__('Database').' '. __('Queries').': ('.count($this->_queries).')');
  180. foreach ($this->_queries as $query)
  181. {
  182. if ($query['fb'] == 'table')
  183. {
  184. $this->multicolumn_table($query['data'], $query['sql']);
  185. } else {
  186. $this->log($query['sql'], $query['label']);
  187. }
  188. }
  189. $this->groupEnd();
  190. }
  191. $this->benchmark('Database');
  192. }
  193. }
  194. return $this;
  195. }
  196. protected function _store_query(array $query)
  197. {
  198. if (empty($query)) return false; // throw exception?
  199. if ($this->get_config('database.group', FALSE))
  200. {
  201. $this->_queries[] = $query;
  202. }
  203. else
  204. {
  205. if ($query['fb'] == 'log')
  206. {
  207. $this->log($query['sql'], $query['label']);
  208. } else {
  209. $this->multicolumn_table($query['data'], $query['sql']);
  210. }
  211. }
  212. }
  213. /**
  214. * @param result object Database_Result for SELECT queries
  215. * @param result mixed the insert id for INSERT queries
  216. * @param result integer number of affected rows for all other queries
  217. */
  218. public function query($result, $type, $sql)
  219. {
  220. $store = array('fb' => 'log', 'sql' => $sql);
  221. $max = $this->get_config('database.rows', 10);
  222. if ($type === Database::SELECT)
  223. {
  224. if ($this->get_config('database.select', FALSE))
  225. {
  226. $rows = $result->as_array();
  227. $result->rewind();
  228. if (count($rows) > 0)
  229. {
  230. $store['fb'] = 'table';
  231. $store['data'] = array_slice($rows, 0, $max);
  232. }
  233. else
  234. $store['label'] = count($rows).' '.__('rows');
  235. }
  236. }
  237. elseif ($type === Database::INSERT)
  238. {
  239. if ($this->get_config('database.insert', FALSE))
  240. {
  241. if (count($result) > 0)
  242. {
  243. $store['fb'] = 'table';
  244. $store['data'] = array_slice($result, 0, $max);
  245. }
  246. else
  247. $store['label'] = count($result).' '.__('rows');
  248. }
  249. }
  250. elseif ($type === Database::UPDATE)
  251. {
  252. if ($this->get_config('database.update', FALSE))
  253. {
  254. $store['label'] = $result.' '.__('rows');
  255. }
  256. }
  257. else
  258. {
  259. if ($this->get_config('database.other', FALSE))
  260. {
  261. $store['label'] = $result.' '.__('rows');
  262. }
  263. }
  264. $this->_store_query($store);
  265. }
  266. public function benchmark($table = FALSE)
  267. {
  268. if ($this->enabled)
  269. {
  270. foreach (Profiler::groups() as $group => $benchmarks)
  271. {
  272. $tablename = ucfirst($group);
  273. // Exclude database unless specifically run
  274. if ((empty($table) AND strpos($tablename,'Database') === FALSE) OR strpos($tablename,$table) === 0)
  275. {
  276. $row = array( array(__('Benchmark'),__('Min'),__('Max'), __('Average'),__('Total')) );
  277. foreach ($benchmarks as $name => $tokens)
  278. {
  279. $stats = Profiler::stats($tokens);
  280. $cell = array( $name.' (' . count($tokens).')' );
  281. foreach (array('min', 'max', 'average', 'total') as $key)
  282. {
  283. $cell[] = ' ' . number_format($stats[$key]['time'], 6). ' '. __('seconds') ;
  284. }
  285. $row[] = $cell;
  286. }
  287. $cell = array('');
  288. foreach (array('min', 'max', 'average', 'total') as $key)
  289. {
  290. $cell[] = ' ' . number_format($stats[$key]['memory'] / 1024, 4) . ' kb';
  291. }
  292. $row[] = $cell;
  293. // Translate before passing...
  294. $this->fb(array(__($tablename), $row ),FirePHP::TABLE);
  295. }
  296. }
  297. if (empty($table) || strpos('Application',$table) === 0)
  298. {
  299. $stats = Profiler::application();
  300. $tablename = array(__('Application Execution').' ('.$stats['count'].')');
  301. $row = array(array('','min', 'max', 'average', 'current'));
  302. $cell = array('Time');
  303. foreach (array('min', 'max', 'average', 'current') as $key)
  304. {
  305. $cell[] = number_format($stats[$key]['time'], 6) . ' ' . __('seconds');
  306. }
  307. $row[] = $cell;
  308. $cell = array('Memory');
  309. foreach (array('min', 'max', 'average', 'current') as $key)
  310. {
  311. $cell[] = number_format($stats[$key]['memory'] / 1024, 4) . ' kb';
  312. }
  313. $row[] = $cell;
  314. $this->fb(array($tablename, $row ),FirePHP::TABLE);
  315. }
  316. }
  317. return $this;
  318. }
  319. /**
  320. * Creats a Key = Value Variable Style Table
  321. * @param array $data
  322. * @param <type> $label
  323. * @param <type> $count
  324. * @return <type> $this
  325. */
  326. public function variable_table(array $data, $label='', $count=TRUE)
  327. {
  328. if ($this->enabled)
  329. {
  330. if (empty($data)) return $this->info('[empty]', $label);
  331. $table = array();
  332. $table[] = array(__('Key'), __('Value'));
  333. foreach($data as $key => $value)
  334. {
  335. if (is_object($value))
  336. {
  337. $value = ($this->get_config('tables.show_objects', FALSE))
  338. ? $value
  339. : get_class($value).' [object]';
  340. }
  341. $table[] = array($key, $value);
  342. }
  343. $label = ($count) ? $label.' ('.count($data).') ' : $label;
  344. $this->table($label, $table);
  345. }
  346. return $this;
  347. }
  348. /**
  349. * Creates a table with column headers from an associative array
  350. * @param array $data
  351. * @param string $label
  352. * @param bool $count (default TRUE) Counts the rows
  353. * @param bool $row_num (default TRUE) Numbers the rows
  354. */
  355. public function multicolumn_table(array $data, $label='', $count=TRUE, $row_num=TRUE)
  356. {
  357. if ($this->enabled)
  358. {
  359. if (empty($data)) return $this->info('[empty]', $label);
  360. $table = array();
  361. foreach ($data as $num => $row)
  362. {
  363. if ($row_num)
  364. {
  365. if ($num === 0) $table[$num]['row_num'] = '';
  366. $table[$num+1]['row_num'] = $num+1;
  367. }
  368. foreach ($row as $field => $value)
  369. {
  370. if ($num === 0)
  371. {
  372. $table[$num][$field] = $field; // header
  373. }
  374. if (is_object($value))
  375. {
  376. $value = ($this->get_config('tables.show_objects', FALSE))
  377. ? $value
  378. : get_class($value).' [object]';
  379. }
  380. $table[$num+1][$field] = $value;
  381. }
  382. }
  383. $label = ($count) ? $label.' ('.count($data).') ' : $label;
  384. $this->table($label, $table);
  385. }
  386. }
  387. public function dump($Key, $Variable, $Options = array())
  388. {
  389. parent::dump($Key, $Variable, $Options);
  390. return $this;
  391. }
  392. public function error($Object, $Label = null, $Options = array())
  393. {
  394. parent::error($Object, $Label, $Options);
  395. return $this;
  396. }
  397. public function group($Name, $Options=null)
  398. {
  399. parent::group($Name, $Options) ;
  400. return $this;
  401. }
  402. public function groupEnd()
  403. {
  404. parent::groupEnd();
  405. return $this;
  406. }
  407. public function info($Object, $Label = null, $Options = array())
  408. {
  409. parent::info($Object, $Label, $Options);
  410. return $this;
  411. }
  412. public function log($Object, $Label = null, $Options = array())
  413. {
  414. parent::log($Object, $Label, $Options);
  415. return $this;
  416. }
  417. public function table($Label, $Table, $Options = array())
  418. {
  419. parent::table($Label, $Table, $Options);
  420. return $this;
  421. }
  422. public function trace($Label)
  423. {
  424. parent::trace($Label);
  425. return $this;
  426. }
  427. public function warn($Object, $Label = null, $Options = array())
  428. {
  429. parent::warn($Object, $Label, $Options);
  430. return $this;
  431. }
  432. public static function instance($_config=Null)
  433. {
  434. return (self::$instance)
  435. ? self::$instance->set_config($_config)
  436. : new self($_config);
  437. }
  438. }