Skip to content

Commit

Permalink
refactor: better debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Feb 25, 2025
1 parent 0242138 commit 6ccce87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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']);
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -400,22 +403,22 @@ 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)
);
}
$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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
12 changes: 12 additions & 0 deletions src/Util/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 6ccce87

Please sign in to comment.