Skip to content

Commit

Permalink
Only apply default value when config option is not found (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jade-GG authored Feb 25, 2025
1 parent e585838 commit 3b765f0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/Listeners/Healthcheck/MagentoSettingsHealthcheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use PDOException;
use Rapidez\Core\Facades\Rapidez;

class MagentoSettingsHealthcheck extends Base
{
Expand All @@ -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'
Expand All @@ -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!')];
}

Expand Down
22 changes: 15 additions & 7 deletions src/Models/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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]);
}

/**
Expand All @@ -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'],
Expand All @@ -94,26 +98,30 @@ 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;
}
}

$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
->when($scope === ConfigScopes::SCOPE_STORE, fn ($query) => $query->whereStore($scopeId))
->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);
}

Expand Down
5 changes: 2 additions & 3 deletions src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Rapidez.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions src/RapidezServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,7 @@ protected function registerBladeDirectives(): self
});

Blade::directive('config', function ($expression) {
$configModel = config('rapidez.models.config');

return "<?php echo {$configModel}::getCachedByPath({$expression}) ?>";
return "<?php echo Rapidez::config({$expression}) ?>";
});

Blade::if('storecode', function ($value) {
Expand Down
5 changes: 2 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Rapidez\Core\Tests\Feature;

use Rapidez\Core\Facades\Rapidez;
use Rapidez\Core\Models\Config;
use Rapidez\Core\Tests\TestCase;

class ConfigTest extends TestCase
{
/**
* @test
*/
public function config_can_be_intentionally_null()
{
$test = Config::create([
'path' => '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();
}
}

0 comments on commit 3b765f0

Please sign in to comment.