From 459c574331475dbd5498281c3daefbb3634288f4 Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Thu, 20 Feb 2025 10:13:55 +0100 Subject: [PATCH 1/6] Only apply default value when config option is not found --- src/Models/Config.php | 9 +++++---- src/Rapidez.php | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Models/Config.php b/src/Models/Config.php index ec598cc75..0a6de3dc3 100644 --- a/src/Models/Config.php +++ b/src/Models/Config.php @@ -59,7 +59,7 @@ public function scopeWhereDefault(Builder $query): void */ public static function getCachedByPath(string $path, $default = false, bool $sensitive = false): string|bool { - return static::getValue($path, options: ['cache' => true, 'decrypt' => $sensitive]) ?? $default; + return static::getValue($path, options: ['cache' => true, 'decrypt' => $sensitive, 'default' => $default]); } /** @@ -101,7 +101,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,8 +109,9 @@ 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); 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) From 76432a90fd0e4f5c3e1f13263da059a2981557d0 Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Thu, 20 Feb 2025 11:10:17 +0100 Subject: [PATCH 2/6] Write test for nullable config --- src/Models/Config.php | 3 +++ tests/Feature/ConfigTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/Feature/ConfigTest.php diff --git a/src/Models/Config.php b/src/Models/Config.php index 0a6de3dc3..9dc782a74 100644 --- a/src/Models/Config.php +++ b/src/Models/Config.php @@ -21,6 +21,9 @@ 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) { diff --git a/tests/Feature/ConfigTest.php b/tests/Feature/ConfigTest.php new file mode 100644 index 000000000..7b8eece32 --- /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(); + } +} From f194fb67dbedd60eb0e30fb6a254ca90ddb76e01 Mon Sep 17 00:00:00 2001 From: Jade-GG Date: Thu, 20 Feb 2025 10:10:46 +0000 Subject: [PATCH 3/6] Apply fixes from Duster --- src/Models/Config.php | 1 + tests/Feature/ConfigTest.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Models/Config.php b/src/Models/Config.php index 9dc782a74..f8e56fe90 100644 --- a/src/Models/Config.php +++ b/src/Models/Config.php @@ -22,6 +22,7 @@ class Config extends Model protected $primaryKey = 'config_id'; const CREATED_AT = null; + protected $guarded = []; protected static function booting() diff --git a/tests/Feature/ConfigTest.php b/tests/Feature/ConfigTest.php index 7b8eece32..8776bbf62 100644 --- a/tests/Feature/ConfigTest.php +++ b/tests/Feature/ConfigTest.php @@ -14,7 +14,7 @@ class ConfigTest extends TestCase public function config_can_be_intentionally_null() { $test = Config::create([ - 'path' => 'rapidez/test/value_null', + 'path' => 'rapidez/test/value_null', 'value' => null, ]); From f11084a0a8af1798c5ae26e3c896ecff75019d50 Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Thu, 20 Feb 2025 14:47:14 +0100 Subject: [PATCH 4/6] Update all uses of deprecated getCachedByPath() --- src/Listeners/Healthcheck/MagentoSettingsHealthcheck.php | 6 +++--- src/Models/Config.php | 4 ++-- src/Models/Product.php | 5 ++--- src/RapidezServiceProvider.php | 4 +--- src/helpers.php | 7 ++++--- 5 files changed, 12 insertions(+), 14 deletions(-) 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 f8e56fe90..7f72268f0 100644 --- a/src/Models/Config.php +++ b/src/Models/Config.php @@ -61,7 +61,7 @@ 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' => $default]); } @@ -73,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'], 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/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..28111e6c5 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,11 +1,12 @@ formatCurrency($price, $currency); From ab95e0046ef61fa44196e20d496e686d9b4b640d Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Thu, 20 Feb 2025 14:50:13 +0100 Subject: [PATCH 5/6] Remove use --- src/helpers.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index 28111e6c5..b002fa9af 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,12 +1,10 @@ formatCurrency($price, $currency); From 0aa9cf402a2af80e26e87094bc93f865d4a0fe52 Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Mon, 24 Feb 2025 11:18:03 +0100 Subject: [PATCH 6/6] Cache `false` when config value not found --- src/Models/Config.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Models/Config.php b/src/Models/Config.php index 7f72268f0..f7658e0b0 100644 --- a/src/Models/Config.php +++ b/src/Models/Config.php @@ -98,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; } @@ -118,7 +121,7 @@ public static function getValue( $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); }