-
Notifications
You must be signed in to change notification settings - Fork 19
/
ImageProcessor.php
70 lines (57 loc) · 2.14 KB
/
ImageProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php declare(strict_types=1);
/**
* This source file is subject to the license that is bundled with this package in the file LICENSE.
*/
namespace PhUml\Processors;
use Symfony\Component\Process\Process;
use Symplify\SmartFileSystem\SmartFileSystem;
/**
* It generates a `png` class diagram from a digraph in DOT format
*
* It takes a digraph in DOT format, saves it to a temporary location and creates a `png` class
* diagram out of it.
* It uses either the `dot` or `neato` command to create the image
*/
final class ImageProcessor implements Processor
{
public static function neato(SmartFileSystem $filesystem): ImageProcessor
{
return new ImageProcessor(new ImageProcessorName('neato'), $filesystem);
}
public static function dot(SmartFileSystem $filesystem): ImageProcessor
{
return new ImageProcessor(new ImageProcessorName('dot'), $filesystem);
}
private function __construct(private readonly ImageProcessorName $name, private readonly SmartFileSystem $fileSystem)
{
}
public function name(): string
{
return $this->name->value();
}
/**
* It returns the contents of a `png` class diagram
*/
public function process(OutputContent $digraphInDotFormat): OutputContent
{
$dotFile = $this->fileSystem->tempnam(sys_get_temp_dir(), 'phuml');
$imageFile = $this->fileSystem->tempnam(sys_get_temp_dir(), 'phuml');
$this->fileSystem->dumpFile($dotFile, $digraphInDotFormat->value());
$this->execute($dotFile, $imageFile);
$image = $this->fileSystem->readFile($imageFile);
$this->fileSystem->remove($dotFile);
$this->fileSystem->remove($imageFile);
return new OutputContent($image);
}
/**
* @throws ImageGenerationFailure If the Graphviz command failed
*/
private function execute(string $inputFile, string $outputFile): void
{
$process = new Process([$this->name->command(), '-Tpng', '-o', $outputFile, $inputFile]);
$process->run();
if (! $process->isSuccessful()) {
throw ImageGenerationFailure::withOutput($process->getErrorOutput());
}
}
}