1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/***************************************** |
4
|
|
|
* Title : PHP Quick Profiler Console Class |
5
|
|
|
* Author : Created by Ryan Campbell |
6
|
|
|
* URL : http://particletree.com/features/php-quick-profiler/ |
7
|
|
|
* Description : This class serves as a wrapper to hold onto |
8
|
|
|
* various messages and profiling data |
9
|
|
|
*****************************************/ |
10
|
|
|
|
11
|
|
|
namespace Particletree\Pqp; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
|
15
|
|
|
class Console |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
protected $store = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Logs data to the console |
23
|
|
|
* Accepts any data type |
24
|
|
|
* |
25
|
|
|
* @param mixed $data |
26
|
|
|
*/ |
27
|
|
|
public function log($data) |
28
|
|
|
{ |
29
|
|
|
array_push($this->store, array( |
30
|
|
|
'data' => $data, |
31
|
|
|
'type' => 'log' |
32
|
|
|
)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Logs memory usage of a variable |
37
|
|
|
* If no parameter is passed in, logs current memory usage |
38
|
|
|
* |
39
|
|
|
* @param mixed $object |
40
|
|
|
* @param string $name |
41
|
|
|
* @param boolean $literal |
42
|
|
|
*/ |
43
|
|
|
public function logMemory($object = null, $name = 'PHP', $literal = false) |
44
|
|
|
{ |
45
|
|
|
$memory = memory_get_usage(); |
46
|
|
|
$dataType = ''; |
47
|
|
|
if (!is_null($object) && !$literal) { |
48
|
|
|
$memory = strlen(serialize($object)); |
49
|
|
|
$dataType = gettype($object); |
50
|
|
|
} else if (is_numeric($object) && $literal) { |
51
|
|
|
$memory = floatval($object); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
array_push($this->store, array( |
55
|
|
|
'name' => $name, |
56
|
|
|
'data' => $memory, |
57
|
|
|
'data_type' => $dataType, |
58
|
|
|
'type' => 'memory' |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Logs exception with optional message override |
64
|
|
|
* |
65
|
|
|
* @param Exception $exception |
66
|
|
|
* @param string $message |
67
|
|
|
*/ |
68
|
|
|
public function logError(Exception $exception, $message = '') |
69
|
|
|
{ |
70
|
|
|
if (empty($message)) { |
71
|
|
|
$message = $exception->getMessage(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
array_push($this->store, array( |
75
|
|
|
'data' => $message, |
76
|
|
|
'file' => $exception->getFile(), |
77
|
|
|
'line' => $exception->getLine(), |
78
|
|
|
'type' => 'error' |
79
|
|
|
)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Logs current time with optional message |
84
|
|
|
* |
85
|
|
|
* @param string $name |
86
|
|
|
* @param float $literalTime |
87
|
|
|
*/ |
88
|
|
|
public function logSpeed($name = 'Point in Time', $literalTime = null) |
89
|
|
|
{ |
90
|
|
|
$time = microtime(true); |
91
|
|
|
if (!is_null($literalTime) && is_float($literalTime)) { |
92
|
|
|
$time = $literalTime; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
array_push($this->store, array( |
96
|
|
|
'data' => $time, |
97
|
|
|
'name' => $name, |
98
|
|
|
'type' => 'speed' |
99
|
|
|
)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns the collected logs |
104
|
|
|
* |
105
|
|
|
* @returns array |
106
|
|
|
*/ |
107
|
|
|
public function getLogs() |
108
|
|
|
{ |
109
|
|
|
return $this->store; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|