diff --git a/libraries/src/Service/Provider/Database.php b/libraries/src/Service/Provider/Database.php index 86d50fc87560b..fd0ee8b776aec 100644 --- a/libraries/src/Service/Provider/Database.php +++ b/libraries/src/Service/Provider/Database.php @@ -90,6 +90,11 @@ function (Container $container) 'prefix' => $conf->get('dbprefix'), ]; + if (JDEBUG) + { + $options['monitor'] = new \Joomla\Database\Monitor\DebugMonitor; + } + try { $db = DatabaseDriver::getInstance($options); diff --git a/plugins/system/debug/DataCollector/QueryCollector.php b/plugins/system/debug/DataCollector/QueryCollector.php index 6e72adcb15421..675f29f976394 100644 --- a/plugins/system/debug/DataCollector/QueryCollector.php +++ b/plugins/system/debug/DataCollector/QueryCollector.php @@ -12,8 +12,8 @@ use DebugBar\DataCollector\AssetProvider; use Joomla\CMS\Factory; use Joomla\CMS\Uri\Uri; +use Joomla\Database\Monitor\DebugMonitor; use Joomla\Plugin\System\Debug\AbstractDataCollector; -use Joomla\Plugin\System\Debug\DebugMonitor; use Joomla\Registry\Registry; /** @@ -114,7 +114,7 @@ public function collect(): array 'xdebug_link' => $this->getXdebugLinkTemplate(), 'root_path' => JPATH_ROOT ], - 'count' => \count($this->queryMonitor->getLog()), + 'count' => \count($this->queryMonitor->getLogs()), ]; } @@ -179,13 +179,13 @@ public function getAssets(): array private function getStatements(): array { $statements = []; - $log = $this->queryMonitor->getLog(); + $logs = $this->queryMonitor->getLogs(); $timings = $this->queryMonitor->getTimings(); $memoryLogs = $this->queryMonitor->getMemoryLogs(); $stacks = $this->queryMonitor->getCallStacks(); $collectStacks = $this->params->get('query_traces'); - foreach ($log as $id => $item) + foreach ($logs as $id => $item) { $queryTime = 0; $queryMemory = 0; diff --git a/plugins/system/debug/DebugMonitor.php b/plugins/system/debug/DebugMonitor.php deleted file mode 100644 index cd7e7ad129d03..0000000000000 --- a/plugins/system/debug/DebugMonitor.php +++ /dev/null @@ -1,158 +0,0 @@ -enabled = $enabled; - } - - /** - * Act on a query being started. - * - * @param string $sql The SQL to be executed. - * - * @return void - * - * @since 4.0.0 - */ - public function startQuery(string $sql) - { - if ($this->enabled) - { - $this->log[] = $sql; - $this->timings[] = microtime(true); - $this->memoryLogs[] = memory_get_usage(); - } - } - - /** - * Act on a query being stopped. - * - * @return void - * - * @since 4.0.0 - */ - public function stopQuery() - { - if ($this->enabled) - { - $this->timings[] = microtime(true); - $this->memoryLogs[] = memory_get_usage(); - $this->callStacks[] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - } - } - - /** - * Get the logged call stacks. - * - * @return array - * - * @since 4.0.0 - */ - public function getCallStacks() - { - return $this->callStacks; - } - - /** - * Get the logged queries. - * - * @return array - * - * @since 4.0.0 - */ - public function getLog() - { - return $this->log; - } - - /** - * Get the logged timings. - * - * @return array - * - * @since 4.0.0 - */ - public function getTimings() - { - return $this->timings; - } - - /** - * Get the logged memory logs. - * - * @return array - * - * @since 4.0.0 - */ - public function getMemoryLogs(): array - { - return $this->memoryLogs; - } -} diff --git a/plugins/system/debug/debug.php b/plugins/system/debug/debug.php index eb4e934fe5459..354b6818c6be3 100644 --- a/plugins/system/debug/debug.php +++ b/plugins/system/debug/debug.php @@ -31,7 +31,6 @@ use Joomla\Plugin\System\Debug\DataCollector\ProfileCollector; use Joomla\Plugin\System\Debug\DataCollector\QueryCollector; use Joomla\Plugin\System\Debug\DataCollector\SessionCollector; -use Joomla\Plugin\System\Debug\DebugMonitor; use Joomla\Plugin\System\Debug\Storage\FileStorage; /** @@ -114,7 +113,7 @@ class PlgSystemDebug extends CMSPlugin /** * The query monitor. * - * @var DebugMonitor + * @var \Joomla\Database\Monitor\DebugMonitor * @since 4.0.0 */ private $queryMonitor; @@ -166,10 +165,14 @@ public function __construct(&$subject, $config) // @todo Remove when a standard autoloader is available. JLoader::registerNamespace('Joomla\\Plugin\\System\\Debug', __DIR__, false, false, 'psr4'); - // Attach our query monitor to the database driver - $this->queryMonitor = new DebugMonitor(JDEBUG); + /** @var \Joomla\Database\Monitor\DebugMonitor */ + $this->queryMonitor = $this->db->getMonitor(); - $this->db->setMonitor($this->queryMonitor); + if (!$this->params->get('queries', 1)) + { + // Remove the database driver monitor + $this->db->setMonitor(null); + } $storagePath = JPATH_CACHE . '/plg_system_debug_' . $this->app->getClientId(); @@ -466,8 +469,8 @@ public function onAfterDisconnect(ConnectionEvent $event) $db = $event->getDriver(); - // Set a dummy monitor to avoid monitoring the following queries - $db->setMonitor(new DebugMonitor); + // Remove the monitor to avoid monitoring the following queries + $db->setMonitor(null); $this->totalQueries = $db->getCount(); @@ -508,9 +511,9 @@ public function onAfterDisconnect(ConnectionEvent $event) if ($this->params->get('query_explains') && in_array($db->getServerType(), ['mysql', 'postgresql'], true)) { - $log = $this->queryMonitor->getLog(); + $logs = $this->queryMonitor->getLogs(); - foreach ($log as $k => $query) + foreach ($logs as $k => $query) { $dbVersion56 = $db->getServerType() === 'mysql' && version_compare($db->getVersion(), '5.6', '>=');