feat(overrides): add override functionality to Options

Signed-off-by: miguel456 <me@nogueira.codes>
This commit is contained in:
Miguel Nogueira 2023-06-29 17:20:41 +01:00
parent 68b60bd3f3
commit 05e2cd4f82
No known key found for this signature in database
GPG Key ID: 3C6A7E29AF26D370
6 changed files with 963 additions and 784 deletions

View File

@ -13,6 +13,19 @@ APP_AUTH_BANNER=""
APP_SITEHOMEPAGE="" APP_SITEHOMEPAGE=""
API_PREFIX="api" API_PREFIX="api"
# FEATURE OVERRIDES
# Feature overrides allow you to force-enable/disable features regardless of application settings and feature flags
# Turning on the override will disable its feature
# Possible values:
# KILLSWITCH: Disables the feature altogether
# FORCE_ENABLE: Enables the feature
# CONTROL: Lets the application settings and feature flags take effect (default)
# Require valid game license to sign up
OVERRIDE_LICENSED_ACCOUNT=CONTROL
# Add legal document links here. You may also leave them empty. # Add legal document links here. You may also leave them empty.
GUIDELINES_URL="#" GUIDELINES_URL="#"
PRIVACY_URL="#" PRIVACY_URL="#"

23
app/Enums/Overrides.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace App\Enums;
enum Overrides:int
{
/**
* Pass control to app settings
*/
case control = 0;
/**
* Forcefully enable feature
*/
case forceEnable = 1;
/**
* Forcefully disable feature
*/
case killSwitch = 2;
}

View File

@ -21,18 +21,19 @@
namespace App\Helpers; namespace App\Helpers;
use App\Enums\Overrides;
use App\Exceptions\EmptyOptionsException; use App\Exceptions\EmptyOptionsException;
use App\Exceptions\OptionNotFoundException; use App\Exceptions\OptionNotFoundException;
use App\Options as Option; use App\Options as Option;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use LogicException;
/**
* The options class. A simple wrapper around the model. Could be a repository, but we're not using that design pattern just yet
*/
class Options class Options
{ {
/** /**
* Returns an assortment of settings found in the mentioned category * Returns an assortment of settings found in the mentioned category
* *
@ -70,7 +71,33 @@ class Options
return $value; return $value;
} }
// Null categories are settings without categories and will appear ungrouped
/**
* CHeck if given option is locallu overriden
*
* @param string $option Option to check
* @throws LogicException Thrown when feature is not locally overridable, e.g. not defined in the settings file
* @return bool
*/
public function isOverriden(string $option): bool {
if (!array_key_exists($option, config('local-overrides.features'))) {
throw new LogicException('Feature does not exist or is not locally overridable');
}
$feature = config("local-overrides.features.$option");
return ($feature == Overrides::forceEnable->value || $feature == Overrides::killSwitch->value);
}
/**
* Defines an option to store in the database.
*
* @param string $option Option name.
* @param string $value Option value.
* @param string $description Description for this, can be null
* @param string|null $category Option category
* @return void
*/
public function setOption(string $option, string $value, string $description, string $category = null) public function setOption(string $option, string $value, string $description, string $category = null)
{ {
Option::create([ Option::create([
@ -84,6 +111,12 @@ class Options
Cache::put($option.'_desc', $description, now()->addDay()); Cache::put($option.'_desc', $description, now()->addDay());
} }
/**
* Gets and deletes option
*
* @param string $option Option to pull
* @return array
*/
public function pullOption($option): array public function pullOption($option): array
{ {
$oldOption = Option::where('option_name', $option)->first(); $oldOption = Option::where('option_name', $option)->first();
@ -96,6 +129,14 @@ class Options
]; ];
} }
/**
* Updates an option
*
* @param $option
* @param $newValue
* @return void
* @throws OptionNotFoundException
*/
public function changeOption($option, $newValue) public function changeOption($option, $newValue)
{ {
$dbOption = Option::where('option_name', $option); $dbOption = Option::where('option_name', $option);
@ -123,6 +164,12 @@ class Options
} }
} }
/**
* Check if option exists
*
* @param string $option
* @return bool
*/
public function optionExists(string $option): bool public function optionExists(string $option): bool
{ {
$dbOption = Option::where('option_name', $option)->first(); $dbOption = Option::where('option_name', $option)->first();
@ -130,4 +177,16 @@ class Options
return ! is_null($dbOption) || ! is_null($locallyCachedOption); return ! is_null($dbOption) || ! is_null($locallyCachedOption);
} }
/**
* Get the actual status for the override in question
*
* @param string $option
* @return bool|Overrides
*/
public function getOverride(string $option): bool|string {
return config("local-overrides.features.$option");
}
} }

View File

@ -47,7 +47,10 @@
"config": { "config": {
"optimize-autoloader": true, "optimize-autoloader": true,
"preferred-install": "dist", "preferred-install": "dist",
"sort-packages": true "sort-packages": true,
"allow-plugins": {
"php-http/discovery": true
}
}, },
"extra": { "extra": {
"laravel": { "laravel": {

1630
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
<?php
return [
'features' => [
'requireGameLicense' => env('OVERRIDE_LICENSED_ACCOUNT', 0)
]
];