From b6214c2371941c01336e7ecb01071b6b5bc7045e Mon Sep 17 00:00:00 2001 From: juniwalk Date: Wed, 24 Apr 2024 23:23:02 +0200 Subject: [PATCH] Refactored output printing --- src/TessaRenderer.php | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/TessaRenderer.php b/src/TessaRenderer.php index e6d6231..a38c820 100644 --- a/src/TessaRenderer.php +++ b/src/TessaRenderer.php @@ -43,27 +43,33 @@ public function clearHistory(): void public function renderCss(?string $bundle = null): void { - $type = Type::StyleSheet; + $this->renderType(Type::StyleSheet, $bundle); + } - if (!$bundle) { - $this->render($type); - return; - } - echo implode(PHP_EOL, $this->compile($bundle, $type)).PHP_EOL; + public function renderJs(?string $bundle = null): void + { + $this->renderType(Type::JavaScript, $bundle); } - public function renderJs(?string $bundle = null): void + /** + * @throws AssetTypeException + */ + public function renderType(Type|string $type, ?string $bundle = null): void { - $type = Type::JavaScript; + $type = Type::make($type, false) ?? $type; - if (!$bundle) { - $this->render($type); - return; + if (!$type instanceof Type) { + throw AssetTypeException::fromType($type); } - echo implode(PHP_EOL, $this->compile($bundle, $type)).PHP_EOL; + match (true) { + $bundle => $this->print( + $this->compile($bundle, $type) + ), + default => $this->render($type), + }; } @@ -96,7 +102,7 @@ public function render(Type|string $type): void $output = array_merge($output, $assets); } - echo implode(PHP_EOL, $output).PHP_EOL; + $this->print($output); } @@ -124,6 +130,15 @@ private function compile(string $bundle, Type $type): array } + /** + * @param Html[] $assets + */ + private function print(array $assets): void + { + echo implode(PHP_EOL, $assets).PHP_EOL; + } + + private function createStyleSheet(Asset $asset): ?Html { $path = $this->createFilePath($asset);