Skip to content

Commit 0ef60e9

Browse files
committed
Simplify the exporters.
1 parent d71011e commit 0ef60e9

File tree

6 files changed

+13
-51
lines changed

6 files changed

+13
-51
lines changed

src/Exporter/Ascii.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Class Ascii.
1717
*/
18-
class Ascii extends AbstractExporter
18+
final class Ascii extends AbstractExporter
1919
{
2020
/**
2121
* {@inheritdoc}

src/Exporter/Graph.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
/**
1313
* Class Graph.
1414
*/
15-
class Graph extends AbstractExporter
15+
final class Graph extends AbstractExporter
1616
{
1717
/**
1818
* The graph.

src/Exporter/Gv.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* Class Gv.
1717
*/
18-
class Gv extends AbstractExporter
18+
final class Gv extends AbstractExporter
1919
{
2020
/**
2121
* The graph attributes.

src/Exporter/Image.php

+8-46
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
use Exception;
88
use loophp\phptree\Node\NodeInterface;
99

10-
use const LOCK_EX;
1110
use const PHP_OS;
1211

1312
/**
1413
* Class Image.
1514
*/
16-
class Image extends Gv
15+
final class Image extends AbstractExporter
1716
{
1817
/**
1918
* @var string
@@ -44,13 +43,13 @@ public function __construct()
4443
*/
4544
public function export(NodeInterface $node): string
4645
{
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+
}
5049

51-
system($this->getConvertCommand($path));
50+
file_put_contents($tmp, (new Gv())->export($node));
5251

53-
return sprintf('%s.%s', $path, $this->getFormat());
52+
return shell_exec($this->getConvertCommand($tmp));
5453
}
5554

5655
/**
@@ -105,47 +104,10 @@ public function setFormat(string $format): self
105104
private function getConvertCommand(string $path): string
106105
{
107106
return sprintf(
108-
'%s -T%s %s -o %s.%s',
107+
'%s -T%s %s',
109108
$this->getExecutable(),
110109
$this->getFormat(),
111-
$path,
112-
$path,
113-
$this->getFormat()
110+
$path
114111
);
115112
}
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-
}
151113
}

src/Exporter/SimpleArray.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* Class SimpleArray.
1313
*/
14-
class SimpleArray extends AbstractExporter
14+
final class SimpleArray extends AbstractExporter
1515
{
1616
/**
1717
* {@inheritdoc}

src/Exporter/Text.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
* Class Text.
1212
*/
13-
class Text extends AbstractExporter
13+
final class Text extends AbstractExporter
1414
{
1515
/**
1616
* {@inheritdoc}

0 commit comments

Comments
 (0)