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'); } /**