Skip to content

Commit

Permalink
Initialize the plugin on first triggered event
Browse files Browse the repository at this point in the history
  • Loading branch information
laoneo committed Sep 12, 2019
1 parent 90eb3b0 commit 6387e5a
Showing 1 changed file with 55 additions and 51 deletions.
106 changes: 55 additions & 51 deletions plugins/system/debug/debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,57 +126,6 @@ class PlgSystemDebug extends CMSPlugin
*/
protected $isAjax = false;

/**
* Registers legacy Listeners to the Dispatcher, emulating how plugins worked under Joomla! 3.x and below.
*
* By default, this method will look for all public methods whose name starts with "on". It will register
* lambda functions (closures) which try to unwrap the arguments of the dispatched Event into method call
* arguments and call your on<Something> method. The result will be passed back to the Event into its 'result'
* argument.
*
* This method additionally supports Joomla\Event\SubscriberInterface and plugins implementing this will be
* registered to the dispatcher as a subscriber.
*
* @return void
*
* @since 4.0
*/
public function registerListeners()
{
parent::registerListeners();

$this->debugLang = $this->app->get('debug_lang');

// Skip the plugin if debug is off
if (!$this->debugLang && !$this->app->get('debug'))
{
return;
}

$this->app->getConfig()->set('gzip', false);
ob_start();
ob_implicit_flush(false);

/** @var \Joomla\Database\Monitor\DebugMonitor */
$this->queryMonitor = $this->db->getMonitor();

if (!$this->params->get('queries', 1))
{
// Remove the database driver monitor
$this->db->setMonitor(null);
}

$storagePath = JPATH_CACHE . '/plg_system_debug_' . $this->app->getName();

$this->debugBar = new DebugBar;
$this->debugBar->setStorage(new FileStorage($storagePath));

$this->isAjax = $this->app->input->get('option') === 'com_ajax'
&& $this->app->input->get('plugin') === 'debug' && $this->app->input->get('group') === 'system';

$this->setupLogging();
}

/**
* Add the CSS for debug.
* We can't do this in the constructor because stuff breaks.
Expand All @@ -187,6 +136,8 @@ public function registerListeners()
*/
public function onAfterDispatch()
{
$this->setupPlugin();

// Only if debugging or language debug is enabled.
if ((JDEBUG || $this->debugLang) && $this->isAuthorisedDisplayDebug() && strtolower($this->app->getDocument()->getType()) === 'html')
{
Expand Down Expand Up @@ -215,6 +166,8 @@ public function onAfterDispatch()
*/
public function onAfterRespond()
{
$this->setupPlugin();

// Do not render if debugging or language debug is not enabled.
if (!JDEBUG && !$this->debugLang || $this->isAjax || strtolower($this->app->getDocument()->getType()) !== 'html')
{
Expand Down Expand Up @@ -331,6 +284,8 @@ public function onAfterRespond()
*/
public function onAjaxDebug()
{
$this->setupPlugin();

// Do not render if debugging or language debug is not enabled.
if (!JDEBUG && !$this->debugLang)
{
Expand All @@ -354,6 +309,53 @@ public function onAjaxDebug()
}
}

/**
* Does the setup for the plugin.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
private function setupPlugin()
{
// Check if setup was done already
if ($this->queryMonitor)
{
return;
}

$this->debugLang = $this->app->get('debug_lang');

// Skip the plugin if debug is off
if (!$this->debugLang && !$this->app->get('debug'))
{
return;
}

$this->app->getConfig()->set('gzip', false);
ob_start();
ob_implicit_flush(false);

/** @var \Joomla\Database\Monitor\DebugMonitor */
$this->queryMonitor = $this->db->getMonitor();

if (!$this->params->get('queries', 1))
{
// Remove the database driver monitor
$this->db->setMonitor(null);
}

$storagePath = JPATH_CACHE . '/plg_system_debug_' . $this->app->getName();

$this->debugBar = new DebugBar;
$this->debugBar->setStorage(new FileStorage($storagePath));

$this->isAjax = $this->app->input->get('option') === 'com_ajax'
&& $this->app->input->get('plugin') === 'debug' && $this->app->input->get('group') === 'system';

$this->setupLogging();
}

/**
* Setup logging functionality.
*
Expand Down Expand Up @@ -461,6 +463,8 @@ private function isAuthorisedDisplayDebug(): bool
*/
public function onAfterDisconnect(ConnectionEvent $event)
{
$this->setupPlugin();

if (!JDEBUG)
{
return;
Expand Down

0 comments on commit 6387e5a

Please sign in to comment.