diff --git a/src/Listeners/Healthcheck/MagentoSettingsHealthcheck.php b/src/Listeners/Healthcheck/MagentoSettingsHealthcheck.php index 170baef2e..bdaeae531 100644 --- a/src/Listeners/Healthcheck/MagentoSettingsHealthcheck.php +++ b/src/Listeners/Healthcheck/MagentoSettingsHealthcheck.php @@ -5,6 +5,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\DB; use PDOException; +use Rapidez\Core\Facades\Rapidez; class MagentoSettingsHealthcheck extends Base { @@ -25,8 +26,7 @@ public function handle() return $response; } - $configModel = config('rapidez.models.config'); - if (! $configModel::getCachedByPath('catalog/frontend/flat_catalog_product', 0)) { + if (! Rapidez::config('catalog/frontend/flat_catalog_product', 0)) { $response['messages'][] = ['type' => 'error', 'value' => __( 'The product flat tables are disabled!' . PHP_EOL . 'Please enable them; see: https://docs.rapidez.io/3.x/installation.html#flat-tables' @@ -40,7 +40,7 @@ public function handle() $response['messages'][] = ['type' => 'error', 'value' => __('Flat table ":flatTable" is missing! Don\'t forget to run bin/magento indexer:reindex', ['flatTable' => $flatTable])]; } - if (! $configModel::getCachedByPath('catalog/frontend/flat_catalog_category', 0)) { + if (! Rapidez::config('catalog/frontend/flat_catalog_category', 0)) { $response['messages'][] = ['type' => 'error', 'value' => __('The category flat tables are disabled!')]; } diff --git a/src/Models/Config.php b/src/Models/Config.php index ec598cc75..f7658e0b0 100644 --- a/src/Models/Config.php +++ b/src/Models/Config.php @@ -21,6 +21,10 @@ class Config extends Model protected $primaryKey = 'config_id'; + const CREATED_AT = null; + + protected $guarded = []; + protected static function booting() { static::addGlobalScope('scope-fallback', function (Builder $builder) { @@ -57,9 +61,9 @@ public function scopeWhereDefault(Builder $query): void /** * @deprecated see: Config::getValue */ - public static function getCachedByPath(string $path, $default = false, bool $sensitive = false): string|bool + public static function getCachedByPath(string $path, $default = false, bool $sensitive = false): string|bool|null { - return static::getValue($path, options: ['cache' => true, 'decrypt' => $sensitive]) ?? $default; + return static::getValue($path, options: ['cache' => true, 'decrypt' => $sensitive, 'default' => $default]); } /** @@ -69,7 +73,7 @@ public static function getValue( string $path, ConfigScopes $scope = ConfigScopes::SCOPE_STORE, ?int $scopeId = null, - array $options = ['cache' => true, 'decrypt' => false] + array $options = ['cache' => true, 'decrypt' => false, 'default' => null] ): mixed { $scopeId ??= match ($scope) { ConfigScopes::SCOPE_WEBSITE => config('rapidez.website') ?? Rapidez::getStore(config('rapidez.store'))['website_id'], @@ -94,6 +98,9 @@ public static function getValue( // Catch the case it is intentionally set to null if (Arr::has($configCache, $cacheKey)) { $result = Arr::get($configCache, $cacheKey); + if ($result === false) { + $result = $options['default'] ?? null; + } return (bool) $options['decrypt'] && is_string($result) ? static::decrypt($result) : $result; } @@ -101,7 +108,7 @@ public static function getValue( $websiteId = $scope === ConfigScopes::SCOPE_STORE ? Rapidez::getStore($scopeId)['website_id'] : $scopeId; - $result = static::query() + $resultObject = static::query() ->withoutGlobalScope('scope-fallback') ->where('path', $path) ->where(fn ($query) => $query @@ -109,11 +116,12 @@ public static function getValue( ->when($scope !== ConfigScopes::SCOPE_DEFAULT, fn ($query) => $query->orWhere(fn ($query) => $query->whereWebsite($websiteId))) ->orWhere(fn ($query) => $query->whereDefault()) ) - ->first('value') - ?->value; + ->first('value'); + + $result = $resultObject ? $resultObject->value : ($options['default'] ?? null); if (($options['cache'] ?? true) && isset($cacheKey)) { - Arr::set($configCache, $cacheKey, $result); + Arr::set($configCache, $cacheKey, $resultObject ? $result : false); Cache::driver('rapidez:multi')->set('magento.config', $configCache); } diff --git a/src/Models/Product.php b/src/Models/Product.php index e8f2664e6..896ce1b9a 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -12,6 +12,7 @@ use Rapidez\Core\Casts\CommaSeparatedToArray; use Rapidez\Core\Casts\CommaSeparatedToIntegerArray; use Rapidez\Core\Casts\DecodeHtmlEntities; +use Rapidez\Core\Facades\Rapidez; use Rapidez\Core\Models\Scopes\Product\WithProductAttributesScope; use Rapidez\Core\Models\Scopes\Product\WithProductCategoryInfoScope; use Rapidez\Core\Models\Scopes\Product\WithProductChildrenScope; @@ -202,9 +203,7 @@ public function getSpecialPriceAttribute($specialPrice) public function getUrlAttribute(): string { - $configModel = config('rapidez.models.config'); - - return '/' . ($this->url_key ? $this->url_key . $configModel::getCachedByPath('catalog/seo/product_url_suffix', '.html') : 'catalog/product/view/id/' . $this->entity_id); + return '/' . ($this->url_key ? $this->url_key . Rapidez::config('catalog/seo/product_url_suffix', '.html') : 'catalog/product/view/id/' . $this->entity_id); } public function getImagesAttribute(): array diff --git a/src/Rapidez.php b/src/Rapidez.php index e2c17c2fd..3108af4ce 100644 --- a/src/Rapidez.php +++ b/src/Rapidez.php @@ -60,7 +60,7 @@ public function getAllFallbackRoutes() public function config(string $path, $default = null, bool $sensitive = false): ?string { - return config('rapidez.models.config')::getValue($path, options: ['cache' => true, 'decrypt' => $sensitive]) ?? $default; + return config('rapidez.models.config')::getValue($path, options: ['cache' => true, 'decrypt' => $sensitive, 'default' => $default]); } public function content($content) diff --git a/src/RapidezServiceProvider.php b/src/RapidezServiceProvider.php index 428bb3e99..59e256ea6 100644 --- a/src/RapidezServiceProvider.php +++ b/src/RapidezServiceProvider.php @@ -226,9 +226,7 @@ protected function registerBladeDirectives(): self }); Blade::directive('config', function ($expression) { - $configModel = config('rapidez.models.config'); - - return ""; + return ""; }); Blade::if('storecode', function ($value) { diff --git a/src/helpers.php b/src/helpers.php index 9563d023c..b002fa9af 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -3,9 +3,8 @@ if (! function_exists('price')) { function price($price) { - $configModel = config('rapidez.models.config'); - $currency = $configModel::getCachedByPath('currency/options/default'); - $locale = $configModel::getCachedByPath('general/locale/code', 'en_US'); + $currency = \Rapidez\Core\Facades\Rapidez::config('currency/options/default'); + $locale = \Rapidez\Core\Facades\Rapidez::config('general/locale/code', 'en_US'); $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); return $formatter->formatCurrency($price, $currency); diff --git a/tests/Feature/ConfigTest.php b/tests/Feature/ConfigTest.php new file mode 100644 index 000000000..8776bbf62 --- /dev/null +++ b/tests/Feature/ConfigTest.php @@ -0,0 +1,26 @@ + 'rapidez/test/value_null', + 'value' => null, + ]); + + $this->assertNull(Rapidez::config('rapidez/test/value_null', 'default')); + $this->assertEquals('default', Rapidez::config('rapidez/test/nonexistent_value', 'default')); + + $test->delete(); + } +}