profiler.php 4.86 KB
Newer Older
Taylor Otwell committed
1 2 3 4 5 6
<?php namespace Laravel\Profiling;

use Laravel\View;
use Laravel\File;
use Laravel\Event;
use Laravel\Config;
7
use Laravel\Request;
8
use Laravel\Database;
Taylor Otwell committed
9 10 11 12 13 14 15 16

class Profiler {

	/**
	 * An array of the recorded Profiler data.
	 *
	 * @var array
	 */
Jesse O'Brien committed
17
	protected static $data = array('queries' => array(), 'logs' => array(), 'timers' => array());
18 19
	
	/**
Taylor Otwell committed
20 21
	 * Get the rendered contents of the Profiler.
	 *
Taylor Otwell committed
22
	 * @param  Response  $response
Taylor Otwell committed
23 24
	 * @return string
	 */
Taylor Otwell committed
25
	public static function render($response)
Taylor Otwell committed
26
	{
Taylor Otwell committed
27 28 29
		// We only want to send the profiler toolbar if the request is not an AJAX
		// request, as sending it on AJAX requests could mess up JSON driven API
		// type applications, so we will not send anything in those scenarios.
30
		if ( ! Request::ajax() and Config::get('application.profiler') )
31
		{
32 33
			static::$data['memory'] = get_file_size(memory_get_usage(true));
			static::$data['memory_peak'] = get_file_size(memory_get_peak_usage(true));
34
			static::$data['time'] = number_format((microtime(true) - LARAVEL_START) * 1000, 2);
Jesse O'Brien committed
35 36 37 38 39
			foreach ( static::$data['timers'] as &$timer)
			{
				$timer['running_time'] = number_format((microtime(true) - $timer['start'] ) * 1000, 2);
			}

40 41
			return render('path: '.__DIR__.'/template'.BLADE_EXT, static::$data);
		}
Taylor Otwell committed
42 43 44
	}

	/**
Jesse O'Brien committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	 * Allow a callback to be timed.
	 *
	 * @param closure $func
	 * @param string $name
	 * @return void
	 */
	public static function time( $func, $name = 'default_func_timer' )
	{
		// First measure the runtime of the func
		$start = microtime(true);
		$func();
		$end = microtime(true);

		// Check to see if a timer by that name exists
		if (isset(static::$data['timers'][$name]))
		{
			$name = $name.uniqid();
		}
		
		// Push the time into the timers array for display
		static::$data['timers'][$name]['start'] = $start;
		static::$data['timers'][$name]['end'] = $end;
		static::$data['timers'][$name]['time'] = number_format(($end - $start) * 1000, 2);
	}

	/**
	 *  Start, or add a tick to a timer.
	 *
	 * @param string $name
	 * @return void
	 */
	public static function tick($name = 'default_timer', $callback = null)
	{
		$name = trim($name);
		if (empty($name)) $name = 'default_timer';

		// Is this a brand new tick?
		if (isset(static::$data['timers'][$name]))
		{
			$current_timer = static::$data['timers'][$name];
			$ticks = count($current_timer['ticks']);

			// Initialize the new time for the tick
			$new_tick = array();
			$mt = microtime(true);
			$new_tick['raw_time'] = $mt - $current_timer['start'];
			$new_tick['time'] = number_format(($mt - $current_timer['start']) * 1000, 2);

			// Use either the start time or the last tick for the diff
			if ($ticks > 0)
			{
				$last_tick = $current_timer['ticks'][$ticks- 1]['raw_time'];
				$new_tick['diff'] = number_format(($new_tick['raw_time'] - $last_tick) * 1000, 2);
			}
			else
			{
				$new_tick['diff'] = $new_tick['time'];
			}

			// Add the new tick to the stack of them
			static::$data['timers'][$name]['ticks'][] = $new_tick;
		}
		else
		{
			// Initialize a start time on the first tick
			static::$data['timers'][$name]['start'] = microtime(true);
			static::$data['timers'][$name]['ticks'] = array();
		}

		// Run the callback for this tick if it's specified
		if ( ! is_null($callback) and is_callable($callback))
		{
			// After we've ticked, call the callback function
			call_user_func_array($callback, array(
				static::$data['timers'][$name]
			));
		}
	}

	/**
Taylor Otwell committed
125 126
	 * Add a log entry to the log entries array.
	 *
Sergii Grebeniuk committed
127 128
	 * @param  string  $type
	 * @param  string  $message
Taylor Otwell committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	 * @return void
	 */
	public static function log($type, $message)
	{
		static::$data['logs'][] = array($type, $message);
	}

	/**
	 * Add a performed SQL query to the Profiler.
	 *
	 * @param 	string 	$sql
	 * @param 	array 	$bindings
	 * @param 	float 	$time
	 * @return 	void
	 */
	public static function query($sql, $bindings, $time)
	{
		foreach ($bindings as $binding)
		{
148
			$binding = Database::connection()->pdo->quote($binding);
149

Taylor Otwell committed
150
			$sql = preg_replace('/\?/', $binding, $sql, 1);
151
			$sql = htmlspecialchars($sql);
Taylor Otwell committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
		}

		static::$data['queries'][] = array($sql, $time);
	}

	/**
	 * Attach the Profiler's event listeners.
	 *
	 * @return void
	 */
	public static function attach()
	{
		// First we'll attach to the query and log events. These allow us to catch
		// all of the SQL queries and log messages that come through Laravel,
		// and we will pass them onto the Profiler for simple storage.
		Event::listen('laravel.log', function($type, $message)
		{
			Profiler::log($type, $message);
		});

		Event::listen('laravel.query', function($sql, $bindings, $time)
		{
			Profiler::query($sql, $bindings, $time);			
		});

		// We'll attach the profiler to the "done" event so that we can easily
		// attach the profiler output to the end of the output sent to the
		// browser. This will display the profiler's nice toolbar.
180
		Event::listen('laravel.done', function($response)
Taylor Otwell committed
181
		{
Taylor Otwell committed
182
			echo Profiler::render($response);
Taylor Otwell committed
183 184 185 186
		});
	}

}