Skip to content

Commit

Permalink
add OpenHandler to load the data after an ajax request.
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa.seef-aldeen committed Aug 18, 2023
1 parent 397afb5 commit fa996dc
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
use Mezzio\DebugBar\DataCollector\DoctrineCollectorFactory;
use Mezzio\DebugBar\Storage\FileStorageFactory;

class ConfigProvider
final class ConfigProvider
{
public const OPEN_HANDLER_URL = 'debugbarOpen';
/**
* Returns the configuration array
*/
Expand All @@ -23,10 +24,11 @@ public function __invoke(): array
'dependencies' => $this->getDependencies(),
'debugbar' => $this->getConfig(),
'middleware_pipeline' => $this->getMiddelewarePipeline(),
'routes' => $this->getRoutes(),
];
}

public function getConfig(): array
private function getConfig(): array
{
return [
'disable' => false,
Expand All @@ -48,7 +50,7 @@ public function getConfig(): array
/**
* Returns the container dependencies
*/
public function getDependencies(): array
private function getDependencies(): array
{
return [
'factories' => [
Expand All @@ -57,14 +59,15 @@ public function getDependencies(): array
DoctrineCollector::class => DoctrineCollectorFactory::class,
DebugBar::class => StandardDebugBarFactory::class,
FileStorage::class => FileStorageFactory::class,
OpenHandler::class => OpenHandlerFactory::class,
],
];
}

/**
* @return array[]
*/
protected function getMiddelewarePipeline(): array
private function getMiddelewarePipeline(): array
{
return [
DebugBarMiddleware::class => [
Expand All @@ -75,4 +78,15 @@ protected function getMiddelewarePipeline(): array
],
];
}

private function getRoutes(): array
{
return [
self::OPEN_HANDLER_URL => [
'path' => '/' . self::OPEN_HANDLER_URL,
'middleware' => OpenHandler::class,
'allowed_methods' => ['GET', 'POST'],
],
];
}
}
10 changes: 8 additions & 2 deletions src/DebugBarMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ private function setConfigOptions(): void
if ($renderOptions['bind_ajax_handler_to_xhr'] ?? false) {
$this->renderer->setBindAjaxHandlerToXHR();
}
if ($this->captureAjax) {
$this->renderer->setOpenHandlerUrl(ConfigProvider::OPEN_HANDLER_URL);
}
}

private function disableDebugBar(ServerRequestInterface $request, ResponseInterface $response): bool
Expand All @@ -132,8 +135,12 @@ private function disableDebugBar(ServerRequestInterface $request, ResponseInterf
$disableCookieValue = $request->getCookieParams()[self::DISABLE_KEY] ?? false;
$disableAttributeValue = $request->getAttribute(self::DISABLE_KEY, '') ?? false;
$isDownload = strpos($response->getHeaderLine('Content-Disposition'), 'attachment;') !== false;
$isOpenHandlerUrl = $request->getUri()->getPath() === '/' . ConfigProvider::OPEN_HANDLER_URL;

if ($disableByConfig || $isDownload || $disableHeaderValue || $disableCookieValue || $disableAttributeValue) {
if (
$disableByConfig || $isDownload || $disableHeaderValue || $disableCookieValue || $disableAttributeValue
|| $isOpenHandlerUrl
) {
return true;
}

Expand Down Expand Up @@ -219,7 +226,6 @@ private function handleAjaxWithNonHtmlResponse(ResponseInterface $response): Res
if ($this->debugBar->getStorage() === null) {
$headers = $this->debugBar->getDataAsHeaders();
} else {
$this->renderer->setOpenHandlerUrl('open.php');
$this->debugBar->getData();
$headerName = 'phpdebugbar-id';
$headers = [$headerName => $this->debugBar->getCurrentRequestId()];
Expand Down
84 changes: 84 additions & 0 deletions src/OpenHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Mezzio\DebugBar;

use DebugBar\DebugBar;
use DebugBar\DebugBarException;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function in_array;

class OpenHandler implements RequestHandlerInterface
{
protected const ALLOW_OPERATION = ['find', 'get', 'clear'];
protected const FILTER_KEYS = ['utime', 'datetime', 'ip', 'uri', 'method'];

protected DebugBar $debugBar;

public function __construct(DebugBar $debugBar)
{
$this->debugBar = $debugBar;
}

/**
* @throws DebugBarException
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$queryParams = $request->getQueryParams();
$op = $queryParams['op'] ?? 'find';
if (! in_array($op, self::ALLOW_OPERATION)) {
throw new DebugBarException('Invalid operation:' . $op);
}
switch ($op) {
case 'get':
$response = $this->get($queryParams);
break;
case 'clear':
$response = $this->clear();
break;
default:
$response = $this->find($queryParams);
}
return new JsonResponse($response);
}

protected function find(array $queryParams): array
{
$max = $queryParams['max'] ?? 20;

$offset = $queryParams['offset'] ?? 0;

$filters = [];
foreach (self::FILTER_KEYS as $key) {
if (isset($queryParams[$key])) {
$filters[$key] = $queryParams[$key];
}
}

return $this->debugBar->getStorage()->find($filters, $max, $offset);
}

/**
* @throws DebugBarException
*/
protected function get(array $queryParams): array
{
$id = $queryParams['id'] ?? null;
if ($id === null) {
throw new DebugBarException("Missing 'id' parameter in 'get' operation");
}
return $this->debugBar->getStorage()->get($id);
}

protected function clear(): array
{
$this->debugBar->getStorage()->clear();
return ['success' => true];
}
}
24 changes: 24 additions & 0 deletions src/OpenHandlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Mezzio\DebugBar;

use DebugBar\DebugBar;
use DebugBar\DebugBarException;
use Psr\Container\ContainerInterface;

class OpenHandlerFactory
{
/**
* @throws DebugBarException
*/
public function __invoke(ContainerInterface $container): OpenHandler
{
$debugBar = $container->get(DebugBar::class);
if ($debugBar->getStorage() === null) {
throw new DebugBarException("DebugBar must have a storage to use OpenHandler");
}
return new OpenHandler($debugBar);
}
}

0 comments on commit fa996dc

Please sign in to comment.