Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an ability to send var-dumper dumps with specifying language for code highlighting. #165

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions app/modules/Ray/Application/Handlers/RemoveSfDumpScriptHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Modules\Ray\Application\Handlers;

use Modules\Sentry\Application\EventHandlerInterface;

final class RemoveSfDumpScriptHandler implements EventHandlerInterface
{
public function handle(array $event): array
{
if (!isset($event['payloads']) || !\is_array($event['payloads'])) {
return $event;
}

foreach ($event['payloads'] as $i => $payload) {
if (isset($payload['content']) && \is_array($payload['content'])) {
foreach ($payload['content'] as $k => $value) {
if ($k === 'values') {
foreach ($value as $j => $val) {
if (!\str_contains($val, 'Sfdump')) {
continue;
}
$event['payloads'][$i]['content'][$k][$j] = $this->cleanHtml($val);
}
continue;
}

if (\is_array($value) || !\str_contains($value, 'Sfdump')) {
continue;
}
$event['payloads'][$i]['content'][$k] = $this->cleanHtml($value);
}
}
}

return $event;
}

private function cleanHtml(string $html): string
{
// Regex to find all instances of sf-dump- followed by digits
$pattern = '/sf-dump-\d+/';
// Perform the search
\preg_match($pattern, $html, $matches);

$sfDumpId = $matches[0] ?? null;
// Remove everything except <pre> tags and their content
return \preg_replace(
'/(?s)(.*?)(<pre[^>]*>.*?<\/pre>)(.*)|(?s)(.*)/',
'$2',
$html,
) . '<script>Sfdump("' . $sfDumpId . '")</script>';
}
}
2 changes: 2 additions & 0 deletions app/modules/Ray/Application/RayBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Application\Service\HttpHandler\HandlerRegistryInterface;
use Modules\Ray\Application\Handlers\MergeEventsHandler;
use Modules\Ray\Application\Handlers\RemoveSfDumpScriptHandler;
use Modules\Ray\EventHandler;
use Modules\Ray\Interfaces\Http\Handler\EventHandler as HttpEventHandler;
use Psr\Container\ContainerInterface;
Expand All @@ -20,6 +21,7 @@ public function defineSingletons(): array
ContainerInterface $container,
): EventHandlerInterface => new EventHandler($container, [
MergeEventsHandler::class,
RemoveSfDumpScriptHandler::class,
]),
];
}
Expand Down
20 changes: 16 additions & 4 deletions app/modules/VarDumper/Interfaces/TCP/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ private function fireEvent(ParsedPayload $payload): void
new HandleReceivedEvent(
type: 'var-dump',
payload: [
'payload' => [
'type' => $payload->data->getType(),
'value' => $this->convertToPrimitive($payload->data),
],
'payload' => $this->prepareContent($payload),
'context' => $payload->context,
],
project: $payload->context['project'] ?? null,
Expand All @@ -78,4 +75,19 @@ private function convertToPrimitive(Data $data): BodyInterface|null
value: $dumper->dump($data, true),
);
}

private function prepareContent(ParsedPayload $payload): array
{
$payloadContent = [
'type' => $payload->data->getType(),
'value' => $this->convertToPrimitive($payload->data),
];

if ($payload->data->getType() === 'string' && isset($payload->context['language'])) {
$payloadContent['type'] = 'code';
$payloadContent['language'] = $payload->context['language'];
}

return $payloadContent;
}
}
Loading
Loading