Skip to content

Commit 3dabf92

Browse files
authored
Merge pull request #1279 from CybotTM/bugfix/plantuml-tempnam-notice
[BUGFIX] Ensure temp directory exists before calling tempnam() + DI support
2 parents cce4441 + a40aec7 commit 3dabf92

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@
2121
use function array_merge;
2222
use function file_get_contents;
2323
use function file_put_contents;
24+
use function is_dir;
25+
use function mkdir;
2426
use function sys_get_temp_dir;
2527
use function tempnam;
2628

2729
final class PlantumlRenderer implements DiagramRenderer
2830
{
29-
public function __construct(private readonly LoggerInterface $logger, private readonly string $plantUmlBinaryPath)
30-
{
31+
private readonly string $tempDirectory;
32+
33+
public function __construct(
34+
private readonly LoggerInterface $logger,
35+
private readonly string $plantUmlBinaryPath,
36+
string|null $tempDirectory = null,
37+
) {
38+
$this->tempDirectory = $tempDirectory ?? sys_get_temp_dir() . '/phpdocumentor';
3139
}
3240

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

48-
$pumlFileLocation = tempnam(sys_get_temp_dir() . '/phpdocumentor', 'pu_');
56+
if (!is_dir($this->tempDirectory)) {
57+
mkdir($this->tempDirectory, 0o755, true);
58+
}
59+
60+
$pumlFileLocation = tempnam($this->tempDirectory, 'pu_');
4961
file_put_contents($pumlFileLocation, $output);
5062
try {
5163
$process = new Process([$this->plantUmlBinaryPath, '-tsvg', $pumlFileLocation], __DIR__, null, null, 600.0);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link https://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Guides\Graphs\Renderer;
15+
16+
use phpDocumentor\Guides\RenderContext;
17+
use PHPUnit\Framework\TestCase;
18+
use Psr\Log\NullLogger;
19+
20+
use function rmdir;
21+
use function sys_get_temp_dir;
22+
use function uniqid;
23+
24+
final class PlantumlRendererTest extends TestCase
25+
{
26+
public function testRenderCreatesTempDirectoryWhenMissing(): void
27+
{
28+
$tempDir = sys_get_temp_dir() . '/plantuml-test-' . uniqid();
29+
30+
// Ensure the directory does not exist
31+
self::assertDirectoryDoesNotExist($tempDir);
32+
33+
// Use a non-existent binary path - the render will fail but directory should be created first
34+
$renderer = new PlantumlRenderer(new NullLogger(), '/non/existent/plantuml', $tempDir);
35+
36+
$renderContext = $this->createMock(RenderContext::class);
37+
$renderContext->method('getLoggerInformation')->willReturn([]);
38+
39+
// The render will fail due to missing binary, but the temp directory should be created
40+
$renderer->render($renderContext, 'A -> B');
41+
42+
self::assertDirectoryExists($tempDir);
43+
44+
// Clean up
45+
@rmdir($tempDir);
46+
}
47+
}

0 commit comments

Comments
 (0)