-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathStandard.php
199 lines (184 loc) · 5.15 KB
/
Standard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* Standard profiler driver that uses outputs for displaying profiling results.
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Profiler\Driver;
use Magento\Framework\Profiler\Driver\Standard\Output\Factory as OutputFactory;
use Magento\Framework\Profiler\Driver\Standard\OutputInterface;
use Magento\Framework\Profiler\Driver\Standard\Stat;
use Magento\Framework\Profiler\DriverInterface;
class Standard implements DriverInterface
{
/**
* Storage for timers statistics
*
* @var Stat
*/
protected $_stat;
/**
* List of profiler driver outputs
*
* @var OutputInterface[]
*/
protected $_outputs = [];
/**
* Constructor
*
* @param array|null $config
*/
public function __construct(array $config = null)
{
$this->_initOutputs($config);
$this->_initStat($config);
register_shutdown_function([$this, 'display']);
}
/**
* Init outputs by configuration
*
* @param array|null $config
* @return void
*/
protected function _initOutputs(array $config = null)
{
if (!$config) {
return;
}
$outputFactory = $this->_getOutputFactory($config);
foreach ($this->_getOutputConfigs($config) as $code => $outputConfig) {
$outputConfig = $this->_parseOutputConfig($outputConfig);
if (false === $outputConfig) {
continue;
}
if (!isset($outputConfig['type']) && !is_numeric($code)) {
$outputConfig['type'] = $code;
}
if (!isset($outputConfig['baseDir']) && isset($config['baseDir'])) {
$outputConfig['baseDir'] = $config['baseDir'];
}
$this->registerOutput($outputFactory->create($outputConfig));
}
}
/**
* Parses output config
*
* @param mixed $outputConfig
* @return array|bool
*/
protected function _parseOutputConfig($outputConfig)
{
$result = false;
if (is_array($outputConfig)) {
$result = $outputConfig;
} elseif (is_scalar($outputConfig) && $outputConfig) {
if (is_numeric($outputConfig)) {
$result = [];
} else {
$result = ['type' => $outputConfig];
}
}
return $result;
}
/**
* Get output configs
*
* @param array $config
* @return array
*/
protected function _getOutputConfigs(array $config = null)
{
$result = [];
if (isset($config['outputs'])) {
$result = $config['outputs'];
} elseif (isset($config['output'])) {
$result[] = $config['output'];
}
return $result;
}
/**
* Gets output factory from configuration or create new one
*
* @param array|null $config
* @return OutputFactory
*/
protected function _getOutputFactory(array $config = null)
{
if (isset($config['outputFactory']) && $config['outputFactory'] instanceof OutputFactory) {
$result = $config['outputFactory'];
} else {
$result = new OutputFactory();
}
return $result;
}
/**
* Init timers statistics object from configuration or create new one
*
* @param array $config|null
* @return void
*/
protected function _initStat(array $config = null)
{
if (isset($config['stat']) && $config['stat'] instanceof Stat) {
$this->_stat = $config['stat'];
} else {
$this->_stat = new Stat();
}
}
/**
* Clear collected statistics for specified timer or for whole profiler if timer id is omitted
*
* @param string|null $timerId
* @return void
*/
public function clear($timerId = null)
{
$this->_stat->clear($timerId);
}
/**
* Start collecting statistics for specified timer
*
* @param string $timerId
* @param array|null $tags
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function start($timerId, array $tags = null)
{
$this->_stat->start($timerId, microtime(true), memory_get_usage(true), memory_get_usage());
}
/**
* Stop recording statistics for specified timer.
*
* @param string $timerId
* @return void
*/
public function stop($timerId)
{
$this->_stat->stop($timerId, microtime(true), memory_get_usage(true), memory_get_usage());
}
/**
* Register profiler output instance to display profiling result at the end of execution
*
* @param OutputInterface $output
* @return void
*/
public function registerOutput(OutputInterface $output)
{
$this->_outputs[] = $output;
}
/**
* Display collected statistics with registered outputs
*
* @return void
*/
public function display()
{
if (\Magento\Framework\Profiler::isEnabled()) {
foreach ($this->_outputs as $output) {
$output->display($this->_stat);
}
}
}
}