Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Profiler.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Framework;
9 
12 
17 class Profiler
18 {
22  const NESTING_SEPARATOR = '->';
23 
29  private static $_enabled = false;
30 
36  private static $_currentPath = [];
37 
43  private static $_pathCount = 0;
44 
50  private static $_pathIndex = [];
51 
57  private static $_drivers = [];
58 
64  private static $_defaultTags = [];
65 
71  private static $_tagFilters = [];
72 
78  private static $_hasTagFilters = false;
79 
86  public static function setDefaultTags(array $tags)
87  {
88  self::$_defaultTags = $tags;
89  }
90 
98  public static function addTagFilter($tagName, $tagValue)
99  {
100  if (!isset(self::$_tagFilters[$tagName])) {
101  self::$_tagFilters[$tagName] = [];
102  }
103  self::$_tagFilters[$tagName][] = $tagValue;
104  self::$_hasTagFilters = true;
105  }
106 
113  private static function _checkTags(array $tags = null)
114  {
115  if (self::$_hasTagFilters) {
116  if (is_array($tags)) {
117  $keysToCheck = array_intersect(array_keys(self::$_tagFilters), array_keys($tags));
118  if ($keysToCheck) {
119  foreach ($keysToCheck as $keyToCheck) {
120  if (in_array($tags[$keyToCheck], self::$_tagFilters[$keyToCheck])) {
121  return true;
122  }
123  }
124  }
125  }
126  return false;
127  }
128  return true;
129  }
130 
137  public static function add(DriverInterface $driver)
138  {
139  self::$_drivers[] = $driver;
140  self::enable();
141  }
142 
149  private static function _getTimerId($timerName = null)
150  {
151  if (!self::$_currentPath) {
152  return (string)$timerName;
153  } elseif ($timerName) {
154  return implode(self::NESTING_SEPARATOR, self::$_currentPath) . self::NESTING_SEPARATOR . $timerName;
155  } else {
156  return implode(self::NESTING_SEPARATOR, self::$_currentPath);
157  }
158  }
159 
166  private static function _getTags(array $tags = null)
167  {
168  if (self::$_defaultTags) {
169  return (array)$tags + self::$_defaultTags;
170  } else {
171  return $tags;
172  }
173  }
174 
182  public static function enable()
183  {
184  self::$_enabled = true;
185  }
186 
194  public static function disable()
195  {
196  self::$_enabled = false;
197  }
198 
204  public static function isEnabled()
205  {
206  return self::$_enabled;
207  }
208 
216  public static function clear($timerName = null)
217  {
218  if (strpos($timerName, self::NESTING_SEPARATOR) !== false) {
219  throw new \InvalidArgumentException('Timer name must not contain a nesting separator.');
220  }
221  $timerId = self::_getTimerId($timerName);
223  foreach (self::$_drivers as $driver) {
224  $driver->clear($timerId);
225  }
226  }
227 
233  public static function reset()
234  {
235  self::clear();
236  self::$_enabled = false;
237  self::$_currentPath = [];
238  self::$_tagFilters = [];
239  self::$_defaultTags = [];
240  self::$_hasTagFilters = false;
241  self::$_drivers = [];
242  self::$_pathCount = 0;
243  self::$_pathIndex = [];
244  }
245 
254  public static function start($timerName, array $tags = null)
255  {
256  if (!self::$_enabled) {
257  return;
258  }
259 
260  $tags = self::_getTags($tags);
261  if (!self::_checkTags($tags)) {
262  return;
263  }
264 
265  if (strpos($timerName, self::NESTING_SEPARATOR) !== false) {
266  throw new \InvalidArgumentException('Timer name must not contain a nesting separator.');
267  }
268 
269  $timerId = self::_getTimerId($timerName);
271  foreach (self::$_drivers as $driver) {
272  $driver->start($timerId, $tags);
273  }
274  /* Continue collecting timers statistics under the latest started one */
275  self::$_currentPath[] = $timerName;
276  self::$_pathCount++;
277  self::$_pathIndex[$timerName][] = self::$_pathCount;
278  }
279 
290  public static function stop($timerName = null)
291  {
292  if (!self::$_enabled || !self::_checkTags(self::_getTags())) {
293  return;
294  }
295 
296  if ($timerName === null) {
297  $timersToStop = 1;
298  } else {
299  $timerPosition = false;
300  if (!empty(self::$_pathIndex[$timerName])) {
301  $timerPosition = array_pop(self::$_pathIndex[$timerName]);
302  }
303  if ($timerPosition === false) {
304  throw new \InvalidArgumentException(sprintf('Timer "%s" has not been started.', $timerName));
305  } elseif ($timerPosition === 1) {
306  $timersToStop = 1;
307  } else {
308  $timersToStop = self::$_pathCount + 1 - $timerPosition;
309  }
310  }
311 
312  for ($i = 0; $i < $timersToStop; $i++) {
313  $timerId = self::_getTimerId();
315  foreach (self::$_drivers as $driver) {
316  $driver->stop($timerId);
317  }
318  /* Move one level up in timers nesting tree */
319  array_pop(self::$_currentPath);
320  self::$_pathCount--;
321  }
322  }
323 
332  public static function applyConfig($config, $baseDir, $isAjax = false)
333  {
335  if ($config['driverConfigs']) {
336  foreach ($config['driverConfigs'] as $driverConfig) {
337  self::add($config['driverFactory']->create($driverConfig));
338  }
339  }
340  foreach ($config['tagFilters'] as $tagName => $tagValue) {
341  self::addTagFilter($tagName, $tagValue);
342  }
343  }
344 
355  protected static function _parseConfig($profilerConfig, $baseDir, $isAjax)
356  {
357  $config = ['baseDir' => $baseDir, 'tagFilters' => []];
358 
359  if (is_scalar($profilerConfig)) {
360  $config['drivers'] = [
361  ['output' => is_numeric($profilerConfig) ? 'html' : $profilerConfig],
362  ];
363  } else {
364  $config = array_merge($config, $profilerConfig);
365  }
366 
367  $driverConfigs = (array)(isset($config['drivers']) ? $config['drivers'] : []);
368  $driverFactory = isset($config['driverFactory']) ? $config['driverFactory'] : new Factory();
369  $tagFilters = (array)(isset($config['tagFilters']) ? $config['tagFilters'] : []);
370 
371  $result = [
372  'driverConfigs' => self::_parseDriverConfigs($driverConfigs, $config['baseDir']),
373  'driverFactory' => $driverFactory,
374  'tagFilters' => $tagFilters,
375  'baseDir' => $config['baseDir'],
376  ];
377  return $result;
378  }
379 
387  protected static function _parseDriverConfigs(array $driverConfigs, $baseDir)
388  {
389  $result = [];
390  foreach ($driverConfigs as $code => $driverConfig) {
391  $driverConfig = self::_parseDriverConfig($driverConfig);
392  if ($driverConfig === false) {
393  continue;
394  }
395  if (!isset($driverConfig['type']) && !is_numeric($code)) {
396  $driverConfig['type'] = $code;
397  }
398  if (!isset($driverConfig['baseDir']) && $baseDir) {
399  $driverConfig['baseDir'] = $baseDir;
400  }
401  $result[] = $driverConfig;
402  }
403  return $result;
404  }
405 
412  protected static function _parseDriverConfig($driverConfig)
413  {
414  $result = false;
415  if (is_array($driverConfig)) {
416  $result = $driverConfig;
417  } elseif (is_scalar($driverConfig) && $driverConfig) {
418  if (is_numeric($driverConfig)) {
419  $result = [];
420  } else {
421  $result = ['type' => $driverConfig];
422  }
423  }
424  return $result;
425  }
426 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$baseDir
Definition: autoload.php:9
$config
Definition: fraud_order.php:17
static add(DriverInterface $driver)
Definition: Profiler.php:137
static setDefaultTags(array $tags)
Definition: Profiler.php:86
static _parseDriverConfigs(array $driverConfigs, $baseDir)
Definition: Profiler.php:387
static _parseDriverConfig($driverConfig)
Definition: Profiler.php:412
static _parseConfig($profilerConfig, $baseDir, $isAjax)
Definition: Profiler.php:355
static applyConfig($config, $baseDir, $isAjax=false)
Definition: Profiler.php:332
$i
Definition: gallery.phtml:31
static addTagFilter($tagName, $tagValue)
Definition: Profiler.php:98
$code
Definition: info.phtml:12