Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jul 27, 2023
1 parent eecf9a4 commit 00f7172
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ is due to the way Vagrant and Virtualbox configure networking by default.
The DebugBar will include its own version of jQuery by default. It will only be disabled
in the admin (which already use jQuery).

If you have added jQuery in your requirements (filename must be jquery.js or jquery.min.js),
the DebugBar will not load its own jQuery version. You can also set the following
If you have added jQuery in your requirements the DebugBar will not load its own jQuery
version (if the filename is jquery.js or jquery.min.js). You can also set the following
configuration flag to false to prevent the DebugBar from including its own jQuery.

```yaml
Expand All @@ -284,7 +284,7 @@ protected function init()
}
```

When using simple theme, you probably want to disable the jquery from the cdn.
When using the simple theme, you probably want to disable the jquery from the cdn.

```php
protected function init()
Expand Down
35 changes: 18 additions & 17 deletions code/DebugBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Exception;
use Monolog\Logger;
use ReflectionObject;
use SilverStripe\ORM\DB;
use Psr\Log\LoggerInterface;
use SilverStripe\Core\Kernel;
use DebugBar\JavascriptRenderer;
Expand All @@ -23,12 +22,9 @@
use SilverStripe\Core\Config\ConfigLoader;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\Connect\PDOConnector;
use DebugBar\DataCollector\MemoryCollector;
use LeKoala\DebugBar\Messages\LogFormatter;
use SilverStripe\Admin\AdminRootController;
use DebugBar\DataCollector\PDO\PDOCollector;
use DebugBar\DataCollector\PDO\TraceablePDO;
use SilverStripe\Core\Manifest\ModuleLoader;
use DebugBar\DataCollector\MessagesCollector;
use LeKoala\DebugBar\Bridge\MonologCollector;
Expand All @@ -46,9 +42,7 @@
use LeKoala\DebugBar\Collector\SilverStripeCollector;
use SilverStripe\Config\Collections\DeltaConfigCollection;
use SilverStripe\Config\Collections\CachedConfigCollection;
use LeKoala\DebugBar\Bridge\SymfonyMailer\MailerEventListener;
use LeKoala\DebugBar\Bridge\SymfonyMailer\SymfonyMailerCollector;
use LeKoala\DebugBar\Bridge\SymfonyMailer\SymfonyMailerLogCollector;

/**
* A simple helper
Expand Down Expand Up @@ -303,20 +297,21 @@ public static function moduleResource($path)

/**
* Include DebugBar assets using Requirements API
* This needs to be called before the template is rendered otherwise the calls to the Requirements API are ignored
*
* @return void
* @return bool
*/
public static function includeRequirements()
{
$debugbar = self::getDebugBar();

if (!$debugbar) {
return;
return false;
}

// Already called
if (self::$renderer) {
return;
return false;
}

$renderer = $debugbar->getJavascriptRenderer();
Expand Down Expand Up @@ -353,16 +348,23 @@ public static function includeRequirements()
}

foreach ($renderer->getAssets('css') as $cssFile) {
Requirements::css(Director::makeRelative(ltrim($cssFile, '/')));
Requirements::css(self::replaceAssetPath($cssFile));
}

foreach ($renderer->getAssets('js') as $jsFile) {
Requirements::javascript(Director::makeRelative(ltrim($jsFile, '/')), [
'type' => 'text/javascript'
Requirements::javascript(self::replaceAssetPath($jsFile), [
'type' => 'application/javascript'
]);
}

self::$renderer = $renderer;

return true;
}

protected static function replaceAssetPath($file)
{
return Director::makeRelative(str_replace('\\', '/', ltrim($file, '/')));
}

/**
Expand All @@ -383,13 +385,13 @@ public static function renderDebugBar()
}
}

// Requirements may have been cleared (CMS iframes...) or not set (Security...)
// Requirements may have been cleared (CMS iframes...) or not set
$js = Requirements::backend()->getJavascript();
$debugBarResource = self::moduleResource('assets/debugbar.js');
$path = $debugBarResource->getRelativePath();

// Url in getJavascript has a / slash, so fix if necessary
$path = str_replace("assets\\debugbar.js", "assets/debugbar.js", $path);
$path = str_replace("\\", "/", $path);
if (!array_key_exists($path, $js)) {
return;
}
Expand All @@ -401,11 +403,10 @@ public static function renderDebugBar()
// Normally deprecation notices are output in a shutdown function, which runs well after debugbar has rendered.
// This ensures the deprecation notices which have been noted up to this point are logged out and collected by
// the MonologCollector.
if (method_exists(Deprecation::class, 'outputNotices')) {
Deprecation::outputNotices();
}
Deprecation::outputNotices();

$script = self::$renderer->render($initialize);

return $script;
}

Expand Down
7 changes: 1 addition & 6 deletions code/Extension/ControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use LeKoala\DebugBar\DebugBar;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Extension;
use SilverStripe\Security\Security;

/**
* A controller extension to log times and render the Debug Bar
Expand Down Expand Up @@ -36,11 +35,7 @@ public function onBeforeInit()

public function onAfterInit()
{
// On Security, onAfterInit is called before init() in your Page method
// jQuery is most likely not included yet
if (!$this->owner instanceof Security) {
DebugBar::includeRequirements();
}
$result = DebugBar::includeRequirements();

DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugbar) {
// Add the headers Collector
Expand Down

0 comments on commit 00f7172

Please sign in to comment.