--wip-- [skip ci]

This commit is contained in:
Philipp Lang 2022-10-27 16:54:24 +02:00
parent 748ea8c9c9
commit 8892b11dc7
7 changed files with 91 additions and 11 deletions

10
config/tex.php Normal file
View File

@ -0,0 +1,10 @@
<?php
return [
'pdflatex' => [
'binary' => env('PDFLATEX_BIN', ''),
],
'xelatex' => [
'binary' => env('XELATEX_BIN', ''),
],
];

View File

@ -15,20 +15,24 @@ class Compiler implements Responsable
$filename = $document->filename(); $filename = $document->filename();
$dir = Str::random(32); $dir = Str::random(32);
$contents = view()->make($document->view(), get_object_vars($document))->render(); $contents = $document->renderBody();
mkdir('/tmp/'.$dir); mkdir('/tmp/'.$dir);
file_put_contents('/tmp/'.$dir.'/'.$document->filename().'.tex', $contents); file_put_contents('/tmp/'.$dir.'/'.$document->filename().'.tex', $contents);
if ($document->template()) { if ($document->template()) {
$templatePath = resource_path("views/tex/templates/{$document->template()}"); $templatePath = $document->template()->fullPath();
exec('cp '.$templatePath.'/* /tmp/'.$dir); exec('rsync -av '.escapeshellarg($templatePath).'/ /tmp/'.escapeshellarg($dir));
} }
$command = 'cd /tmp/'.$dir; $command = collect([
$command .= ' && '.$document->getEngine().' --halt-on-error '.$document->filename().'.tex'; 'cd /tmp/'.escapeshellarg($dir),
$command .= ' && '.$document->getEngine().' --halt-on-error '.$document->filename().'.tex'; $document->getEngine()->binary().' --halt-on-error '.$document->filename().'.tex',
$document->getEngine()->binary().' --halt-on-error '.$document->filename().'.tex',
])->implode(' && ');
exec($command, $output, $returnVar); exec($command, $output, $returnVar);
dd($output);
if (file_exists('/tmp/'.$dir.'/'.$document->filename().'.pdf')) { if (file_exists('/tmp/'.$dir.'/'.$document->filename().'.pdf')) {
$this->file = new File('/tmp/'.$dir.'/'.$document->filename().'.pdf'); $this->file = new File('/tmp/'.$dir.'/'.$document->filename().'.pdf');

View File

@ -0,0 +1,9 @@
<?php
namespace Zoomyboy\Tex;
use Exception;
class CompilerException extends Exception
{
}

View File

@ -10,9 +10,9 @@ abstract class Document
abstract public function view(): string; abstract public function view(): string;
abstract public function template(): ?string; abstract public function template(): ?Template;
abstract public function getEngine(): string; abstract public function getEngine(): Engine;
public function assertHasContent(string $content): void public function assertHasContent(string $content): void
{ {
@ -22,8 +22,10 @@ abstract class Document
); );
} }
protected function renderBody(): string public function renderBody(): string
{ {
return 'gggggMax'; return view()
->make($this->view(), get_object_vars($this))
->render();
} }
} }

23
src/Engine.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace Zoomyboy\Tex;
enum Engine: string
{
case PDFLATEX = 'pdflatex';
case XELATEX = 'xelatex';
public function binary(): string
{
$configPath = 'tex.'.$this->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');
}
}

30
src/Template.php Normal file
View File

@ -0,0 +1,30 @@
<?php
namespace Zoomyboy\Tex;
final class Template
{
private function __construct(private string $resourcePath)
{
throw_unless(
$this->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());
}
}

View File

@ -15,12 +15,14 @@ class TexServiceProvider extends ServiceProvider
public function register() public function register()
{ {
view()->addExtension('tex', 'tex', function () { 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')); return new CompilerEngine($compiler, app('files'));
}); });
app()->bind('tex.compiler', Compiler::class); app()->bind('tex.compiler', Compiler::class);
$this->mergeConfigFrom(__DIR__.'/../config/tex.php', 'tex');
} }
/** /**