From 6ccce874a4b1887b769d20f8b5394f904a6fbc83 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Tue, 25 Feb 2025 15:55:33 +0100 Subject: [PATCH] refactor: better debug logs --- src/Assets/Asset.php | 16 ++++++++++------ src/Util/Str.php | 12 ++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index e3bb65cc6..9e8d48ca3 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -172,6 +172,7 @@ public function __construct(Builder $builder, string|array $paths, array|null $o $this->data['files'][] = $file[$i]['filepath']; } $cache->set($cacheKey, $this->data); + $this->builder->getLogger()->debug(\sprintf('Asset created: "%s"', $this->data['path'])); } $this->data = $cache->get($cacheKey); @@ -308,7 +309,7 @@ public function compile(): self // debug if ($this->builder->isDebug()) { $scssPhp->setQuietDeps(false); - $this->builder->getLogger()->debug(\sprintf("SCSS compiler imported paths:\n%s", (string) implode("\n", array_unique($importDir)))); + $this->builder->getLogger()->debug(\sprintf("SCSS compiler imported paths:\n%s", Util\Str::arrayToList(array_unique($importDir)))); } // update data $this->data['path'] = preg_replace('/sass|scss/m', 'css', $this->data['path']); @@ -319,6 +320,7 @@ public function compile(): self $this->data['size'] = \strlen($this->data['content']); $this->compiled = true; $cache->set($cacheKey, $this->data); + $this->builder->getLogger()->debug(\sprintf('Asset compiled: "%s"', $this->data['path'])); } $this->data = $cache->get($cacheKey); @@ -377,6 +379,7 @@ public function minify(): self $this->data['size'] = \strlen($this->data['content']); $this->minified = true; $cache->set($cacheKey, $this->data); + $this->builder->getLogger()->debug(\sprintf('Asset minified: "%s"', $this->data['path'])); } $this->data = $cache->get($cacheKey); @@ -400,14 +403,14 @@ public function optimize(string $filepath): self } $cacheKey = $cache->createKeyFromAsset($this, $tags); if (!$cache->has($cacheKey)) { - $message = $filepath; + $message = \sprintf('Asset processed: "%s"', $this->data['path']); $sizeBefore = filesize($filepath); Optimizer::create($quality)->optimize($filepath); $sizeAfter = filesize($filepath); if ($sizeAfter < $sizeBefore) { $message = \sprintf( - '%s (%s Ko -> %s Ko)', - $message, + 'Asset optimized: "%s" (%s Ko -> %s Ko)', + $this->data['path'], ceil($sizeBefore / 1000), ceil($sizeAfter / 1000) ); @@ -415,7 +418,7 @@ public function optimize(string $filepath): self $this->data['content'] = Util\File::fileGetContents($filepath); $this->data['size'] = $sizeAfter; $cache->set($cacheKey, $this->data); - $this->builder->getLogger()->debug(\sprintf('Asset "%s" optimized', $message)); + $this->builder->getLogger()->debug($message); } $this->data = $cache->get($cacheKey, $this->data); @@ -461,6 +464,7 @@ public function resize(int $width): self $assetResized->data['size'] = \strlen($assetResized->data['content']); $cache->set($cacheKey, $assetResized->data); + $this->builder->getLogger()->debug(\sprintf('Asset resized: "%s" (%sx)', $assetResized->data['path'], $width)); } $assetResized->data = $cache->get($cacheKey); @@ -500,8 +504,8 @@ public function convert(string $format, ?int $quality = null): self $asset->data['path'] = preg_replace('/\.' . $this->data['ext'] . '$/m', ".$format", $this->data['path']); $asset->data['subtype'] = "image/$format"; $asset->data['size'] = \strlen($asset->data['content']); - $cache->set($cacheKey, $asset->data); + $this->builder->getLogger()->debug(\sprintf('Asset converted: "%s" (%s -> %s)', $asset->data['path'], $this->data['ext'], $format)); } $asset->data = $cache->get($cacheKey); diff --git a/src/Util/Str.php b/src/Util/Str.php index 8f343e86c..8d57d98ae 100644 --- a/src/Util/Str.php +++ b/src/Util/Str.php @@ -37,6 +37,18 @@ public static function combineArrayToString( return substr($string, 0, -2); } + /** + * Returns a string representation of an array. + */ + public static function arrayToList(array $array): string + { + array_walk($array, function (&$value) { + $value = \sprintf(" - %s", $value); + }); + + return implode("\n", $array); + } + /** * Converts 'true', 'false', 'on', 'off', 'yes', 'no' to a boolean. *