Skip to content

Commit d200761

Browse files
linawolfjaapio
authored andcommitted
!!![FEATURE] Improve Plantuml debug output
For this we have to pass the rendercontext to the DiagramRenderer, a breaking change if the interface was implemented externally.
1 parent 82e3f7b commit d200761

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

packages/guides-graphs/src/Graphs/Renderer/DiagramRenderer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
namespace phpDocumentor\Guides\Graphs\Renderer;
1515

16+
use phpDocumentor\Guides\RenderContext;
17+
1618
interface DiagramRenderer
1719
{
18-
public function render(string $diagram): string|null;
20+
public function render(RenderContext $renderContext, string $diagram): string|null;
1921
}

packages/guides-graphs/src/Graphs/Renderer/PlantumlRenderer.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
namespace phpDocumentor\Guides\Graphs\Renderer;
1515

16+
use phpDocumentor\Guides\RenderContext;
1617
use Psr\Log\LoggerInterface;
1718
use Symfony\Component\Process\Exception\RuntimeException;
1819
use Symfony\Component\Process\Process;
1920

21+
use function array_merge;
2022
use function file_get_contents;
2123
use function file_put_contents;
2224
use function sys_get_temp_dir;
@@ -28,7 +30,7 @@ public function __construct(private readonly LoggerInterface $logger, private re
2830
{
2931
}
3032

31-
public function render(string $diagram): string|null
33+
public function render(RenderContext $renderContext, string $diagram): string|null
3234
{
3335
$output = <<<PUML
3436
@startuml
@@ -50,12 +52,24 @@ public function render(string $diagram): string|null
5052
$process->run();
5153

5254
if (!$process->isSuccessful()) {
53-
$this->logger->error('Generating the class diagram failed', ['error' => $process->getErrorOutput()]);
55+
$this->logger->error(
56+
'Generating the class diagram failed',
57+
array_merge(
58+
['error' => $process->getErrorOutput()],
59+
$renderContext->getLoggerInformation(),
60+
),
61+
);
5462

5563
return null;
5664
}
5765
} catch (RuntimeException $e) {
58-
$this->logger->error('Generating the class diagram failed', ['error' => $e->getMessage()]);
66+
$this->logger->error(
67+
'Generating the class diagram failed',
68+
array_merge(
69+
['error' => $e->getMessage()],
70+
$renderContext->getLoggerInformation(),
71+
),
72+
);
5973

6074
return null;
6175
}

packages/guides-graphs/src/Graphs/Renderer/PlantumlServerRenderer.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace phpDocumentor\Guides\Graphs\Renderer;
66

7+
use phpDocumentor\Guides\RenderContext;
78
use Psr\Log\LoggerInterface;
89
use Symfony\Contracts\HttpClient\HttpClientInterface;
910

1011
use function Jawira\PlantUml\encodep;
12+
use function sprintf;
1113

1214
final class PlantumlServerRenderer implements DiagramRenderer
1315
{
@@ -18,17 +20,26 @@ public function __construct(
1820
) {
1921
}
2022

21-
public function render(string $diagram): string|null
23+
public function render(RenderContext $renderContext, string $diagram): string|null
2224
{
2325
$encodedDiagram = encodep($diagram);
2426

27+
$url = $this->plantumlServerUrl . '/svg/' . $encodedDiagram;
28+
2529
$response = $this->httpClient->request(
2630
'GET',
27-
$this->plantumlServerUrl . '/svg/' . $encodedDiagram,
31+
$url,
2832
);
2933

3034
if ($response->getStatusCode() !== 200) {
31-
$this->logger->error('Failed to render diagram using server:' . $this->plantumlServerUrl);
35+
$this->logger->warning(
36+
sprintf(
37+
'Failed to render diagram using url: %s. The server returned status code %s. ',
38+
$url,
39+
$response->getStatusCode(),
40+
),
41+
$renderContext->getLoggerInformation(),
42+
);
3243

3344
return null;
3445
}

packages/guides-graphs/src/Graphs/Renderer/TestRenderer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
namespace phpDocumentor\Guides\Graphs\Renderer;
66

7+
use phpDocumentor\Guides\RenderContext;
8+
79
use function md5;
810

911
final class TestRenderer implements DiagramRenderer
1012
{
11-
public function render(string $diagram): string|null
13+
public function render(RenderContext $renderContext, string $diagram): string|null
1214
{
1315
return md5($diagram);
1416
}

packages/guides-graphs/src/Graphs/Twig/UmlExtension.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
namespace phpDocumentor\Guides\Graphs\Twig;
1515

1616
use phpDocumentor\Guides\Graphs\Renderer\DiagramRenderer;
17+
use phpDocumentor\Guides\RenderContext;
1718
use Twig\Extension\AbstractExtension;
1819
use Twig\TwigFunction;
1920

21+
use function assert;
22+
2023
final class UmlExtension extends AbstractExtension
2124
{
2225
private DiagramRenderer $diagramRenderer;
@@ -37,12 +40,16 @@ public function __construct(iterable $renderers, string $rendererAlias)
3740
public function getFunctions(): array
3841
{
3942
return [
40-
new TwigFunction('uml', $this->uml(...), ['is_safe' => ['html']]),
43+
new TwigFunction('uml', $this->uml(...), ['is_safe' => ['html'], 'needs_context' => true]),
4144
];
4245
}
4346

44-
public function uml(string $source): string|null
47+
/** @param array{env: RenderContext} $context */
48+
public function uml(array $context, string $source): string|null
4549
{
46-
return $this->diagramRenderer->render($source);
50+
$renderContext = $context['env'];
51+
assert($renderContext instanceof RenderContext);
52+
53+
return $this->diagramRenderer->render($renderContext, $source);
4754
}
4855
}

0 commit comments

Comments
 (0)