Skip to content

Commit

Permalink
make siteconfig, admin et db connection optionnal
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jan 13, 2021
1 parent 1df17c9 commit 412a6c2
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 29 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -128,6 +128,8 @@ There is a variety of other useful information available via various tabs and in
* **Parameters:** Displays all GET, POST and REQUEST parameters from the current request
* **Config:** Displays a list of the current [SiteConfig](https://github.com/silverstripe/silverstripe-siteconfig) settings from the CMS
* **Requirements:** Shows a list of all [`Requirements`](https://docs.silverstripe.org/en/developer_guides/templates/requirements/) calls made during a page's execution
* **Mails:** Shows a list of all emails sent
* **Headers:** Shows a list of all headers

#### Indicators

Expand Down Expand Up @@ -214,6 +216,7 @@ LeKoala\DebugBar\DebugBar:
| `warn_request_time_seconds` | int | Threshold (seconds) for what constitutes a *dangerously* long page request (upper limit) |
| `warn_warning_ratio` | float | Ratio to divide the warning request time by to get the *warning* level (default 0.5) |
| `show_namespace` | bool | Show the fully qualified namespace in the Database tab when set to true. Defaults to false |
| `db_collector` | bool | Show the db tab. Defaults to true |
| `config_collector` | bool | Show the config tab. Defaults to true |
| `partial_cache_collector` | bool | Show the partial cache tab. Defaults to true |
| `email_collector` | bool | Show the email tab. Defaults to true |
Expand Down
11 changes: 8 additions & 3 deletions _config/debugbar.yml
Expand Up @@ -36,6 +36,7 @@ LeKoala\DebugBar\DebugBar:
show_namespaces: false
# Enable or disable collectors below
config_collector: true
db_collector: true
partial_cache_collector: true
email_collector: true
header_collector: true
Expand All @@ -51,12 +52,16 @@ SilverStripe\Core\Injector\Injector:
properties:
Middlewares:
DebugBarMiddleware: '%$DebugBarMiddleware'
SilverStripe\Admin\LeftAndMain:
extensions:
- LeKoala\DebugBar\Extension\LeftAndMainExtension
SilverStripe\Control\Controller:
extensions:
- LeKoala\DebugBar\Extension\ControllerExtension
TractorCow\SilverStripeProxyDB\ProxyDBFactory:
extensions:
- LeKoala\DebugBar\Extension\ProxyDBExtension
---
Only:
moduleexists: silverstripe/admin
---
SilverStripe\Admin\LeftAndMain:
extensions:
- LeKoala\DebugBar\Extension\LeftAndMainExtension
23 changes: 15 additions & 8 deletions code/Collector/SilverStripeCollector.php
Expand Up @@ -30,7 +30,7 @@ public function collect()
'session' => self::getSessionData(),
'config' => self::getConfigData(),
'locale' => i18n::get_locale(),
'version' => LeftAndMain::create()->CMSVersion(),
'version' => class_exists(LeftAndMain::class) ? LeftAndMain::create()->CMSVersion() : 'unknown',
'cookies' => self::getCookieData(),
'parameters' => self::getRequestParameters(),
'requirements' => self::getRequirementsData(),
Expand Down Expand Up @@ -123,6 +123,9 @@ public static function getSessionData()

public static function getConfigData()
{
if (!class_exists(SiteConfig::class)) {
return [];
}
return SiteConfig::current_site_config()->toMap();
}

Expand Down Expand Up @@ -177,6 +180,7 @@ public function getWidgets()
$userIcon = 'user';
$userText = 'Logged in as ' . $memberTag;

// TODO: upgrade to newer version of the module
// Masquerade integration
if (DebugBar::getRequest()->getSession()->get('Masquerade.Old.loggedInAs')) {
$userIcon = 'user-secret';
Expand All @@ -192,7 +196,7 @@ public function getWidgets()
),
"version" => array(
"icon" => "hashtag",
"tooltip" => LeftAndMain::create()->CMSVersion(),
"tooltip" => class_exists(LeftAndMain::class) ? LeftAndMain::create()->CMSVersion() : 'unknown',
"default" => ""
),
"locale" => array(
Expand All @@ -218,12 +222,6 @@ public function getWidgets()
"map" => "$name.parameters",
"default" => "{}"
),
"SiteConfig" => array(
"icon" => "sliders",
"widget" => "PhpDebugBar.Widgets.VariableListWidget",
"map" => "$name.config",
"default" => "{}"
),
"requirements" => array(
"icon" => "file-text-o",
"widget" => "PhpDebugBar.Widgets.ListWidget",
Expand All @@ -242,6 +240,15 @@ public function getWidgets()
)
);

if (!empty($this->getConfigData())) {
$widgets["SiteConfig"] = array(
"icon" => "sliders",
"widget" => "PhpDebugBar.Widgets.VariableListWidget",
"map" => "$name.config",
"default" => "{}"
);
}

if (!empty(self::$debug)) {
$widgets["debug"] = array(
"icon" => "list-alt",
Expand Down
31 changes: 19 additions & 12 deletions code/DebugBar.php
@@ -1,4 +1,5 @@
<?php

namespace LeKoala\DebugBar;

use DebugBar\JavascriptRenderer;
Expand Down Expand Up @@ -148,18 +149,20 @@ public static function initDebugBar()
}
}

$connector = DB::get_connector();
if (!self::config()->get('force_proxy') && $connector instanceof PDOConnector) {
// Use a little bit of magic to replace the pdo instance
$refObject = new ReflectionObject($connector);
$refProperty = $refObject->getProperty('pdoConnection');
$refProperty->setAccessible(true);
$traceablePdo = new TraceablePDO($refProperty->getValue($connector));
$refProperty->setValue($connector, $traceablePdo);
if (self::config()->db_collector) {
$connector = DB::get_connector();
if (!self::config()->get('force_proxy') && $connector instanceof PDOConnector) {
// Use a little bit of magic to replace the pdo instance
$refObject = new ReflectionObject($connector);
$refProperty = $refObject->getProperty('pdoConnection');
$refProperty->setAccessible(true);
$traceablePdo = new TraceablePDO($refProperty->getValue($connector));
$refProperty->setValue($connector, $traceablePdo);

$debugbar->addCollector(new PDOCollector($traceablePdo));
} else {
$debugbar->addCollector(new DatabaseCollector);
$debugbar->addCollector(new PDOCollector($traceablePdo));
} else {
$debugbar->addCollector(new DatabaseCollector);
}
}

// Add message collector last so other collectors can send messages to the console using it
Expand Down Expand Up @@ -449,7 +452,11 @@ public static function isDevUrl()
public static function isAdminUrl()
{
$baseUrl = rtrim(BASE_URL, '/');
$adminUrl = AdminRootController::config()->get('url_base');
if (class_exists(AdminRootController::class)) {
$adminUrl = AdminRootController::config()->get('url_base');
} else {
$adminUrl = 'admin';
}

return strpos(self::getRequestUrl(), $baseUrl . '/' . $adminUrl . '/') === 0;
}
Expand Down
6 changes: 2 additions & 4 deletions code/Extension/LeftAndMainExtension.php
Expand Up @@ -3,14 +3,12 @@
namespace LeKoala\DebugBar\Extension;

use LeKoala\DebugBar\DebugBar;
use SilverStripe\Admin\LeftAndMainExtension as BaseLeftAndMainExtension;
use SilverStripe\Core\Extension;

/**
* Description of DebugBarLeftAndMainExtension
*
* @author Koala
*/
class LeftAndMainExtension extends BaseLeftAndMainExtension
class LeftAndMainExtension extends Extension
{
public function accessedCMS()
{
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -18,13 +18,13 @@
"require": {
"php": ">=5.6",
"silverstripe/framework": "^4.0",
"silverstripe/siteconfig": "^4.0",
"silverstripe/admin": "^1.0",
"maximebf/debugbar": "^1.13",
"jdorn/sql-formatter": "1.3.x-dev",
"tractorcow/silverstripe-proxy-db": "^0.1.0"
},
"require-dev": {
"silverstripe/siteconfig": "^4.0",
"silverstripe/admin": "^1.0",
"sminnee/phpunit": "^5.7",
"squizlabs/php_codesniffer": "^3.0"
},
Expand Down

0 comments on commit 412a6c2

Please sign in to comment.