From 8892b11dc7c5e623cc18db6dd9372cd36946f477 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Thu, 27 Oct 2022 16:54:24 +0200 Subject: [PATCH] --wip-- [skip ci] --- config/tex.php | 10 ++++++++++ src/Compiler.php | 16 ++++++++++------ src/CompilerException.php | 9 +++++++++ src/Document.php | 10 ++++++---- src/Engine.php | 23 +++++++++++++++++++++++ src/Template.php | 30 ++++++++++++++++++++++++++++++ src/TexServiceProvider.php | 4 +++- 7 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 config/tex.php create mode 100644 src/CompilerException.php create mode 100644 src/Engine.php create mode 100644 src/Template.php diff --git a/config/tex.php b/config/tex.php new file mode 100644 index 0000000..37084c5 --- /dev/null +++ b/config/tex.php @@ -0,0 +1,10 @@ + [ + 'binary' => env('PDFLATEX_BIN', ''), + ], + 'xelatex' => [ + 'binary' => env('XELATEX_BIN', ''), + ], +]; diff --git a/src/Compiler.php b/src/Compiler.php index b92f6e4..01afcab 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -15,20 +15,24 @@ class Compiler implements Responsable $filename = $document->filename(); $dir = Str::random(32); - $contents = view()->make($document->view(), get_object_vars($document))->render(); + $contents = $document->renderBody(); mkdir('/tmp/'.$dir); file_put_contents('/tmp/'.$dir.'/'.$document->filename().'.tex', $contents); if ($document->template()) { - $templatePath = resource_path("views/tex/templates/{$document->template()}"); - exec('cp '.$templatePath.'/* /tmp/'.$dir); + $templatePath = $document->template()->fullPath(); + exec('rsync -av '.escapeshellarg($templatePath).'/ /tmp/'.escapeshellarg($dir)); } - $command = 'cd /tmp/'.$dir; - $command .= ' && '.$document->getEngine().' --halt-on-error '.$document->filename().'.tex'; - $command .= ' && '.$document->getEngine().' --halt-on-error '.$document->filename().'.tex'; + $command = collect([ + 'cd /tmp/'.escapeshellarg($dir), + $document->getEngine()->binary().' --halt-on-error '.$document->filename().'.tex', + $document->getEngine()->binary().' --halt-on-error '.$document->filename().'.tex', + ])->implode(' && '); + exec($command, $output, $returnVar); + dd($output); if (file_exists('/tmp/'.$dir.'/'.$document->filename().'.pdf')) { $this->file = new File('/tmp/'.$dir.'/'.$document->filename().'.pdf'); diff --git a/src/CompilerException.php b/src/CompilerException.php new file mode 100644 index 0000000..897e4d7 --- /dev/null +++ b/src/CompilerException.php @@ -0,0 +1,9 @@ +make($this->view(), get_object_vars($this)) + ->render(); } } diff --git a/src/Engine.php b/src/Engine.php new file mode 100644 index 0000000..18603c9 --- /dev/null +++ b/src/Engine.php @@ -0,0 +1,23 @@ +value.'.binary'; + $binary = config($configPath); + + throw_if( + !$binary, + CompilerException::class, + 'Binary for engine '.$this->value.' not found. Please specify a binary file via the config '.$configPath.'.', + ); + + return config('tex.'.$this->value.'.binary'); + } +} diff --git a/src/Template.php b/src/Template.php new file mode 100644 index 0000000..8adf1d9 --- /dev/null +++ b/src/Template.php @@ -0,0 +1,30 @@ +exists(), + CompilerException::class, + 'Path for template '.$this->resourcePath.' not found. Please make sure the template directory '.$this->fullPath().' exists' + ); + } + + public static function make(string $resourcePath): self + { + return new static($resourcePath); + } + + public function fullPath(): string + { + return resource_path('views/'.str_replace('.', '/', $this->resourcePath)); + } + + public function exists(): bool + { + return is_dir($this->fullPath()); + } +} diff --git a/src/TexServiceProvider.php b/src/TexServiceProvider.php index 9efc33e..54375e0 100644 --- a/src/TexServiceProvider.php +++ b/src/TexServiceProvider.php @@ -15,12 +15,14 @@ class TexServiceProvider extends ServiceProvider public function register() { view()->addExtension('tex', 'tex', function () { - $compiler = new TexCompiler(app('files'), config('view.compiled')); + $compiler = new BladeCompiler(app('files'), config('view.compiled')); return new CompilerEngine($compiler, app('files')); }); app()->bind('tex.compiler', Compiler::class); + + $this->mergeConfigFrom(__DIR__.'/../config/tex.php', 'tex'); } /**