1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace loophp\phptree\Exporter; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use loophp\phptree\Node\NodeInterface; |
9
|
|
|
|
10
|
|
|
use const LOCK_EX; |
11
|
|
|
use const PHP_OS; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Image. |
15
|
|
|
*/ |
16
|
|
|
class Image extends Gv |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $executable = 'dot'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $format = 'svg'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Image constructor. |
30
|
|
|
*/ |
31
|
4 |
|
public function __construct() |
32
|
|
|
{ |
33
|
4 |
|
if (0 === mb_stripos(PHP_OS, 'WIN')) { |
34
|
|
|
$this->executable = 'dot.exe'; |
35
|
|
|
} |
36
|
4 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param \loophp\phptree\Node\NodeInterface $node |
40
|
|
|
* |
41
|
|
|
* @throws Exception |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
1 |
|
public function export(NodeInterface $node): string |
46
|
|
|
{ |
47
|
1 |
|
$path = $this->getTemporaryFile(); |
48
|
|
|
|
49
|
1 |
|
$this->writeToFile($path, parent::export($node)); |
50
|
|
|
|
51
|
1 |
|
system($this->getConvertCommand($path)); |
52
|
|
|
|
53
|
1 |
|
return sprintf('%s.%s', $path, $this->getFormat()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the executable to use. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
3 |
|
public function getExecutable(): string |
62
|
|
|
{ |
63
|
3 |
|
return $this->executable; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
2 |
|
public function getFormat(): string |
70
|
|
|
{ |
71
|
2 |
|
return $this->format; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Change the executable to use. |
76
|
|
|
* |
77
|
|
|
* @param string $executable |
78
|
|
|
* |
79
|
|
|
* @return \loophp\phptree\Exporter\Image |
80
|
|
|
*/ |
81
|
1 |
|
public function setExecutable(string $executable): self |
82
|
|
|
{ |
83
|
1 |
|
$this->executable = $executable; |
84
|
|
|
|
85
|
1 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $format |
90
|
|
|
* |
91
|
|
|
* @return \loophp\phptree\Exporter\Image |
92
|
|
|
*/ |
93
|
1 |
|
public function setFormat(string $format): self |
94
|
|
|
{ |
95
|
1 |
|
$this->format = $format; |
96
|
|
|
|
97
|
1 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $path |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
1 |
|
private function getConvertCommand(string $path): string |
106
|
|
|
{ |
107
|
1 |
|
return sprintf( |
108
|
1 |
|
'%s -T%s %s -o %s.%s', |
109
|
1 |
|
$this->getExecutable(), |
110
|
1 |
|
$this->getFormat(), |
111
|
1 |
|
$path, |
112
|
1 |
|
$path, |
113
|
1 |
|
$this->getFormat() |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @throws Exception |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
1 |
|
private function getTemporaryFile(): string |
123
|
|
|
{ |
124
|
1 |
|
$path = tempnam(sys_get_temp_dir(), 'graphviz'); |
125
|
|
|
|
126
|
1 |
|
if (false === $path) { |
127
|
|
|
throw new Exception('Unable to get temporary file name for graphviz script'); |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return $path; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $path |
135
|
|
|
* @param string $content |
136
|
|
|
* |
137
|
|
|
* @throws Exception |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
1 |
|
private function writeToFile(string $path, string $content): bool |
142
|
|
|
{ |
143
|
1 |
|
$ret = file_put_contents($path, $content, LOCK_EX); |
144
|
|
|
|
145
|
1 |
|
if (false === $ret) { |
146
|
|
|
throw new Exception('Unable to write graphviz script to temporary file'); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
return true; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|