|
7 | 7 | use Exception;
|
8 | 8 | use loophp\phptree\Node\NodeInterface;
|
9 | 9 |
|
10 |
| -use const LOCK_EX; |
11 | 10 | use const PHP_OS;
|
12 | 11 |
|
13 | 12 | /**
|
14 | 13 | * Class Image.
|
15 | 14 | */
|
16 |
| -class Image extends Gv |
| 15 | +final class Image extends AbstractExporter |
17 | 16 | {
|
18 | 17 | /**
|
19 | 18 | * @var string
|
@@ -44,13 +43,13 @@ public function __construct()
|
44 | 43 | */
|
45 | 44 | public function export(NodeInterface $node): string
|
46 | 45 | {
|
47 |
| - $path = $this->getTemporaryFile(); |
48 |
| - |
49 |
| - $this->writeToFile($path, parent::export($node)); |
| 46 | + if (false === $tmp = tempnam(sys_get_temp_dir(), 'phptree-export-')) { |
| 47 | + return ''; |
| 48 | + } |
50 | 49 |
|
51 |
| - system($this->getConvertCommand($path)); |
| 50 | + file_put_contents($tmp, (new Gv())->export($node)); |
52 | 51 |
|
53 |
| - return sprintf('%s.%s', $path, $this->getFormat()); |
| 52 | + return shell_exec($this->getConvertCommand($tmp)); |
54 | 53 | }
|
55 | 54 |
|
56 | 55 | /**
|
@@ -105,47 +104,10 @@ public function setFormat(string $format): self
|
105 | 104 | private function getConvertCommand(string $path): string
|
106 | 105 | {
|
107 | 106 | return sprintf(
|
108 |
| - '%s -T%s %s -o %s.%s', |
| 107 | + '%s -T%s %s', |
109 | 108 | $this->getExecutable(),
|
110 | 109 | $this->getFormat(),
|
111 |
| - $path, |
112 |
| - $path, |
113 |
| - $this->getFormat() |
| 110 | + $path |
114 | 111 | );
|
115 | 112 | }
|
116 |
| - |
117 |
| - /** |
118 |
| - * @throws Exception |
119 |
| - * |
120 |
| - * @return string |
121 |
| - */ |
122 |
| - private function getTemporaryFile(): string |
123 |
| - { |
124 |
| - $path = tempnam(sys_get_temp_dir(), 'graphviz'); |
125 |
| - |
126 |
| - if (false === $path) { |
127 |
| - throw new Exception('Unable to get temporary file name for graphviz script'); |
128 |
| - } |
129 |
| - |
130 |
| - return $path; |
131 |
| - } |
132 |
| - |
133 |
| - /** |
134 |
| - * @param string $path |
135 |
| - * @param string $content |
136 |
| - * |
137 |
| - * @throws Exception |
138 |
| - * |
139 |
| - * @return bool |
140 |
| - */ |
141 |
| - private function writeToFile(string $path, string $content): bool |
142 |
| - { |
143 |
| - $ret = file_put_contents($path, $content, LOCK_EX); |
144 |
| - |
145 |
| - if (false === $ret) { |
146 |
| - throw new Exception('Unable to write graphviz script to temporary file'); |
147 |
| - } |
148 |
| - |
149 |
| - return true; |
150 |
| - } |
151 | 113 | }
|
0 commit comments