Skip to content

Commit

Permalink
Add Api Guard & Fix Cache Key
Browse files Browse the repository at this point in the history
  • Loading branch information
anisAronno committed Nov 2, 2023
1 parent d32735e commit dea8108
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [Run Seeder](#run-seeder)
- [Define User Model Relation (Optional)](#define-user-model-relation-optional)
- [Usage](#usage)
- [Check if a Key Exists in Database](#check-if-a-key-exists-in-database)
- [Get Settings Field](#get-settings-field)
- [Get All Settings Fields](#get-all-settings-fields)
- [Set Settings](#set-settings)
Expand Down Expand Up @@ -65,6 +66,13 @@ public function settingsProperties(): HasMany
## Usage
The package provides methods for managing settings. Here are the available functions:

### Check if a Key Exists in Database
You can use the `hasSettings` method to check if a key exists in the database:

```php
hasSettings(string $key);
```

### Get Settings Field
Retrieve a specific setting using its key:

Expand Down Expand Up @@ -110,12 +118,24 @@ deleteSettings(string $key);
## Settings Table CRUD
To manage your settings table, you can use the following routes:

- Get all settings: `api/v1/settings` (GET) (name: `settings.index`)
- Get a single setting: `api/v1/settings/{setting_key}` (GET) (name: `settings.show`)
- Get all settings: `api/v1/settings` (GET) (name: `settings.index`) - No middleware required
- Get a single setting: `api/v1/settings/{setting_key}` (GET) (name: `settings.show`) - No middleware required
- Store a new setting: `api/v1/settings` (POST) (name: `settings.store`)
- Update a setting: `api/v1/settings/update/{setting_key}` (POST) (name: `settings.update`)
- Delete a setting: `api/v1/settings/{setting_key}` (DELETE) (name: `settings.destroy`)

You can customize the authentication guard for the routes by publishing the config file and changing the 'guard' key to your desired authentication guard:

```php
'guard' => ['auth'],
```

Make sure to publish the config file using the following command:

```shell
php artisan vendor:publish --tag=laravel-settings
```

To view the complete route list, run:

```shell
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "anisaronno/laravel-settings",
"description": "A Laravel package for managing application settings, similar to WordPress options.",
"version": "0.1.4",
"version": "0.1.5",
"license": "MIT",
"keywords": [
"laravel-settings",
Expand Down
1 change: 1 addition & 0 deletions src/Config/laravel-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
*/

'cache_expiry_time' => 60,
'guard' => ['auth'],
];
15 changes: 8 additions & 7 deletions src/Helpers/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ class SettingsHelper
*/
public static function hasSettings(string $settingsKey): bool
{
$key = CacheKey::getLaravelSettingsCacheKey();
$tagKey = CacheKey::getLaravelSettingsCacheKey();
$cacheKey = 'isExist'.$settingsKey;

try {
$settings = CacheControl::init($key)->remember($settingsKey, now()->addDay(), function () use ($settingsKey) {
$settings = CacheControl::init($tagKey)->remember($cacheKey, now()->addDay(), function () use ($settingsKey) {
return SettingsProperty::where('settings_key', $settingsKey)->exists();
});

return !! $settings;
return !!$settings;
} catch (\Throwable $th) {
return false;
}
Expand Down Expand Up @@ -71,7 +72,7 @@ public static function getSettings(string $settingsKey): string
*/
public static function setSettings(string $key, string $value): SettingsProperty
{
if(! self::isKeyAlphaDash($key, 'ascii')) {
if(!self::isKeyAlphaDash($key, 'ascii')) {
throw new Exception("The settings key field must only contain letters, numbers, dashes, and underscores", 400);
}

Expand Down Expand Up @@ -108,7 +109,7 @@ public static function setSettings(string $key, string $value): SettingsProperty
*/
public static function upsertSettings(string $key, string $value): SettingsProperty
{
if(! self::isKeyAlphaDash($key, 'ascii')) {
if(!self::isKeyAlphaDash($key, 'ascii')) {
throw new Exception("The settings key field must only contain letters, numbers, dashes, and underscores", 400);
}

Expand Down Expand Up @@ -160,7 +161,7 @@ public static function getAllSettings(): Collection
*/
public static function updateSettings(string $key, string $value = ''): bool
{
if(! self::isKeyAlphaDash($key, 'ascii')) {
if(!self::isKeyAlphaDash($key, 'ascii')) {
throw new Exception("The settings key field must only contain letters, numbers, dashes, and underscores", 400);
}

Expand Down Expand Up @@ -215,7 +216,7 @@ public static function deleteSettings(string $key): bool
*/
private static function isKeyAlphaDash($value, string $parameters = 'ascii'): bool
{
if (! is_string($value) && ! is_numeric($value)) {
if (!is_string($value) && !is_numeric($value)) {
return false;
}

Expand Down
16 changes: 11 additions & 5 deletions src/Routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@

use AnisAronno\LaravelSettings\Http\Controllers\LaravelSettingsController;

Route::prefix('/v1')->group(function () {
$middleware = config()->has('laravel-settings.guard') ? config('laravel-settings.guard') : [];

Route::prefix('/v1')->group(function () use ($middleware) {
Route::get('/settings', [LaravelSettingsController::class, 'index'])->name('settings.index');
Route::get('/settings/{key}', [LaravelSettingsController::class, 'show'])->name('settings.show');
Route::post('/settings', [LaravelSettingsController::class, 'store'])->name('settings.store');
Route::post('/settings/update/{key}', [LaravelSettingsController::class, 'update'])->name('settings.update');
Route::delete('/settings/{key}', [LaravelSettingsController::class, 'destroy'])->name('settings.destroy');
Route::get('/settings/{key}', [LaravelSettingsController::class, 'show'])->name('settings.show');

Route::middleware($middleware)->group(function () {
Route::post('/settings', [LaravelSettingsController::class, 'store'])->name('settings.store');
Route::post('/settings/update/{key}', [LaravelSettingsController::class, 'update'])->name('settings.update');
Route::delete('/settings/{key}', [LaravelSettingsController::class, 'destroy'])->name('settings.destroy');
});

});

0 comments on commit dea8108

Please sign in to comment.