Skip to content

Commit c058e7c

Browse files
committed
progress on test / refactor compiler
1 parent 1c38fa3 commit c058e7c

6 files changed

Lines changed: 77 additions & 38 deletions

File tree

src/Compiler.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,38 @@ public function __construct(BladeCompiler $bladeCompiler, array $globalArgs = []
2424
$this->bladeDirectives();
2525
}
2626

27+
/**
28+
* Compile the content and styles of an ODT template
29+
* @return array{content: string, styles: string}
30+
*/
2731
public function compile(OdtFile $template, array $args = [], array $options = []): array
2832
{
2933
return [
30-
'content' => $this->compileXML($template->content(), $template, $args, $options),
31-
'styles' => $this->compileXML($template->styles(), $template, $args, $options),
34+
'content' => $this->compileTemplate($template->content(), $template, $args, $options),
35+
'styles' => $this->compileTemplate($template->styles(), $template, $args, $options),
3236
];
3337
}
3438

35-
private function compileXML(string $xml, OdtFile $template, array $args = [], array $options = []): string
39+
/**
40+
* @param array{content: string, styles: string} $compiled
41+
*/
42+
public function render(array $compiled, array $args = []): array
43+
{
44+
return [
45+
'content' => $this->renderTemplate($compiled['content'], $args),
46+
'styles' => $this->renderTemplate($compiled['styles'], $args),
47+
];
48+
}
49+
50+
private function compileTemplate(string $xml, OdtFile $template, array $args = [], array $options = []): string
3651
{
3752
if ($options['cleanVars'] ?? false) {
3853
$xml = (new VariableCleaner())->compile($xml);
3954
}
4055
$xml = $this->precompile($xml, $template, $args);
4156
$xml = $this->bladeCompile($xml);
4257

43-
$xml = $this->render($xml, $args);
44-
return $this->postRender($xml);
58+
return $xml;
4559
}
4660

4761
private function precompile(string $value, OdtFile $template, array $args): string
@@ -66,7 +80,7 @@ private function bladeCompile(string $value): string
6680
return $compiled;
6781
}
6882

69-
private function render(string $renderableContent, $args): string
83+
private function renderTemplate(string $renderableContent, $args): string
7084
{
7185
$args = $args
7286
+ [
@@ -85,11 +99,13 @@ private function render(string $renderableContent, $args): string
8599
try {
86100
eval('?>' . $renderableContent);
87101
} catch (\Exception $e) {
88-
ob_get_clean();
102+
ob_end_clean();
89103
throw $e;
90104
}
91105

92-
return ob_get_clean();
106+
$output = ob_get_clean();
107+
108+
return $this->postRender($output);
93109
}
94110

95111
/**

src/Converters/OfficeConverter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ public function __construct(string $bin = 'soffice')
1616
$this->bin = $bin;
1717
}
1818

19-
public function setBinary(string $bin): self
19+
public function setBinaryPath(string $bin): self
2020
{
2121
$this->bin = $bin;
2222
return $this;
2323
}
2424

25+
public function binaryPath(): string
26+
{
27+
return $this->bin;
28+
}
29+
2530
public function convert(string $inputPath, string $outputPath)
2631
{
2732
$options = [

src/Files/OdtFile.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Illuminate\Contracts\Support\Responsable;
66
use InvalidArgumentException;
77
use Petecoop\ODT\Compiler;
8+
use Petecoop\ODT\Converters\Converter;
89
use Petecoop\ODT\ODT;
9-
use Petecoop\ODT\OfficeConverter;
1010
use PhpZip\ZipFile;
1111
use Spatie\TemporaryDirectory\TemporaryDirectory;
1212

@@ -17,9 +17,9 @@ class OdtFile implements Responsable
1717
protected string $content;
1818
protected string $styles;
1919
protected array $tableOptions = [];
20-
protected null|ODT $odt = null;
20+
protected null|ODT $instance = null;
2121
protected null|Compiler $compiler = null;
22-
protected null|OfficeConverter $converter = null;
22+
protected null|Converter $converter = null;
2323

2424
public function __construct(
2525
protected string $path,
@@ -111,8 +111,8 @@ public function render(array $args = [], array $options = []): OdtFile
111111

112112
$output = new OdtFile($this->path);
113113
$output->setContent($compiled['content'])->setStyles($compiled['styles'])->setZip($this->zip);
114-
if ($this->odt) {
115-
$output->setODT($this->odt);
114+
if ($this->instance) {
115+
$output->setInstance($this->instance);
116116
}
117117
if ($this->compiler) {
118118
$output->setCompiler($this->compiler);
@@ -179,15 +179,15 @@ public function toResponse($request)
179179
->outputAsSymfonyResponse($this->fileName);
180180
}
181181

182-
public function setODT(ODT $odt): self
182+
public function setinstance(ODT $odt): self
183183
{
184-
$this->odt = $odt;
184+
$this->instance = $odt;
185185
return $this;
186186
}
187187

188188
public function compiler(): null|Compiler
189189
{
190-
return $this->compiler ?? $this->odt?->compiler();
190+
return $this->compiler ?? $this->instance?->compiler();
191191
}
192192

193193
public function setCompiler(Compiler $compiler): self
@@ -196,12 +196,12 @@ public function setCompiler(Compiler $compiler): self
196196
return $this;
197197
}
198198

199-
public function converter(): null|OfficeConverter
199+
public function converter(): null|Converter
200200
{
201-
return $this->converter ?? $this->odt?->converter();
201+
return $this->converter ?? $this->instance?->converter();
202202
}
203203

204-
public function setConverter(OfficeConverter $converter): self
204+
public function setConverter(Converter $converter): self
205205
{
206206
$this->converter = $converter;
207207
return $this;

src/ODT.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,27 @@ public static function make(null|string $bin = null): self
3838
public function open(string $path)
3939
{
4040
$template = new OdtFile($path);
41-
$template->setODT($this);
41+
$template->setInstance($this);
4242

4343
return $template;
4444
}
4545

46-
public function compiler()
46+
public function compiler(): Compiler
4747
{
4848
return $this->compiler;
4949
}
5050

51-
public function converter()
51+
public function converter(): Converter
5252
{
5353
return $this->converter;
5454
}
5555

5656
public function officeBinary(string $binaryPath): self
5757
{
58-
$this->converter->setBinary($binaryPath);
58+
if ($this->converter instanceof OfficeConverter) {
59+
$this->converter->setBinaryPath($binaryPath);
60+
}
61+
5962
return $this;
6063
}
6164
}

tests/TestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use Illuminate\Filesystem\Filesystem;
66
use Illuminate\View\Compilers\BladeCompiler;
7+
use Petecoop\ODT\Files\OdtFile;
78
use Petecoop\ODT\ODT;
8-
use Petecoop\ODT\Template;
99
use PHPUnit\Framework\TestCase as BaseTestCase;
1010

1111
class TestCase extends BaseTestCase
@@ -16,9 +16,9 @@ function odt(): ODT
1616
return new ODT($blade);
1717
}
1818

19-
function template(string $xml): Template
19+
function template(string $xml): OdtFile
2020
{
21-
$template = new Template('');
21+
$template = new OdtFile('');
2222
$template->setContent($xml);
2323
$template->setStyles('');
2424

tests/Unit/ODTTest.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
11
<?php
22

3+
use Petecoop\ODT\Compiler;
4+
use Petecoop\ODT\Converters\OfficeConverter;
5+
use Petecoop\ODT\Files\OdtFile;
36
use Petecoop\ODT\ODT;
4-
use Petecoop\ODT\Output;
5-
use Petecoop\ODT\Template;
67

7-
test('instance', function () {
8+
test('create instance', function () {
89
expect($this->odt())->toBeInstanceOf(ODT::class);
10+
11+
$odt = ODT::make();
12+
expect($odt)->toBeInstanceOf(ODT::class);
13+
expect($odt->converter())->toBeInstanceOf(OfficeConverter::class);
14+
expect($odt->compiler())->toBeInstanceOf(Compiler::class);
915
});
1016

11-
test('template open', function () {
12-
$template = $this->odt()->open('tests/files/basic-template.odt');
13-
expect($template)->toBeInstanceOf(Template::class);
17+
test('set office converter', function () {
18+
$odt = ODT::make('/usr/bin/libreoffice');
19+
expect($odt->converter())->toBeInstanceOf(OfficeConverter::class);
1420

15-
$content = $template->content();
16-
$this->assertStringContainsString('Basic template {{ $test }}', $content);
21+
/** @var OfficeConverter */
22+
$converter = $odt->converter();
23+
expect($converter->binaryPath())->toEqual('/usr/bin/libreoffice');
24+
});
25+
26+
test('open a template', function () {
27+
$odt = $this->odt();
28+
$template = $odt->open('tests/files/basic-template.odt');
29+
expect($template)->toBeInstanceOf(OdtFile::class);
30+
expect($template->compiler())->toBe($odt->compiler());
31+
expect($template->converter())->toBe($odt->converter());
32+
expect($template->content())->toContain('Basic template {{ $test }}');
1733
});
1834

1935
test('render template', function () {
2036
$template = $this->odt()->open('tests/files/basic-template.odt');
2137
$output = $template->render(['test' => 'Hello Tests']);
22-
expect($output)->toBeInstanceOf(Output::class);
23-
24-
$this->assertStringContainsString('Basic template Hello Tests', $output->content());
38+
expect($output)->toBeInstanceOf(OdtFile::class);
39+
expect($output->content())->toContain('Basic template Hello Tests');
2540
});
2641

2742
test('compile', function ($xml, $args, $expected) {

0 commit comments

Comments
 (0)