profiler.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php namespace Laravel\Profiling;
  2. use Laravel\View;
  3. use Laravel\File;
  4. use Laravel\Event;
  5. use Laravel\Config;
  6. use Laravel\Request;
  7. use Laravel\Database;
  8. class Profiler {
  9. /**
  10. * An array of the recorded Profiler data.
  11. *
  12. * @var array
  13. */
  14. protected static $data = array('queries' => array(), 'logs' => array());
  15. /**
  16. * Get the rendered contents of the Profiler.
  17. *
  18. * @param Response $response
  19. * @return string
  20. */
  21. public static function render($response)
  22. {
  23. // We only want to send the profiler toolbar if the request is not an AJAX
  24. // request, as sending it on AJAX requests could mess up JSON driven API
  25. // type applications, so we will not send anything in those scenarios.
  26. if ( ! Request::ajax())
  27. {
  28. static::$data['memory'] = get_file_size(memory_get_usage(true));
  29. static::$data['memory_peak'] = get_file_size(memory_get_peak_usage(true));
  30. static::$data['time'] = number_format((microtime(true) - LARAVEL_START) * 1000, 2);
  31. return render('path: '.__DIR__.'/template'.BLADE_EXT, static::$data);
  32. }
  33. }
  34. /**
  35. * Add a log entry to the log entries array.
  36. *
  37. * @param string $type
  38. * @param string $message
  39. * @return void
  40. */
  41. public static function log($type, $message)
  42. {
  43. static::$data['logs'][] = array($type, $message);
  44. }
  45. /**
  46. * Add a performed SQL query to the Profiler.
  47. *
  48. * @param string $sql
  49. * @param array $bindings
  50. * @param float $time
  51. * @return void
  52. */
  53. public static function query($sql, $bindings, $time)
  54. {
  55. foreach ($bindings as $binding)
  56. {
  57. $binding = Database::connection()->pdo->quote($binding);
  58. $sql = preg_replace('/\?/', $binding, $sql, 1);
  59. }
  60. static::$data['queries'][] = array($sql, $time);
  61. }
  62. /**
  63. * Attach the Profiler's event listeners.
  64. *
  65. * @return void
  66. */
  67. public static function attach()
  68. {
  69. // First we'll attach to the query and log events. These allow us to catch
  70. // all of the SQL queries and log messages that come through Laravel,
  71. // and we will pass them onto the Profiler for simple storage.
  72. Event::listen('laravel.log', function($type, $message)
  73. {
  74. Profiler::log($type, $message);
  75. });
  76. Event::listen('laravel.query', function($sql, $bindings, $time)
  77. {
  78. Profiler::query($sql, $bindings, $time);
  79. });
  80. // We'll attach the profiler to the "done" event so that we can easily
  81. // attach the profiler output to the end of the output sent to the
  82. // browser. This will display the profiler's nice toolbar.
  83. Event::listen('laravel.done', function($response)
  84. {
  85. echo Profiler::render($response);
  86. });
  87. }
  88. }