[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Plugin 5 * @subpackage System.Debug 6 * 7 * @copyright (C) 2018 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 */ 10 11 namespace Joomla\Plugin\System\Debug\DataCollector; 12 13 use DebugBar\DataCollector\AssetProvider; 14 use Joomla\CMS\Uri\Uri; 15 use Joomla\Database\Monitor\DebugMonitor; 16 use Joomla\Plugin\System\Debug\AbstractDataCollector; 17 use Joomla\Registry\Registry; 18 19 // phpcs:disable PSR1.Files.SideEffects 20 \defined('_JEXEC') or die; 21 // phpcs:enable PSR1.Files.SideEffects 22 23 /** 24 * QueryDataCollector 25 * 26 * @since 4.0.0 27 */ 28 class QueryCollector extends AbstractDataCollector implements AssetProvider 29 { 30 /** 31 * Collector name. 32 * 33 * @var string 34 * @since 4.0.0 35 */ 36 private $name = 'queries'; 37 38 /** 39 * The query monitor. 40 * 41 * @var DebugMonitor 42 * @since 4.0.0 43 */ 44 private $queryMonitor; 45 46 /** 47 * Profile data. 48 * 49 * @var array 50 * @since 4.0.0 51 */ 52 private $profiles; 53 54 /** 55 * Explain data. 56 * 57 * @var array 58 * @since 4.0.0 59 */ 60 private $explains; 61 62 /** 63 * Accumulated Duration. 64 * 65 * @var integer 66 * @since 4.0.0 67 */ 68 private $accumulatedDuration = 0; 69 70 /** 71 * Accumulated Memory. 72 * 73 * @var integer 74 * @since 4.0.0 75 */ 76 private $accumulatedMemory = 0; 77 78 /** 79 * Constructor. 80 * 81 * @param Registry $params Parameters. 82 * @param DebugMonitor $queryMonitor Query monitor. 83 * @param array $profiles Profile data. 84 * @param array $explains Explain data 85 * 86 * @since 4.0.0 87 */ 88 public function __construct(Registry $params, DebugMonitor $queryMonitor, array $profiles, array $explains) 89 { 90 $this->queryMonitor = $queryMonitor; 91 92 parent::__construct($params); 93 94 $this->profiles = $profiles; 95 $this->explains = $explains; 96 } 97 98 /** 99 * Called by the DebugBar when data needs to be collected 100 * 101 * @since 4.0.0 102 * 103 * @return array Collected data 104 */ 105 public function collect(): array 106 { 107 $statements = $this->getStatements(); 108 109 return [ 110 'data' => [ 111 'statements' => $statements, 112 'nb_statements' => \count($statements), 113 'accumulated_duration_str' => $this->getDataFormatter()->formatDuration($this->accumulatedDuration), 114 'memory_usage_str' => $this->getDataFormatter()->formatBytes($this->accumulatedMemory), 115 'xdebug_link' => $this->getXdebugLinkTemplate(), 116 'root_path' => JPATH_ROOT, 117 ], 118 'count' => \count($this->queryMonitor->getLogs()), 119 ]; 120 } 121 122 /** 123 * Returns the unique name of the collector 124 * 125 * @since 4.0.0 126 * 127 * @return string 128 */ 129 public function getName(): string 130 { 131 return $this->name; 132 } 133 134 /** 135 * Returns a hash where keys are control names and their values 136 * an array of options as defined in {@see \DebugBar\JavascriptRenderer::addControl()} 137 * 138 * @since 4.0.0 139 * 140 * @return array 141 */ 142 public function getWidgets(): array 143 { 144 return [ 145 'queries' => [ 146 'icon' => 'database', 147 'widget' => 'PhpDebugBar.Widgets.SQLQueriesWidget', 148 'map' => $this->name . '.data', 149 'default' => '[]', 150 ], 151 'queries:badge' => [ 152 'map' => $this->name . '.count', 153 'default' => 'null', 154 ], 155 ]; 156 } 157 158 /** 159 * Assets for the collector. 160 * 161 * @since 4.0.0 162 * 163 * @return array 164 */ 165 public function getAssets(): array 166 { 167 return [ 168 'css' => Uri::root(true) . '/media/plg_system_debug/widgets/sqlqueries/widget.min.css', 169 'js' => Uri::root(true) . '/media/plg_system_debug/widgets/sqlqueries/widget.min.js', 170 ]; 171 } 172 173 /** 174 * Prepare the executed statements data. 175 * 176 * @since 4.0.0 177 * 178 * @return array 179 */ 180 private function getStatements(): array 181 { 182 $statements = []; 183 $logs = $this->queryMonitor->getLogs(); 184 $boundParams = $this->queryMonitor->getBoundParams(); 185 $timings = $this->queryMonitor->getTimings(); 186 $memoryLogs = $this->queryMonitor->getMemoryLogs(); 187 $stacks = $this->queryMonitor->getCallStacks(); 188 $collectStacks = $this->params->get('query_traces'); 189 190 foreach ($logs as $id => $item) { 191 $queryTime = 0; 192 $queryMemory = 0; 193 194 if ($timings && isset($timings[$id * 2 + 1])) { 195 // Compute the query time. 196 $queryTime = ($timings[$id * 2 + 1] - $timings[$id * 2]); 197 $this->accumulatedDuration += $queryTime; 198 } 199 200 if ($memoryLogs && isset($memoryLogs[$id * 2 + 1])) { 201 // Compute the query memory usage. 202 $queryMemory = ($memoryLogs[$id * 2 + 1] - $memoryLogs[$id * 2]); 203 $this->accumulatedMemory += $queryMemory; 204 } 205 206 $trace = []; 207 $callerLocation = ''; 208 209 if (isset($stacks[$id])) { 210 $cnt = 0; 211 212 foreach ($stacks[$id] as $i => $stack) { 213 $class = $stack['class'] ?? ''; 214 $file = $stack['file'] ?? ''; 215 $line = $stack['line'] ?? ''; 216 217 $caller = $this->formatCallerInfo($stack); 218 $location = $file && $line ? "$file:$line" : 'same'; 219 220 $isCaller = 0; 221 222 if (\Joomla\Database\DatabaseDriver::class === $class && false === strpos($file, 'DatabaseDriver.php')) { 223 $callerLocation = $location; 224 $isCaller = 1; 225 } 226 227 if ($collectStacks) { 228 $trace[] = [\count($stacks[$id]) - $cnt, $isCaller, $caller, $file, $line]; 229 } 230 231 $cnt++; 232 } 233 } 234 235 $explain = $this->explains[$id] ?? []; 236 $explainColumns = []; 237 238 // Extract column labels for Explain table 239 if ($explain) { 240 $explainColumns = array_keys(reset($explain)); 241 } 242 243 $statements[] = [ 244 'sql' => $item, 245 'params' => $boundParams[$id] ?? [], 246 'duration_str' => $this->getDataFormatter()->formatDuration($queryTime), 247 'memory_str' => $this->getDataFormatter()->formatBytes($queryMemory), 248 'caller' => $callerLocation, 249 'callstack' => $trace, 250 'explain' => $explain, 251 'explain_col' => $explainColumns, 252 'profile' => $this->profiles[$id] ?? [], 253 ]; 254 } 255 256 return $statements; 257 } 258 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |