Skip to content

Commit

Permalink
perf: compiler optim with sprintf + PHP 8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Aug 18, 2024
1 parent 83c40da commit a9936f9
Show file tree
Hide file tree
Showing 55 changed files with 228 additions and 228 deletions.
60 changes: 30 additions & 30 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public function __construct(Builder $builder, string|array $paths, array|null $o
$paths = \is_array($paths) ? $paths : [$paths];
array_walk($paths, function ($path) {
if (!\is_string($path)) {
throw new RuntimeException(sprintf('The path of an asset must be a string ("%s" given).', \gettype($path)));
throw new RuntimeException(\sprintf('The path of an asset must be a string ("%s" given).', \gettype($path)));
}
if (empty($path)) {
throw new RuntimeException('The path of an asset can\'t be empty.');
}
if (substr($path, 0, 2) == '..') {
throw new RuntimeException(sprintf('The path of asset "%s" is wrong: it must be directly relative to "assets" or "static" directory, or a remote URL.', $path));
throw new RuntimeException(\sprintf('The path of asset "%s" is wrong: it must be directly relative to "assets" or "static" directory, or a remote URL.', $path));
}
});
$this->data = [
Expand Down Expand Up @@ -107,7 +107,7 @@ public function __construct(Builder $builder, string|array $paths, array|null $o

// fill data array with file(s) informations
$cache = new Cache($this->builder, (string) $this->builder->getConfig()->get('cache.assets.dir'));
$cacheKey = sprintf('%s__%s', $filename ?: implode('_', $paths), $this->builder->getVersion());
$cacheKey = \sprintf('%s__%s', $filename ?: implode('_', $paths), $this->builder->getVersion());
if (!$cache->has($cacheKey)) {
$pathsCount = \count($paths);
$file = [];
Expand All @@ -117,7 +117,7 @@ public function __construct(Builder $builder, string|array $paths, array|null $o
// bundle: same type only
if ($i > 0) {
if ($file[$i]['type'] != $file[$i - 1]['type']) {
throw new RuntimeException(sprintf('Asset bundle type error (%s != %s).', $file[$i]['type'], $file[$i - 1]['type']));
throw new RuntimeException(\sprintf('Asset bundle type error (%s != %s).', $file[$i]['type'], $file[$i - 1]['type']));
}
}
// missing allowed = empty path
Expand Down Expand Up @@ -158,7 +158,7 @@ public function __construct(Builder $builder, string|array $paths, array|null $o
$filename = '/scripts.js';
break;
default:
throw new RuntimeException(sprintf('Asset bundle supports %s files only.', '.scss, .css and .js'));
throw new RuntimeException(\sprintf('Asset bundle supports %s files only.', '.scss, .css and .js'));
}
}
// bundle: filename and path
Expand Down Expand Up @@ -293,7 +293,7 @@ public function compile(): self
$outputStyles = ['expanded', 'compressed'];
$outputStyle = strtolower((string) $this->config->get('assets.compile.style'));
if (!\in_array($outputStyle, $outputStyles)) {
throw new ConfigException(sprintf('"%s" value must be "%s".', 'assets.compile.style', implode('" or "', $outputStyles)));
throw new ConfigException(\sprintf('"%s" value must be "%s".', 'assets.compile.style', implode('" or "', $outputStyles)));
}
$scssPhp->setOutputStyle($outputStyle);
// variables
Expand Down Expand Up @@ -358,7 +358,7 @@ public function minify(): self
$minifier = new Minify\JS($this->data['content']);
break;
default:
throw new RuntimeException(sprintf('Not able to minify "%s".', $this->data['path']));
throw new RuntimeException(\sprintf('Not able to minify "%s".', $this->data['path']));
}
$this->data['path'] = preg_replace(
'/\.' . $this->data['ext'] . '$/m',
Expand Down Expand Up @@ -397,7 +397,7 @@ public function optimize(string $filepath): self
Optimizer::create($quality)->optimize($filepath);
$sizeAfter = filesize($filepath);
if ($sizeAfter < $sizeBefore) {
$message = sprintf(
$message = \sprintf(
'%s (%s Ko -> %s Ko)',
$message,
ceil($sizeBefore / 1000),
Expand All @@ -407,7 +407,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(\sprintf('Asset "%s" optimized', $message));
}
$this->data = $cache->get($cacheKey, $this->data);

Expand All @@ -422,10 +422,10 @@ public function optimize(string $filepath): self
public function resize(int $width): self
{
if ($this->data['missing']) {
throw new RuntimeException(sprintf('Not able to resize "%s": file not found.', $this->data['path']));
throw new RuntimeException(\sprintf('Not able to resize "%s": file not found.', $this->data['path']));
}
if ($this->data['type'] != 'image') {
throw new RuntimeException(sprintf('Not able to resize "%s": not an image.', $this->data['path']));
throw new RuntimeException(\sprintf('Not able to resize "%s": not an image.', $this->data['path']));
}
if ($width >= $this->data['width']) {
return $this;
Expand Down Expand Up @@ -467,7 +467,7 @@ public function resize(int $width): self
public function convert(string $format, ?int $quality = null): self
{
if ($this->data['type'] != 'image') {
throw new RuntimeException(sprintf('Not able to convert "%s" (%s) to %s: not an image.', $this->data['path'], $this->data['type'], $format));
throw new RuntimeException(\sprintf('Not able to convert "%s" (%s) to %s: not an image.', $this->data['path'], $this->data['type'], $format));
}

if ($quality === null) {
Expand Down Expand Up @@ -566,7 +566,7 @@ public function offsetGet($offset)
*/
public function getIntegrity(string $algo = 'sha384'): string
{
return sprintf('%s-%s', $algo, base64_encode(hash($algo, $this->data['content'], true)));
return \sprintf('%s-%s', $algo, base64_encode(hash($algo, $this->data['content'], true)));
}

/**
Expand All @@ -577,7 +577,7 @@ public function getIntegrity(string $algo = 'sha384'): string
public function getAudio(): Mp3Info
{
if ($this->data['type'] !== 'audio') {
throw new RuntimeException(sprintf('Not able to get audio infos of "%s".', $this->data['path']));
throw new RuntimeException(\sprintf('Not able to get audio infos of "%s".', $this->data['path']));
}

return new Mp3Info($this->data['file']);
Expand All @@ -591,7 +591,7 @@ public function getAudio(): Mp3Info
public function getVideo(): array
{
if ($this->data['type'] !== 'video') {
throw new RuntimeException(sprintf('Not able to get video infos of "%s".', $this->data['path']));
throw new RuntimeException(\sprintf('Not able to get video infos of "%s".', $this->data['path']));
}

return \Clwu\Mp4::getInfo($this->data['file']);
Expand All @@ -608,7 +608,7 @@ public function dataurl(): string
return Image::getDataUrl($this, $this->config->get('assets.images.quality') ?? 75);
}

return sprintf('data:%s;base64,%s', $this->data['subtype'], base64_encode($this->data['content']));
return \sprintf('data:%s;base64,%s', $this->data['subtype'], base64_encode($this->data['content']));
}

/**
Expand All @@ -623,13 +623,13 @@ public function save(): void
if (!$this->builder->getBuildOptions()['dry-run'] && !Util\File::getFS()->exists($filepath)) {
try {
Util\File::getFS()->dumpFile($filepath, $this->data['content']);
$this->builder->getLogger()->debug(sprintf('Asset "%s" saved', $filepath));
$this->builder->getLogger()->debug(\sprintf('Asset "%s" saved', $filepath));
if ($this->optimize) {
$this->optimize($filepath);
}
} catch (\Symfony\Component\Filesystem\Exception\IOException) {
if (!$this->ignore_missing) {
throw new RuntimeException(sprintf('Can\'t save asset "%s".', $filepath));
throw new RuntimeException(\sprintf('Can\'t save asset "%s".', $filepath));
}
}
}
Expand Down Expand Up @@ -674,7 +674,7 @@ private function loadFile(string $path, bool $ignore_missing = false, ?string $r
return $file;
}

throw new RuntimeException(sprintf('Can\'t load asset file "%s" (%s).', $path, $e->getMessage()));
throw new RuntimeException(\sprintf('Can\'t load asset file "%s" (%s).', $path, $e->getMessage()));
}

if (Util\Url::isUrl($path)) {
Expand Down Expand Up @@ -733,7 +733,7 @@ private function findFile(string $path, ?string $remote_fallback = null): string
if (Util\Str::endsWith($urlPath, '/css') || Util\Str::endsWith($urlPath, '/css2')) {
$extension = 'css';
}
$relativePath = Page::slugify(sprintf(
$relativePath = Page::slugify(\sprintf(
'%s%s%s%s',
$urlHost,
$this->sanitize($urlPath),
Expand All @@ -745,13 +745,13 @@ private function findFile(string $path, ?string $remote_fallback = null): string
if (!file_exists($filePath)) {
try {
if (!Util\Url::isRemoteFileExists($url)) {
throw new RuntimeException(sprintf('File "%s" doesn\'t exists', $url));
throw new RuntimeException(\sprintf('File "%s" doesn\'t exists', $url));
}
if (false === $content = Util\File::fileGetContents($url, true)) {
throw new RuntimeException(sprintf('Can\'t get content of file "%s".', $url));
throw new RuntimeException(\sprintf('Can\'t get content of file "%s".', $url));
}
if (\strlen($content) <= 1) {
throw new RuntimeException(sprintf('File "%s" is empty.', $url));
throw new RuntimeException(\sprintf('File "%s" is empty.', $url));
}
} catch (RuntimeException $e) {
// is there a fallback in assets/
Expand All @@ -760,16 +760,16 @@ private function findFile(string $path, ?string $remote_fallback = null): string
if (Util\File::getFS()->exists($filePath)) {
return $filePath;
}
throw new RuntimeException(sprintf('Fallback file "%s" doesn\'t exists.', $filePath));
throw new RuntimeException(\sprintf('Fallback file "%s" doesn\'t exists.', $filePath));
}

throw new RuntimeException($e->getMessage());
}
if (false === $content = Util\File::fileGetContents($url, true)) {
throw new RuntimeException(sprintf('Can\'t get content of "%s"', $url));
throw new RuntimeException(\sprintf('Can\'t get content of "%s"', $url));
}
if (\strlen($content) <= 1) {
throw new RuntimeException(sprintf('Asset at "%s" is empty', $url));
throw new RuntimeException(\sprintf('Asset at "%s" is empty', $url));
}
// put file in cache
Util\File::getFS()->dumpFile($filePath, $content);
Expand Down Expand Up @@ -806,7 +806,7 @@ private function findFile(string $path, ?string $remote_fallback = null): string
}
}

throw new RuntimeException(sprintf('Can\'t find file "%s".', $path));
throw new RuntimeException(\sprintf('Can\'t find file "%s".', $path));
}

/**
Expand All @@ -823,7 +823,7 @@ private function getWidth(): int
return (int) $svg->width;
}
if (false === $size = $this->getImageSize()) {
throw new RuntimeException(sprintf('Not able to get width of "%s".', $this->data['path']));
throw new RuntimeException(\sprintf('Not able to get width of "%s".', $this->data['path']));
}

return $size[0];
Expand All @@ -843,7 +843,7 @@ private function getHeight(): int
return (int) $svg->height;
}
if (false === $size = $this->getImageSize()) {
throw new RuntimeException(sprintf('Not able to get height of "%s".', $this->data['path']));
throw new RuntimeException(\sprintf('Not able to get height of "%s".', $this->data['path']));
}

return $size[1];
Expand All @@ -867,7 +867,7 @@ private function getImageSize()
return false;
}
} catch (\Exception $e) {
throw new RuntimeException(sprintf('Handling asset "%s" failed: "%s"', $this->data['path_source'], $e->getMessage()));
throw new RuntimeException(\sprintf('Handling asset "%s" failed: "%s"', $this->data['path_source'], $e->getMessage()));
}

return $size;
Expand Down
18 changes: 9 additions & 9 deletions src/Assets/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,23 @@ public function clear(): bool
*/
public function getMultiple($keys, $default = null): iterable
{
throw new \Exception(sprintf('%s::%s not yet implemented.', __CLASS__, __FUNCTION__));
throw new \Exception(\sprintf('%s::%s not yet implemented.', __CLASS__, __FUNCTION__));
}

/**
* {@inheritdoc}
*/
public function setMultiple($values, $ttl = null): bool
{
throw new \Exception(sprintf('%s::%s not yet implemented.', __CLASS__, __FUNCTION__));
throw new \Exception(\sprintf('%s::%s not yet implemented.', __CLASS__, __FUNCTION__));
}

/**
* {@inheritdoc}
*/
public function deleteMultiple($keys): bool
{
throw new \Exception(sprintf('%s::%s not yet implemented.', __CLASS__, __FUNCTION__));
throw new \Exception(\sprintf('%s::%s not yet implemented.', __CLASS__, __FUNCTION__));
}

/**
Expand Down Expand Up @@ -171,10 +171,10 @@ public function createKeyFromString(string $value): string
public function createKeyFromPath(string $path, string $relativePath): string
{
if (false === $content = Util\File::fileGetContents($path)) {
throw new RuntimeException(sprintf('Can\'t create cache key for "%s".', $path));
throw new RuntimeException(\sprintf('Can\'t create cache key for "%s".', $path));
}

return $this->prepareKey(sprintf('%s__%s', $relativePath, $this->createKeyFromString($content)));
return $this->prepareKey(\sprintf('%s__%s', $relativePath, $this->createKeyFromString($content)));
}

/**
Expand All @@ -184,7 +184,7 @@ public function createKeyFromAsset(Asset $asset, array $tags = null): string
{
$tags = implode('_', $tags ?? []);

return $this->prepareKey(sprintf(
return $this->prepareKey(\sprintf(
'%s%s%s__%s__%s',
$asset['filename'],
"_{$asset['ext']}",
Expand All @@ -201,7 +201,7 @@ public function clearByPattern(string $pattern): int
{
try {
if (!Util\File::getFS()->exists($this->cacheDir)) {
throw new RuntimeException(sprintf('Can\'t remove cache directory "%s".', $this->cacheDir));
throw new RuntimeException(\sprintf('Can\'t remove cache directory "%s".', $this->cacheDir));
}
$fileCount = 0;
$iterator = new \RecursiveIteratorIterator(
Expand All @@ -213,7 +213,7 @@ public function clearByPattern(string $pattern): int
if (preg_match('/' . $pattern . '/i', $file->getPathname())) {
Util\File::getFS()->remove($file->getPathname());
$fileCount++;
$this->builder->getLogger()->debug(sprintf('Cache file "%s" removed', $file->getPathname()));
$this->builder->getLogger()->debug(\sprintf('Cache file "%s" removed', $file->getPathname()));
}
}
}
Expand All @@ -231,7 +231,7 @@ public function clearByPattern(string $pattern): int
*/
private function getFilePathname(string $key): string
{
return Util::joinFile($this->cacheDir, sprintf('%s.ser', $key));
return Util::joinFile($this->cacheDir, \sprintf('%s.ser', $key));
}

/**
Expand Down
Loading

0 comments on commit a9936f9

Please sign in to comment.