Processor cleanup, moved logRequest() and debuggerRequest() into Debugger class

This commit is contained in:
Matias Griese
2019-06-03 10:44:10 +03:00
parent 9738c55633
commit 115bdb7e10
14 changed files with 87 additions and 107 deletions

View File

@@ -30,7 +30,9 @@ use DebugBar\JavascriptRenderer;
use DebugBar\StandardDebugBar;
use Grav\Common\Config\Config;
use Grav\Common\Processors\ProcessorInterface;
use Grav\Framework\Psr7\Response;
use Monolog\Logger;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use RocketTheme\Toolbox\Event\Event;
@@ -249,6 +251,54 @@ class Debugger
}
public function debuggerRequest(RequestInterface $request): Response
{
$clockwork = $this->clockwork;
$headers = [
'Content-Type' => 'application/json',
'Grav-Internal-SkipShutdown' => 1
];
$path = $request->getUri()->getPath();
$clockworkDataUri = '#/__clockwork(?:/(?<id>[0-9-]+))?(?:/(?<direction>(?:previous|next)))?(?:/(?<count>\d+))?#';
if (preg_match($clockworkDataUri, $path, $matches) === false) {
$response = ['message' => 'Bad Input'];
return new Response(400, $headers, json_encode($response));
}
$id = $matches['id'] ?? null;
$direction = $matches['direction'] ?? null;
$count = $matches['count'] ?? null;
$storage = $clockwork->getStorage();
if ($direction === 'previous') {
$data = $storage->previous($id, $count);
} elseif ($direction === 'next') {
$data = $storage->next($id, $count);
} elseif ($id === 'latest') {
$data = $storage->latest();
} else {
$data = $storage->find($id);
}
if (preg_match('#(?<id>[0-9-]+|latest)/extended#', $path)) {
$clockwork->extendRequest($data);
}
if (!$data) {
$response = ['message' => 'Not Found'];
return new Response(404, $headers, json_encode($response));
}
$data = is_array($data) ? array_map(function ($item) { return $item->toArray(); }, $data) : $data->toArray();
return new Response(200, $headers, json_encode($data));
}
protected function addMeasures()
{
if (!$this->enabled) {

View File

@@ -18,7 +18,7 @@ class AssetsProcessor extends ProcessorBase
public $id = '_assets';
public $title = 'Assets';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer();
$this->container['assets']->init();

View File

@@ -18,7 +18,7 @@ class BackupsProcessor extends ProcessorBase
public $id = '_backups';
public $title = 'Backups';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer();
$backups = $this->container['backups'];

View File

@@ -19,7 +19,7 @@ class DebuggerAssetsProcessor extends ProcessorBase
public $id = 'debugger_assets';
public $title = 'Debugger Assets';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer();
$this->container['debugger']->addAssets();

View File

@@ -9,9 +9,6 @@
namespace Grav\Common\Processors;
use Clockwork\Clockwork;
use Clockwork\DataSource\PsrMessageDataSource;
use Clockwork\Helpers\ServerTiming;
use Grav\Common\Config\Config;
use Grav\Common\Debugger;
use Grav\Common\Uri;
@@ -20,7 +17,6 @@ use Grav\Framework\Psr7\Response;
use Grav\Framework\Session\Exceptions\SessionException;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\SyslogHandler;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
@@ -30,23 +26,16 @@ class InitializeProcessor extends ProcessorBase
public $id = '_init';
public $title = 'Initialize';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer('_config', 'Configuration');
$config = $this->initializeConfig();
$this->stopTimer('_config');
$this->startTimer('_logger', 'Logger');
$this->initializeLogger($config);
$this->stopTimer('_logger');
$this->startTimer('_errors', 'Error Handlers Reset');
$this->initializeErrors();
$this->stopTimer('_errors');
$this->startTimer('_debugger', 'Init Debugger');
/** @var Debugger $debugger */
$debugger = $this->container['debugger']->init();
// Clockwork integration.
$clockwork = $debugger->getClockwork();
if ($clockwork) {
$server = $request->getServerParams();
@@ -63,17 +52,14 @@ class InitializeProcessor extends ProcessorBase
// Handle clockwork API calls.
$uri = $request->getUri();
if (mb_strpos($uri->getPath(), $baseUri . '/__clockwork/') === 0) {
return $this->debuggerRequest($request, $clockwork);
return $debugger->debuggerRequest($request);
}
$this->container['clockwork'] = $clockwork;
}
$this->stopTimer('_debugger');
$this->startTimer('_init', 'Initialize');
$this->initialize($config);
$this->stopTimer('_init');
$this->initializeSession($config);
// Wrap call to next handler so that debugger can profile it.
@@ -85,80 +71,10 @@ class InitializeProcessor extends ProcessorBase
return $debugger->logRequest($request, $response);
}
protected function logRequest(ServerRequestInterface $request, ResponseInterface $response, Clockwork $clockwork = null)
{
if (!$clockwork) {
return $response;
}
$clockwork->getTimeline()->finalize($request->getAttribute('request_time'));
$clockwork->addDataSource(new PsrMessageDataSource($request, $response));
$clockwork->resolveRequest();
$clockwork->storeRequest();
$clockworkRequest = $clockwork->getRequest();
$response = $response
->withHeader('X-Clockwork-Id', $clockworkRequest->id)
->withHeader('X-Clockwork-Version', $clockwork::VERSION);
$basePath = $request->getAttribute('base_uri');
if ($basePath) {
$response = $response->withHeader('X-Clockwork-Path', $basePath . '/__clockwork/');
}
return $response->withHeader('Server-Timing', ServerTiming::fromRequest($clockworkRequest)->value());
}
protected function debuggerRequest(RequestInterface $request, Clockwork $clockwork): Response
{
$headers = [
'Content-Type' => 'application/json',
'Grav-Internal-SkipShutdown' => 1
];
$path = $request->getUri()->getPath();
$clockworkDataUri = '#/__clockwork(?:/(?<id>[0-9-]+))?(?:/(?<direction>(?:previous|next)))?(?:/(?<count>\d+))?#';
if (preg_match($clockworkDataUri, $path, $matches) === false) {
$response = ['message' => 'Bad Input'];
return new Response(400, $headers, json_encode($response));
}
$id = $matches['id'] ?? null;
$direction = $matches['direction'] ?? null;
$count = $matches['count'] ?? null;
$storage = $clockwork->getStorage();
if ($direction === 'previous') {
$data = $storage->previous($id, $count);
} elseif ($direction === 'next') {
$data = $storage->next($id, $count);
} elseif ($id === 'latest') {
$data = $storage->latest();
} else {
$data = $storage->find($id);
}
if (preg_match('#(?<id>[0-9-]+|latest)/extended#', $path)) {
$clockwork->extendRequest($data);
}
if (!$data) {
$response = ['message' => 'Not Found'];
return new Response(404, $headers, json_encode($response));
}
$data = is_array($data) ? array_map(function ($item) { return $item->toArray(); }, $data) : $data->toArray();
return new Response(200, $headers, json_encode($data));
}
protected function initializeConfig(): Config
{
$this->startTimer('_config', 'Configuration');
// Initialize Configuration
$grav = $this->container;
/** @var Config $config */
@@ -166,11 +82,15 @@ class InitializeProcessor extends ProcessorBase
$config->init();
$grav['plugins']->setup();
$this->stopTimer('_config');
return $config;
}
protected function initializeLogger(Config $config)
protected function initializeLogger(Config $config): void
{
$this->startTimer('_logger', 'Logger');
// Initialize Logging
$grav = $this->container;
@@ -187,16 +107,24 @@ class InitializeProcessor extends ProcessorBase
$log->pushHandler($logHandler);
break;
}
$this->stopTimer('_logger');
}
protected function initializeErrors()
protected function initializeErrors(): void
{
$this->startTimer('_errors', 'Error Handlers Reset');
// Initialize Error Handlers
$this->container['errors']->resetHandlers();
$this->stopTimer('_errors');
}
protected function initialize(Config $config)
protected function initialize(Config $config): void
{
$this->startTimer('_init', 'Initialize');
// Use output buffering to prevent headers from being sent too early.
ob_start();
if ($config->get('system.cache.gzip') && !@ob_start('ob_gzhandler')) {
@@ -225,9 +153,11 @@ class InitializeProcessor extends ProcessorBase
}
$this->container->setLocale();
$this->stopTimer('_init');
}
protected function initializeSession(Config $config)
protected function initializeSession(Config $config): void
{
// FIXME: Initialize session should happen later after plugins have been loaded. This is a workaround to fix session issues in AWS.
if (isset($this->container['session']) && $config->get('system.session.initialize', true)) {

View File

@@ -20,7 +20,7 @@ class PagesProcessor extends ProcessorBase
public $id = 'pages';
public $title = 'Pages';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer();

View File

@@ -18,7 +18,7 @@ class PluginsProcessor extends ProcessorBase
public $id = 'plugins';
public $title = 'Plugins';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer();
// TODO: remove in 2.0.

View File

@@ -25,21 +25,21 @@ abstract class ProcessorBase implements ProcessorInterface
$this->container = $container;
}
protected function startTimer($id = null, $title = null)
protected function startTimer($id = null, $title = null): void
{
/** @var Debugger $debugger */
$debugger = $this->container['debugger'];
$debugger->startTimer($id ?? $this->id, $title ?? $this->title);
}
protected function stopTimer($id = null)
protected function stopTimer($id = null): void
{
/** @var Debugger $debugger */
$debugger = $this->container['debugger'];
$debugger->stopTimer($id ?? $this->id);
}
protected function addMessage($message, $label = 'info', $isString = true)
protected function addMessage($message, $label = 'info', $isString = true): void
{
/** @var Debugger $debugger */
$debugger = $this->container['debugger'];

View File

@@ -20,7 +20,7 @@ class RenderProcessor extends ProcessorBase
public $id = 'render';
public $title = 'Render';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer();

View File

@@ -20,7 +20,7 @@ class RequestProcessor extends ProcessorBase
public $id = 'request';
public $title = 'Request';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer();

View File

@@ -19,7 +19,7 @@ class SchedulerProcessor extends ProcessorBase
public $id = '_scheduler';
public $title = 'Scheduler';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer();
$scheduler = $this->container['scheduler'];

View File

@@ -19,7 +19,7 @@ class TasksProcessor extends ProcessorBase
public $id = 'tasks';
public $title = 'Tasks';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer();

View File

@@ -18,7 +18,7 @@ class ThemesProcessor extends ProcessorBase
public $id = 'themes';
public $title = 'Themes';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer();
$this->container['themes']->init();

View File

@@ -18,7 +18,7 @@ class TwigProcessor extends ProcessorBase
public $id = 'twig';
public $title = 'Twig';
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->startTimer();
$this->container['twig']->init();