Standard.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Standard profiler driver that uses outputs for displaying profiling results.
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Profiler\Driver;
  9. use Magento\Framework\Profiler\Driver\Standard\Output\Factory as OutputFactory;
  10. use Magento\Framework\Profiler\Driver\Standard\OutputInterface;
  11. use Magento\Framework\Profiler\Driver\Standard\Stat;
  12. use Magento\Framework\Profiler\DriverInterface;
  13. class Standard implements DriverInterface
  14. {
  15. /**
  16. * Storage for timers statistics
  17. *
  18. * @var Stat
  19. */
  20. protected $_stat;
  21. /**
  22. * List of profiler driver outputs
  23. *
  24. * @var OutputInterface[]
  25. */
  26. protected $_outputs = [];
  27. /**
  28. * Constructor
  29. *
  30. * @param array|null $config
  31. */
  32. public function __construct(array $config = null)
  33. {
  34. $this->_initOutputs($config);
  35. $this->_initStat($config);
  36. register_shutdown_function([$this, 'display']);
  37. }
  38. /**
  39. * Init outputs by configuration
  40. *
  41. * @param array|null $config
  42. * @return void
  43. */
  44. protected function _initOutputs(array $config = null)
  45. {
  46. if (!$config) {
  47. return;
  48. }
  49. $outputFactory = $this->_getOutputFactory($config);
  50. foreach ($this->_getOutputConfigs($config) as $code => $outputConfig) {
  51. $outputConfig = $this->_parseOutputConfig($outputConfig);
  52. if (false === $outputConfig) {
  53. continue;
  54. }
  55. if (!isset($outputConfig['type']) && !is_numeric($code)) {
  56. $outputConfig['type'] = $code;
  57. }
  58. if (!isset($outputConfig['baseDir']) && isset($config['baseDir'])) {
  59. $outputConfig['baseDir'] = $config['baseDir'];
  60. }
  61. $this->registerOutput($outputFactory->create($outputConfig));
  62. }
  63. }
  64. /**
  65. * Parses output config
  66. *
  67. * @param mixed $outputConfig
  68. * @return array|bool
  69. */
  70. protected function _parseOutputConfig($outputConfig)
  71. {
  72. $result = false;
  73. if (is_array($outputConfig)) {
  74. $result = $outputConfig;
  75. } elseif (is_scalar($outputConfig) && $outputConfig) {
  76. if (is_numeric($outputConfig)) {
  77. $result = [];
  78. } else {
  79. $result = ['type' => $outputConfig];
  80. }
  81. }
  82. return $result;
  83. }
  84. /**
  85. * Get output configs
  86. *
  87. * @param array $config
  88. * @return array
  89. */
  90. protected function _getOutputConfigs(array $config = null)
  91. {
  92. $result = [];
  93. if (isset($config['outputs'])) {
  94. $result = $config['outputs'];
  95. } elseif (isset($config['output'])) {
  96. $result[] = $config['output'];
  97. }
  98. return $result;
  99. }
  100. /**
  101. * Gets output factory from configuration or create new one
  102. *
  103. * @param array|null $config
  104. * @return OutputFactory
  105. */
  106. protected function _getOutputFactory(array $config = null)
  107. {
  108. if (isset($config['outputFactory']) && $config['outputFactory'] instanceof OutputFactory) {
  109. $result = $config['outputFactory'];
  110. } else {
  111. $result = new OutputFactory();
  112. }
  113. return $result;
  114. }
  115. /**
  116. * Init timers statistics object from configuration or create new one
  117. *
  118. * @param array $config|null
  119. * @return void
  120. */
  121. protected function _initStat(array $config = null)
  122. {
  123. if (isset($config['stat']) && $config['stat'] instanceof Stat) {
  124. $this->_stat = $config['stat'];
  125. } else {
  126. $this->_stat = new Stat();
  127. }
  128. }
  129. /**
  130. * Clear collected statistics for specified timer or for whole profiler if timer id is omitted
  131. *
  132. * @param string|null $timerId
  133. * @return void
  134. */
  135. public function clear($timerId = null)
  136. {
  137. $this->_stat->clear($timerId);
  138. }
  139. /**
  140. * Start collecting statistics for specified timer
  141. *
  142. * @param string $timerId
  143. * @param array|null $tags
  144. * @return void
  145. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  146. */
  147. public function start($timerId, array $tags = null)
  148. {
  149. $this->_stat->start($timerId, microtime(true), memory_get_usage(true), memory_get_usage());
  150. }
  151. /**
  152. * Stop recording statistics for specified timer.
  153. *
  154. * @param string $timerId
  155. * @return void
  156. */
  157. public function stop($timerId)
  158. {
  159. $this->_stat->stop($timerId, microtime(true), memory_get_usage(true), memory_get_usage());
  160. }
  161. /**
  162. * Register profiler output instance to display profiling result at the end of execution
  163. *
  164. * @param OutputInterface $output
  165. * @return void
  166. */
  167. public function registerOutput(OutputInterface $output)
  168. {
  169. $this->_outputs[] = $output;
  170. }
  171. /**
  172. * Display collected statistics with registered outputs
  173. *
  174. * @return void
  175. */
  176. public function display()
  177. {
  178. if (\Magento\Framework\Profiler::isEnabled()) {
  179. foreach ($this->_outputs as $output) {
  180. $output->display($this->_stat);
  181. }
  182. }
  183. }
  184. }