|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 7.1 |
|
4
|
|
|
* |
|
5
|
|
|
* This source file is subject to the license that is bundled with this package in the file LICENSE. |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace PhUml\Processors; |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
10
|
|
|
use Symfony\Component\Process\Process; |
|
11
|
|
|
|
|
12
|
|
|
abstract class ImageProcessor extends Processor |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var Process */ |
|
15
|
|
|
protected $process; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Filesystem */ |
|
18
|
|
|
private $fileSystem; |
|
19
|
|
|
|
|
20
|
24 |
|
public function __construct(Process $process = null, Filesystem $fileSystem = null) |
|
21
|
|
|
{ |
|
22
|
24 |
|
$this->process = $process ?? new Process($this->command()); |
|
23
|
24 |
|
$this->fileSystem = $fileSystem ?? new Filesystem(); |
|
24
|
24 |
|
} |
|
25
|
|
|
|
|
26
|
6 |
|
public function process(string $digraphInDotFormat): string |
|
27
|
|
|
{ |
|
28
|
6 |
|
$dotFile = $this->fileSystem->tempnam('/tmp', 'phuml'); |
|
29
|
6 |
|
$imageFile = $this->fileSystem->tempnam('/tmp', 'phuml'); |
|
30
|
|
|
|
|
31
|
6 |
|
$this->fileSystem->dumpFile($dotFile, $digraphInDotFormat); |
|
32
|
6 |
|
$this->fileSystem->remove($imageFile); |
|
33
|
|
|
|
|
34
|
6 |
|
$this->execute($dotFile, $imageFile); |
|
35
|
|
|
|
|
36
|
6 |
|
$image = file_get_contents($imageFile); |
|
37
|
|
|
|
|
38
|
6 |
|
$this->fileSystem->remove($dotFile); |
|
39
|
6 |
|
$this->fileSystem->remove($imageFile); |
|
40
|
|
|
|
|
41
|
6 |
|
return $image; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
18 |
|
public function execute(string $inputFile, string $outputFile): void |
|
45
|
|
|
{ |
|
46
|
18 |
|
$this->process->setCommandLine([$this->command(), '-Tpng', '-o', $outputFile, $inputFile]); |
|
47
|
18 |
|
$this->process->run(); |
|
48
|
18 |
|
if (!$this->process->isSuccessful()) { |
|
49
|
6 |
|
throw new ImageGenerationFailure($this->process->getErrorOutput()); |
|
50
|
|
|
} |
|
51
|
12 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
abstract public function command(): string; |
|
54
|
|
|
} |
|
55
|
|
|
|