Debug bar

This commit is contained in:
Pierre Rudloff 2021-02-06 15:00:26 +01:00
parent 58f79c5012
commit 5c0ed594f3
9 changed files with 149 additions and 11 deletions

View file

@ -8,6 +8,7 @@ use Alltube\Controller\JsonController;
use Alltube\Exception\ConfigException;
use Alltube\Exception\DependencyException;
use Alltube\Factory\ConfigFactory;
use Alltube\Factory\DebugBarFactory;
use Alltube\Factory\LocaleManagerFactory;
use Alltube\Factory\LoggerFactory;
use Alltube\Factory\SessionFactory;
@ -16,6 +17,7 @@ use Alltube\Middleware\CspMiddleware;
use Alltube\Middleware\LinkHeaderMiddleware;
use Alltube\Middleware\LocaleMiddleware;
use Alltube\Middleware\RouterPathMiddleware;
use DebugBar\DebugBarException;
use Slim\Container;
use SmartyException;
@ -26,6 +28,7 @@ class App extends \Slim\App
* @throws ConfigException
* @throws DependencyException
* @throws SmartyException
* @throws DebugBarException
*/
public function __construct()
{
@ -43,12 +46,17 @@ class App extends \Slim\App
// Locales.
$container['locale'] = LocaleManagerFactory::create($container);
// Smarty.
$container['view'] = ViewFactory::create($container);
// Logger.
$container['logger'] = LoggerFactory::create($container);
if ($container->get('config')->debug) {
// Debug bar.
$container['debugbar'] = DebugBarFactory::create($container);
}
// Smarty.
$container['view'] = ViewFactory::create($container);
// Middlewares.
$this->add(new LocaleMiddleware($container));
$this->add(new CspMiddleware($container));

View file

@ -0,0 +1,46 @@
<?php
namespace Alltube\Factory;
use DebugBar\DataCollector\ConfigCollector;
use DebugBar\DataCollector\MemoryCollector;
use DebugBar\DataCollector\MessagesCollector;
use DebugBar\DataCollector\PhpInfoCollector;
use DebugBar\DataCollector\RequestDataCollector;
use DebugBar\DebugBar;
use DebugBar\DebugBarException;
use Slim\Container;
/**
* Class DebugBarFactory
* @package Alltube\Factory
*/
class DebugBarFactory
{
/**
* @param Container $container
* @return DebugBar
* @throws DebugBarException
*/
public static function create(Container $container): DebugBar
{
$debugBar = new DebugBar();
$requestCollector = new RequestDataCollector();
$configCollector = new ConfigCollector(get_object_vars($container->get('config')));
$debugBar->addCollector(new PhpInfoCollector())
->addCollector(new MessagesCollector())
->addCollector($requestCollector)
->addCollector(new MemoryCollector())
->addCollector($configCollector);
$container->get('logger')->add('debugbar', $debugBar->getCollector('messages'));
$requestCollector->useHtmlVarDumper();
$configCollector->useHtmlVarDumper();
return $debugBar;
}
}

View file

@ -3,6 +3,7 @@
namespace Alltube\Factory;
use Consolidation\Log\Logger;
use Consolidation\Log\LoggerManager;
use Consolidation\Log\LogOutputStyler;
use Slim\Container;
use Symfony\Component\Console\Output\ConsoleOutput;
@ -16,9 +17,9 @@ class LoggerFactory
/**
* @param Container $container
* @return Logger
* @return LoggerManager
*/
public static function create(Container $container): Logger
public static function create(Container $container): LoggerManager
{
$config = $container->get('config');
if ($config->debug) {
@ -27,9 +28,13 @@ class LoggerFactory
$verbosity = ConsoleOutput::VERBOSITY_NORMAL;
}
$loggerManager = new LoggerManager();
$logger = new Logger(new ConsoleOutput($verbosity));
$logger->setLogOutputStyler(new LogOutputStyler());
return $logger;
$loggerManager->add('default', $logger);
return $loggerManager;
}
}

View file

@ -85,6 +85,14 @@ class ViewFactory
$view->offsetSet('config', $container->get('config'));
$view->offsetSet('domain', $uri->withBasePath('')->getBaseUrl());
if ($container->has('debugbar')) {
$view->offsetSet(
'debug_render',
$container->get('debugbar')
->getJavascriptRenderer($uri->getBaseUrl() . '/vendor/maximebf/debugbar/src/DebugBar/Resources/')
);
}
return $view;
}
}

View file

@ -37,7 +37,8 @@ class CspMiddleware
public function applyHeader(Response $response): MessageInterface
{
$csp = new CSPBuilder();
$csp->addDirective('default-src', [])
$csp->disableOldBrowserSupport()
->addDirective('default-src', [])
->addDirective('font-src', ['self' => true])
->addDirective('style-src', ['self' => true])
->addDirective('manifest-src', ['self' => true])
@ -47,9 +48,10 @@ class CspMiddleware
->addSource('img-src', '*');
if ($this->config->debug) {
// So symfony/debug and symfony/error-handler can work.
$csp->setDirective('script-src', ['unsafe-inline' => true])
->setDirective('style-src', ['self' => true, 'unsafe-inline' => true]);
// So maximebf/debugbar, symfony/debug and symfony/error-handler can work.
$csp->setDirective('script-src', ['self' => true, 'unsafe-inline' => true])
->setDirective('style-src', ['self' => true, 'unsafe-inline' => true])
->addSource('img-src', 'data:');
}
return $csp->injectCSPHeader($response);

View file

@ -44,6 +44,7 @@
"enlightn/security-checker": "^1.4",
"ergebnis/composer-normalize": "^2.6",
"insite/composer-dangling-locked-deps": "^0.2.1",
"maximebf/debugbar": "^1.16",
"php-mock/php-mock-mockery": "^1.3",
"phpro/grumphp": "^1.1",
"phpstan/phpstan": "^0.12.25",

63
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "aacbcefea720bab616e7236caa0ae3e7",
"content-hash": "8f0982143c6c873f68d41017e195387a",
"packages": [
{
"name": "aura/session",
@ -4181,6 +4181,67 @@
],
"time": "2019-12-17T07:42:37+00:00"
},
{
"name": "maximebf/debugbar",
"version": "v1.16.5",
"source": {
"type": "git",
"url": "https://github.com/maximebf/php-debugbar.git",
"reference": "6d51ee9e94cff14412783785e79a4e7ef97b9d62"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/maximebf/php-debugbar/zipball/6d51ee9e94cff14412783785e79a4e7ef97b9d62",
"reference": "6d51ee9e94cff14412783785e79a4e7ef97b9d62",
"shasum": ""
},
"require": {
"php": "^7.1|^8",
"psr/log": "^1.0",
"symfony/var-dumper": "^2.6|^3|^4|^5"
},
"require-dev": {
"phpunit/phpunit": "^7.5.20 || ^9.4.2"
},
"suggest": {
"kriswallsmith/assetic": "The best way to manage assets",
"monolog/monolog": "Log using Monolog",
"predis/predis": "Redis storage"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.16-dev"
}
},
"autoload": {
"psr-4": {
"DebugBar\\": "src/DebugBar/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Maxime Bouroumeau-Fuseau",
"email": "maxime.bouroumeau@gmail.com",
"homepage": "http://maximebf.com"
},
{
"name": "Barry vd. Heuvel",
"email": "barryvdh@gmail.com"
}
],
"description": "Debug bar in the browser for php application",
"homepage": "https://github.com/maximebf/php-debugbar",
"keywords": [
"debug",
"debugbar"
],
"time": "2020-12-07T11:07:24+00:00"
},
{
"name": "mockery/mockery",
"version": "1.2.2",

View file

@ -37,5 +37,8 @@
</div>
</footer>
</div>
{if isset($debug_render)}
{$debug_render->render()}
{/if}
</body>
</html>

View file

@ -22,6 +22,10 @@
<meta name="theme-color" content="#4F4F4F"/>
<link rel="manifest" href="{base_url}/resources/manifest.json"/>
<meta name="generator" content="AllTube Download ({$config->getAppVersion()})"/>
{if isset($debug_render)}
{$debug_render->renderHead()}
{/if}
</head>
<body>
<div class="page {$class}">