|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains functionality related to debugging, logging and timing actions |
|
4
|
|
|
* |
|
5
|
|
|
* @package BZiON |
|
6
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace BZIon\Debug; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Useful debugging functions (will only waste time on a development environment) |
|
13
|
|
|
*/ |
|
14
|
|
|
abstract class Debug |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Start counting time for an event |
|
18
|
|
|
* @param string $event The event's name |
|
19
|
|
|
* @return void |
|
20
|
|
|
*/ |
|
21
|
70 |
|
public static function startStopwatch($event) |
|
22
|
|
|
{ |
|
23
|
70 |
|
if (!\Service::getContainer()) { |
|
24
|
|
|
return; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
70 |
|
$stopwatch = \Service::getContainer()->get('debug.stopwatch', null); |
|
28
|
|
|
|
|
29
|
70 |
|
if ($stopwatch) { |
|
30
|
70 |
|
$stopwatch->start($event); |
|
31
|
70 |
|
} |
|
32
|
70 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Stop counting time for an event and get its duration |
|
36
|
|
|
* @param string $event The event's name |
|
37
|
|
|
* @return int The time in milliseconds |
|
38
|
|
|
*/ |
|
39
|
70 |
|
public static function finishStopwatch($event) |
|
40
|
|
|
{ |
|
41
|
70 |
|
if (!\Service::getContainer()) { |
|
42
|
|
|
return 0; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
70 |
|
$stopwatch = \Service::getContainer()->get('debug.stopwatch', null); |
|
46
|
|
|
|
|
47
|
70 |
|
if (!$stopwatch) { |
|
48
|
|
|
return 0; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
70 |
|
$event = $stopwatch->stop($event); |
|
52
|
70 |
|
$periods = $event->getPeriods(); |
|
53
|
70 |
|
$duration = end($periods)->getDuration(); |
|
54
|
|
|
|
|
55
|
70 |
|
return $duration; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Log a debug message |
|
60
|
|
|
* @param string $message The message to return |
|
61
|
|
|
* @param array $context Any additional information to show |
|
62
|
|
|
* @param string $channel The channel to store the log into |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
70 |
|
public static function log($message, array $context = array(), $channel = 'app') |
|
66
|
|
|
{ |
|
67
|
70 |
|
if (!\Service::getContainer()) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
70 |
|
$logger = \Service::getContainer()->get("monolog.logger.$channel", null); |
|
72
|
|
|
|
|
73
|
70 |
|
if (!$logger) { |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
70 |
|
$logger->debug($message, $context); |
|
78
|
70 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Log a cache fetch in the data collector |
|
82
|
|
|
* @param string $type The type of the fetched model |
|
83
|
|
|
* @param int $id The ID of the fetched model |
|
84
|
|
|
*/ |
|
85
|
70 |
|
public static function logCacheFetch($type, $id) |
|
86
|
|
|
{ |
|
87
|
70 |
|
if (\Service::isDebug() && \Service::getContainer()) { |
|
88
|
|
|
$collector = \Service::getContainer()->get('data_collector.bzion_database_collector', null); |
|
89
|
|
|
if ($collector) { |
|
90
|
|
|
$collector->logCacheFetch($type, $id); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
70 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|