fix: 🐛 use blade directives helper to prevent blade files from being cached #76 #77

This commit is contained in:
Alexis Gousseau 2026-02-02 15:47:55 +01:00
parent dc49a08128
commit ae54ee559b
2 changed files with 86 additions and 43 deletions

View file

@ -0,0 +1,80 @@
<?php
namespace App\Helpers;
use Illuminate\Support\Facades\Auth;
use App\Models\SellUnit;
use \Illuminate\Support\Str;
class BladeDirectivesHelper
{
/**
* Format specific value.
* Used for blade directive @fmt_currency_without_auth.
*
* @param mixed $value
* @return string
*/
private static function formatPriceValue($value): string
{
if (is_float($value)) {
return sprintf('%s €', number_format($value, 2, ',', ' '));
}
return $value;
}
/**
* Show the formated value only if authorized.
* Used for blade directive @fmt_currency and @fmt_currency_without_ttc.
*
* @param mixed $value
* @return string
*/
public static function formatCurrency($value): string
{
if (!self::shouldShowPrices()) {
return '-.--';
}
return self::formatPriceValue($value);
}
/**
* Check if prices must be shown or not.
*
* @return bool
*/
private static function shouldShowPrices(): bool
{
return (config('bosettings.show_prices_authenticated') === "authenticated" && Auth::guard('customer')->check())
|| config('bosettings.show_prices_guest') == "guest";
}
/**
* Format the sales unit according to configuration.
* Used for blade directive @fmt_sellunit.
*
* @param mixed $expression
* @return string
*/
public static function formatSellUnit($expression): string
{
if (config('bosettings.sell_unit_management') == "strict") {
return !empty($expression) ? sprintf('/ %s', SellUnit::make($expression)->label) : '';
}
return !empty($expression) ? sprintf('/ %s', $expression) : '';
}
/**
* Extract a title produced according to the theme configuration.
* Used for blade directive @excerpt_product_title.
*
* @param string $expression
* @param int|null $limit
* @return string
*/
public static function excerptTitle(string $expression, $limit = null): string
{
$limit = $limit ?? config('theme.max_product_title_length');
return Str::limit($expression, $limit);
}
}

View file

@ -71,58 +71,21 @@ class AppServiceProvider extends ServiceProvider
View::share('published_tags', $tags);
}
// Use blade directives helper to prevent blade files from being cached.
Blade::directive('fmt_currency_without_auth', function ($expression) {
return "<?php
if(is_float($expression)) {
echo sprintf('%s €', number_format($expression, 2, ',', ' '));
} else {
echo $expression;
} ?>";
return "<?php echo \App\Helpers\BladeDirectivesHelper::formatPriceValue($expression); ?>";
});
Blade::directive('fmt_currency', function ($expression) {
// Prix transmis en fonction du setting show_prices.
if (
(config('bosettings.show_prices_authenticated') === "authenticated" && Auth::guard('customer')->check())
|| config('bosettings.show_prices_guest') == "guest"
) {
return "<?php
if(is_float($expression)) {
echo sprintf('%s €', number_format($expression, 2, ',', ' '));
} else {
echo $expression;
} ?>";
}
return "-.--";
return "<?php echo \App\Helpers\BladeDirectivesHelper::formatCurrency($expression); ?>";
});
Blade::directive('fmt_currency_without_ttc', function ($expression) {
// Prix transmis en fonction du setting show_prices.
if (
(config('bosettings.show_prices_authenticated') === "authenticated" && Auth::guard('customer')->check())
|| config('bosettings.show_prices_guest') == "guest"
) {
return "<?php
if(is_float($expression)) {
echo sprintf('%s €', number_format($expression, 2, ',', ' '));
} else {
echo $expression;
} ?>";
}
return "-.--";
return "<?php echo \App\Helpers\BladeDirectivesHelper::formatCurrency($expression); ?>";
});
Blade::directive('fmt_sellunit', function ($expression) {
if (config('bosettings.sell_unit_management') == "strict") {
return "<?php if(!empty($expression)) { echo sprintf('/ %s', SellUnit::make($expression)->label); } ?>";
} else {
return "<?php if(!empty($expression)) { echo sprintf('/ %s', $expression); } ?>";
}
return "<?php echo \App\Helpers\BladeDirectivesHelper::formatSellUnit($expression); ?>";
});
Blade::directive('excerpt_product_title', function ($expression) {
$limit = config('theme.max_product_title_length');
return "<?php echo Illuminate\Support\Str::limit($expression,$limit) ?>";
return "<?php echo \App\Helpers\BladeDirectivesHelper::excerptTitle($expression); ?>";
});
// Default paginator template for back Office.