Skip to content

Commit

Permalink
Merge pull request #9 from wintercms/wip/merge-upstream
Browse files Browse the repository at this point in the history
Merge upstream changes, customise CSS
  • Loading branch information
LukeTowers authored Aug 2, 2022
2 parents b98b569 + 06039bb commit ddbba80
Show file tree
Hide file tree
Showing 18 changed files with 1,974 additions and 1,896 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
MIT License

Copyright (c) 2015-2020 Scott Bedard
Copyright (c) 2020 Luke Towers
Copyright (c) 2020 October CMS
Copyright (c) 2021 Winter CMS

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
195 changes: 154 additions & 41 deletions Plugin.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
<?php namespace Winter\Debugbar;

use Event;
use Config;
use Backend\Classes\Controller as BackendController;
use Backend\Models\UserRole;
use System\Classes\PluginBase;
use System\Classes\CombineAssets;
use Barryvdh\Debugbar\Facades\Debugbar;
use Barryvdh\Debugbar\LaravelDebugbar;
use Barryvdh\Debugbar\SymfonyHttpDriver;
use Cms\Classes\Controller as CmsController;
use Cms\Classes\Layout;
use Cms\Classes\Page;
use Config;
use Event;
use Illuminate\Contracts\Http\Kernel as HttpKernelContract;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Session\SessionManager;
use System\Classes\CombineAssets;
use System\Classes\PluginBase;
use Twig\Extension\ProfilerExtension;
use Twig\Profiler\Profile;
use Winter\Debugbar\Classes\WinterDebugbar;
use Winter\Debugbar\Collectors\BackendCollector;
use Winter\Debugbar\Collectors\CmsCollector;
use Winter\Debugbar\Collectors\ComponentsCollector;
use Winter\Debugbar\Collectors\ModelsCollector;

/**
* Debugbar Plugin Information File
*
* TODO:
* - Fix styling by scoping a html reset to phpdebugbar-openhandler and phpdebugbar
* Winter.Debugbar Plugin
*/
class Plugin extends PluginBase
{
Expand All @@ -22,7 +35,6 @@ class Plugin extends PluginBase

/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
Expand All @@ -33,59 +45,160 @@ public function pluginDetails()
'author' => 'Winter CMS',
'icon' => 'icon-bug',
'homepage' => 'https://github.com/wintercms/wn-debugbar-plugin',
'replaces' => ['RainLab.Debugbar' => '<= 3.1.1'],
'replaces' => ['RainLab.Debugbar' => '<= 3.3.2'],
];
}

/**
* Register service provider, Twig extensions, and alias facade.
* Registers the plugin
*/
public function register()
{
// Provide the winter.debugbar config under the debugbar namespace
Config::registerNamespaceAlias('winter.debugbar', 'debugbar');

// Register the service provider
$this->app->register(\Winter\Debugbar\Classes\ServiceProvider::class);

// Replace the LaravelDebugbar with the WinterDebugbar
$this->app->singleton(LaravelDebugbar::class, function ($app) {
$debugbar = new WinterDebugbar($app);

if ($app->bound(SessionManager::class)) {
$sessionManager = $app->make(SessionManager::class);
$httpDriver = new SymfonyHttpDriver($sessionManager);
$debugbar->setHttpDriver($httpDriver);
}

return $debugbar;
});

// Register alias
$alias = AliasLoader::getInstance();
$alias->alias('Debugbar', Debugbar::class);

// Register the asset bundle
CombineAssets::registerCallback(function ($combiner) {
$combiner->registerBundle('$/winter/debugbar/assets/less/debugbar.less');
});
}

/**
* Boots service provider, Twig extensions, and alias facade, and injects collectors and resources.
*/
public function boot()
{
// Disabled by config, halt
if (Config::get('debugbar.enabled') === false) {
return;
}

// Register middleware
if (Config::get('app.debugAjax', false)) {
$this->app['Illuminate\Contracts\Http\Kernel']->pushMiddleware('\Winter\Debugbar\Middleware\InterpretsAjaxExceptions');
$this->app[HttpKernelContract::class]->pushMiddleware(\Winter\Debugbar\Middleware\InterpretsAjaxExceptions::class);
}

// Add styling
$addResources = function ($controller) {
$debugBar = $this->app->make('Barryvdh\Debugbar\LaravelDebugbar');
if ($debugBar->isEnabled()) {
$controller->addCss(url(Config::get('cms.pluginsPath', '/plugins') . '/winter/debugbar/assets/css/debugbar.css'));
}
};
Event::listen('backend.page.beforeDisplay', $addResources, PHP_INT_MAX);
Event::listen('cms.page.beforeDisplay', $addResources, PHP_INT_MAX);
// Register custom collectors
if ($this->app->runningInBackend()) {
$this->addBackendCollectors();
} else {
$this->registerCmsTwigExtensions();
$this->addFrontendCollectors();
}

Event::listen('cms.page.beforeDisplay', function ($controller, $url, $page) {
// Twig extensions
$twig = $controller->getTwig();
if (!$twig->hasExtension(\Winter\DebugBar\Twig\Extension\Debug::class)) {
$twig->addExtension(new \Winter\DebugBar\Twig\Extension\Debug($this->app));
$twig->addExtension(new \Winter\DebugBar\Twig\Extension\Stopwatch($this->app));
}
});
$this->addGlobalCollectors();
}

/**
* Register the plugin
* Add globally available collectors
*/
public function register()
public function addGlobalCollectors()
{
// Configure the debugbar
Config::set('debugbar', Config::get('winter.debugbar::config'));
if (Config::get('debugbar.collectors.models', true)) {
/** @var \Barryvdh\Debugbar\LaravelDebugbar $debugBar */
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);
$modelsCollector = $this->app->make(ModelsCollector::class);
$debugBar->addCollector($modelsCollector);
}
}

// Register the debugbar serviceprovider
$this->app->register(\Winter\Debugbar\Classes\ServiceProvider::class);
/**
* Add collectors used by the frontend only
*/
public function addFrontendCollectors()
{
/** @var \Barryvdh\Debugbar\LaravelDebugbar $debugBar */
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);

// Register alias
$alias = AliasLoader::getInstance();
$alias->alias('Debugbar', '\Barryvdh\Debugbar\Facade');
if (Config::get('debugbar.collectors.cms', true)) {
// Disable route collector as the CMS collector presents this info instead
Config::set('debugbar.collectors.route', false);

// Register asset bundles
CombineAssets::registerCallback(function ($combiner) {
$combiner->registerBundle('$/winter/debugbar/assets/css/debugbar.less');
Event::listen('cms.page.beforeDisplay', function (CmsController $controller, $url, ?Page $page) use ($debugBar) {
if ($page) {
$collector = new CmsCollector($controller, $url, $page);
if (!$debugBar->hasCollector($collector->getName())) {
$debugBar->addCollector($collector);
}
}
});
}

if (Config::get('debugbar.collectors.components', true)) {
Event::listen('cms.page.initComponents', function (CmsController $controller, ?Page $page, ?Layout $layout) use ($debugBar) {
if ($page) {
$collector = new ComponentsCollector($controller, $page, $layout);
if (!$debugBar->hasCollector($collector->getName())) {
$debugBar->addCollector($collector);
}
}
});
}
}

/**
* Add collectors used by the Backend only
*/
public function addBackendCollectors()
{
/** @var \Barryvdh\Debugbar\LaravelDebugbar $debugBar */
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);

if (Config::get('debugbar.collectors.backend', true)) {
Event::listen('backend.page.beforeDisplay', function (BackendController $controller, $action, array $params) use ($debugBar) {
$collector = new BackendCollector($controller, $action, $params);
if (!$debugBar->hasCollector($collector->getName())) {
$debugBar->addCollector($collector);
}
});
}
}

/**
* Registers extensions in the CMS Twig environment
*/
protected function registerCmsTwigExtensions()
{
$profile = new Profile;
$debugBar = $this->app->make(\Barryvdh\Debugbar\LaravelDebugbar::class);

Event::listen('cms.page.beforeDisplay', function ($controller, $url, $page) use ($profile, $debugBar) {
$twig = $controller->getTwig();
if (!$twig->hasExtension(\Winter\DebugBar\Twig\Extension\Debug::class)) {
$twig->addExtension(new \Winter\DebugBar\Twig\Extension\Debug($this->app));
$twig->addExtension(new \Winter\DebugBar\Twig\Extension\Stopwatch($this->app));
}

if (!$twig->hasExtension(ProfilerExtension::class)) {
$twig->addExtension(new ProfilerExtension($profile));
}
});

if (class_exists(\DebugBar\Bridge\NamespacedTwigProfileCollector::class)) {
$debugBar->addCollector(new \DebugBar\Bridge\NamespacedTwigProfileCollector($profile));
} else {
$debugBar->addCollector(new \DebugBar\Bridge\TwigProfileCollector($profile));
}
}

/**
Expand Down
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ Easily see what's going on under the hood of your Winter CMS application.

# Installation

To install from the [Marketplace](https://github.com/wintercms/wn-debugbar-plugin), click on the "Add to Project" button and then select the project you wish to add it to and pay for the plugin. Once the plugin has been added to the project, go to the backend and check for updates to pull in the plugin.
To install with Composer, run the following command from your project folder:

To install from the backend, go to **Settings -> Updates & Plugins -> Install Plugins** and then search for `Winter.DebugBar`.

To install from [the repository](https://github.com/wintercms/wn-debugbar-plugin), clone it into **plugins/winter/debugbar** and then run `composer update` from your project root in order to pull in the dependencies.

To install it with Composer, run `composer require winter/wn-debugbar-plugin` from your project root.
```bash
composer require winter/wn-debugbar-plugin
```

### Usage

Set `debug` to `true` in `config/app.php`, and the debugbar should appear on your site to all authenticated backend users with the `winter.debugbar.access_debugbar` permission. If you would like to make the debugbar accessible to all users regardless of authentication & permissions, then set `allow_public_access` to `true` in `config/winter/debugbar/config.php`.
Set `debug` to `true` in `config/app.php` and the debugbar should appear on your site to all authenticated backend users with the `winter.debugbar.access_debugbar` permission.

If you would like to make the debug bar accessible to all users, regardless of authentication and permissions, set `allow_public_access` to `true` in the configuration file.

See [barryvdh/laravel-debugbar](https://github.com/barryvdh/laravel-debugbar) for more usage instructions and documentation.

To include exceptions in the response header of ajax calls set `debugAjax` to `true` in `config/app.php`.
### Configuration

All configuration for the plugin is found in the `plugins/winter/debugbar` directory. To override any of these settings, create an override file called `config/winter/debugbar/config.php` in your local system.

To include exceptions in the response header of AJAX calls, set `debugAjax` to `true` in `config/app.php`.

Events are not captured by default since it can slow down the front-end when many events are fired, you may enable it with the `collectors.events` setting.
Loading

0 comments on commit ddbba80

Please sign in to comment.