profiler.php 2.4 KB

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