Skip to content
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
18 changes: 15 additions & 3 deletions packages/guides-graphs/src/Graphs/Renderer/PlantumlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@
use function array_merge;
use function file_get_contents;
use function file_put_contents;
use function is_dir;
use function mkdir;
use function sys_get_temp_dir;
use function tempnam;

final class PlantumlRenderer implements DiagramRenderer
{
public function __construct(private readonly LoggerInterface $logger, private readonly string $plantUmlBinaryPath)
{
private readonly string $tempDirectory;

public function __construct(
private readonly LoggerInterface $logger,
private readonly string $plantUmlBinaryPath,
string|null $tempDirectory = null,
) {
$this->tempDirectory = $tempDirectory ?? sys_get_temp_dir() . '/phpdocumentor';
}

public function render(RenderContext $renderContext, string $diagram): string|null
Expand All @@ -45,7 +53,11 @@ public function render(RenderContext $renderContext, string $diagram): string|nu
@enduml
PUML;

$pumlFileLocation = tempnam(sys_get_temp_dir() . '/phpdocumentor', 'pu_');
if (!is_dir($this->tempDirectory)) {
mkdir($this->tempDirectory, 0o755, true);
}

$pumlFileLocation = tempnam($this->tempDirectory, 'pu_');
file_put_contents($pumlFileLocation, $output);
try {
$process = new Process([$this->plantUmlBinaryPath, '-tsvg', $pumlFileLocation], __DIR__, null, null, 600.0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides\Graphs\Renderer;

use phpDocumentor\Guides\RenderContext;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;

use function rmdir;
use function sys_get_temp_dir;
use function uniqid;

final class PlantumlRendererTest extends TestCase
{
public function testRenderCreatesTempDirectoryWhenMissing(): void
{
$tempDir = sys_get_temp_dir() . '/plantuml-test-' . uniqid();

// Ensure the directory does not exist
self::assertDirectoryDoesNotExist($tempDir);

// Use a non-existent binary path - the render will fail but directory should be created first
$renderer = new PlantumlRenderer(new NullLogger(), '/non/existent/plantuml', $tempDir);

$renderContext = $this->createMock(RenderContext::class);
$renderContext->method('getLoggerInformation')->willReturn([]);

// The render will fail due to missing binary, but the temp directory should be created
$renderer->render($renderContext, 'A -> B');

self::assertDirectoryExists($tempDir);

// Clean up
@rmdir($tempDir);
}
}
Loading