diff --git a/classes/App.php b/classes/App.php index a2b8434..551a2fa 100644 --- a/classes/App.php +++ b/classes/App.php @@ -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)); diff --git a/classes/Factory/DebugBarFactory.php b/classes/Factory/DebugBarFactory.php new file mode 100644 index 0000000..adf3286 --- /dev/null +++ b/classes/Factory/DebugBarFactory.php @@ -0,0 +1,46 @@ +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; + } +} diff --git a/classes/Factory/LoggerFactory.php b/classes/Factory/LoggerFactory.php index fe85133..da52c1d 100644 --- a/classes/Factory/LoggerFactory.php +++ b/classes/Factory/LoggerFactory.php @@ -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; } } diff --git a/classes/Factory/ViewFactory.php b/classes/Factory/ViewFactory.php index 1cc09e2..ffb1a7b 100644 --- a/classes/Factory/ViewFactory.php +++ b/classes/Factory/ViewFactory.php @@ -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; } } diff --git a/classes/Middleware/CspMiddleware.php b/classes/Middleware/CspMiddleware.php index 3449df3..88ecccf 100644 --- a/classes/Middleware/CspMiddleware.php +++ b/classes/Middleware/CspMiddleware.php @@ -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); diff --git a/composer.json b/composer.json index 9f2b878..884a1d3 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 540be18..d42376a 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/templates/inc/footer.tpl b/templates/inc/footer.tpl index 322e3e8..fe770c2 100644 --- a/templates/inc/footer.tpl +++ b/templates/inc/footer.tpl @@ -37,5 +37,8 @@ +{if isset($debug_render)} + {$debug_render->render()} +{/if} diff --git a/templates/inc/head.tpl b/templates/inc/head.tpl index d214316..4e5d68d 100644 --- a/templates/inc/head.tpl +++ b/templates/inc/head.tpl @@ -22,6 +22,10 @@ + + {if isset($debug_render)} + {$debug_render->renderHead()} + {/if}