Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Only apply default value when config option is not found #737

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 11 additions & 6 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 Down Expand Up @@ -101,16 +105,17 @@ 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
->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);
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
7 changes: 4 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

use Rapidez\Core\Facades\Rapidez;

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::config('currency/options/default');
$locale = 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();
}
}