From 8abac26b58f1615e844325a3a81edcfcbfc4f3d1 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Wed, 26 Oct 2022 16:13:19 +0200 Subject: [PATCH] wip --- composer.json | 9 +++- src/{TexCompiler.php => BladeCompiler.php} | 4 +- src/Compiler.php | 11 ++++ src/CompilerSpy.php | 60 ++++++++++++++++++++++ src/Document.php | 21 ++++++++ src/Tex.php | 28 ++++++++++ src/TexServiceProvider.php | 2 + 7 files changed, 132 insertions(+), 3 deletions(-) rename src/{TexCompiler.php => BladeCompiler.php} (53%) create mode 100644 src/Compiler.php create mode 100644 src/CompilerSpy.php create mode 100644 src/Document.php create mode 100644 src/Tex.php diff --git a/composer.json b/composer.json index 008b7a8..5959e0c 100644 --- a/composer.json +++ b/composer.json @@ -11,5 +11,12 @@ "email": "philipp@zoomyboy.de" } ], - "require": {} + "require": {}, + "extra": { + "laravel": { + "providers": [ + "Zoomyboy\\Tex\\TexServiceProvider" + ] + } + } } diff --git a/src/TexCompiler.php b/src/BladeCompiler.php similarity index 53% rename from src/TexCompiler.php rename to src/BladeCompiler.php index 11696b3..ef9532b 100644 --- a/src/TexCompiler.php +++ b/src/BladeCompiler.php @@ -2,9 +2,9 @@ namespace Zoomyboy\Tex; -use Illuminate\View\Compilers\BladeCompiler; +use Illuminate\View\Compilers\BladeCompiler as BaseBladeCompiler; -class TexCompiler extends BladeCompiler +class BladeCompiler extends BaseBladeCompiler { protected $contentTags = ['<<<', '>>>']; diff --git a/src/Compiler.php b/src/Compiler.php new file mode 100644 index 0000000..9b2ee96 --- /dev/null +++ b/src/Compiler.php @@ -0,0 +1,11 @@ + + */ + private array $compiledDocuments = []; + + /** + * @param class-string $documentClass + * @param callable(Document): bool $check + */ + public function assertCompiled(string $documentClass, callable $check): void + { + $compilations = $this->getCompilations($documentClass); + Assert::assertFalse( + $compilations->isEmpty(), + 'The TeX Document "'.$documentClass.'" has not been compiled at all.' + ); + + Assert::assertFalse( + $compilations->isEmpty(), + 'The TeX Document "'.$documentClass.'" has not been compiled.' + ); + + foreach ($compilations as $compilation) { + } + + Assert::assertFalse( + $this->getCompilations($documentClass)->isEmpty(), + 'The TeX Document "'.$documentClass.'" has not been compiled.' + ); + } + + /** + * @param class-string $documentClass + * + * @return Collection + */ + protected function getCompilations(string $documentClass): Collection + { + return collect($this->compiledDocuments)->filter(fn ($rendered) => get_class($rendered) === $documentClass); + } + + public function compile(Document $document): string + { + $filename = static::$actualCompiler->compile($document); + + $this->compiledDocuments[] = $document; + + return $filename; + } +} diff --git a/src/Document.php b/src/Document.php new file mode 100644 index 0000000..29e7228 --- /dev/null +++ b/src/Document.php @@ -0,0 +1,21 @@ +renderBody(), + ); + } + + protected function renderBody(): string + { + return 'gggggMax'; + } +} diff --git a/src/Tex.php b/src/Tex.php new file mode 100644 index 0000000..fabbecc --- /dev/null +++ b/src/Tex.php @@ -0,0 +1,28 @@ + $documentClass, callable(Document): bool $check) + * @method static void compile(Document $document) + */ +class Tex extends Facade +{ + public static function getFacadeAccessor(): string + { + return 'tex.compiler'; + } + + public static function spy(): void + { + CompilerSpy::$actualCompiler = app(Compiler::class); + static::swap(app(CompilerSpy::class)); + } + + public static function fake(): void + { + static::swap(app(CompilerFake::class)); + } +} diff --git a/src/TexServiceProvider.php b/src/TexServiceProvider.php index 1ffadeb..9efc33e 100644 --- a/src/TexServiceProvider.php +++ b/src/TexServiceProvider.php @@ -19,6 +19,8 @@ class TexServiceProvider extends ServiceProvider return new CompilerEngine($compiler, app('files')); }); + + app()->bind('tex.compiler', Compiler::class); } /**