Skip to content

Commit

Permalink
- General changes made
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehmet Faruk Demirkoparan committed Jan 23, 2025
1 parent eb9e800 commit 269ab1b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ You can publish the config with:

Config file:
``` bash
php artisan vendor:publish --provider="EmadHa\DynamicConfig\ServiceProvider" --tag="config"
php artisan vendor:publish --provider="AuroraWebSoftware\AConfig\ServiceProvider" --tag="config"
```

Migration:
```bash
php artisan vendor:publish --provider="EmadHa\DynamicConfig\ServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="AuroraWebSoftware\AConfig\ServiceProvider" --tag="migrations"
```

## Usage
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
{
"name": "emadha/laravel-dynamic-config",
"description": "This Package makes it possible for users to have their config files stored in a database table, making it easier to customize these values from UI.",
"name": "aurorawebsoftware/aconfig",
"description": "",
"type": "library",
"require": {
"laravel/framework": "^7.0|^8.0|^9.0|^10.0|^11.0"
},
"license": "MIT",
"authors": [
{
"name": "Emad Ha",
"email": "[email protected]"
"name": "Aurora Web Software Team",
"email": "[email protected]",
"role": "Developer"
}
],
"minimum-stability": "dev",
"autoload": {
"psr-4": {
"EmadHa\\DynamicConfig\\": "src/"
"AuroraWebSoftware\\AConfig\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"EmadHa\\DynamicConfig\\ServiceProvider"
"AuroraWebSoftware\\AConfig\\ServiceProvider"
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion config/site-config.php → config/aconfig.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
return [
/* The Config database table name */
'table' => 'conf',
'table' => 'aconfig',

/*
* The key that defines which config file should be loaded dynamically
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSiteConfigTable extends Migration
class CreateAConfigTable extends Migration
{
/**
* Run the migrations.
Expand All @@ -13,11 +13,11 @@ class CreateSiteConfigTable extends Migration
*/
public function up()
{
if (!Schema::hasTable(config('emadha.site-config.table'))) {
Schema::create(config('emadha.site-config.table'), function (Blueprint $table) {
if (!Schema::hasTable(config('aconfig.table'))) {
Schema::create(config('aconfig.table'), function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('k', 500)->unique();
$table->text('v')->nullable();
$table->string('key', 500)->unique();
$table->json('value')->nullable();
$table->timestamps();
});
}
Expand All @@ -30,6 +30,6 @@ class CreateSiteConfigTable extends Migration
*/
public function down()
{
Schema::drop(config('emadha.site-config.table'));
Schema::drop(config('aconfig.table'));
}
}
29 changes: 13 additions & 16 deletions src/DynamicConfig.php → src/AConfig.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
<?php

namespace EmadHa\DynamicConfig;
namespace AuroraWebSoftware\AConfig;

use Illuminate\Database\Eloquent\Model;

/**
* Class DynamicConfig
*
* @property mixed v
* @property mixed value
* @package EmadHa\DynamicConfig
*/
class DynamicConfig extends Model
class AConfig extends Model
{
/**
* @var array
*/
protected $guarded = ['id'];

protected $casts = [
'value' => 'array',
];


/**
* DynamicConfig constructor.
*
Expand All @@ -25,7 +30,7 @@ class DynamicConfig extends Model
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->setTable(config('emadha.site-config.table'));
$this->setTable(config('aconfig.table'));
}

/**
Expand All @@ -37,7 +42,7 @@ public function __construct(array $attributes = [])
*/
public function setTo($value)
{
return $this->update(['v' => $value]);
return $this->update(['value' => $value]);
}

/**
Expand All @@ -48,7 +53,7 @@ public function setTo($value)
public function default()
{
return config(
config('emadha.site-config.defaults_key') . '.' . $this->k
config('aconfig.defaults_key') . '.' . $this->key
);
}

Expand All @@ -60,18 +65,10 @@ public function default()
*/
public function revert()
{
return config($this->k)->setTo(
config(config('emadha.site-config.defaults_key') . '.' . $this->k)
return config($this->key)->setTo(
config(config('aconfig.defaults_key') . '.' . $this->key)
);
}

/**
* @return mixed|string
*/
public function __toString()
{
return $this->v;
}

}

34 changes: 17 additions & 17 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace EmadHa\DynamicConfig;
namespace AuroraWebSoftware\AConfig;

use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -29,15 +29,15 @@ public function register()
public function boot()
{
if ($this->app->runningInConsole()) {
if (!class_exists('CreateSiteConfigTable')) {
if (!class_exists('CreateAConfigTable')) {
$timestamp = date('Y_m_d_His', time());
$this->publishes([
__DIR__ . '/../database/migrations/create_site_config_table.php.stub' => database_path('migrations/' . $timestamp . '_create_site_config_table.php'),
__DIR__ . '/../database/migrations/create_aconfig_table.php.stub' => database_path('migrations/' . $timestamp . '_create_aconfig_table.php'),
], 'migrations');
}

$this->publishes([
__DIR__ . '/../config/site-config.php' => config_path('emadha/site-config.php'),
__DIR__ . '/../config/aconfig.php' => config_path('aconfig.php'),
], 'config');
}

Expand All @@ -48,12 +48,12 @@ private function initConfig()
{

# Check if the table exists
if (!Schema::hasTable(config('emadha.site-config.table'))) {
if (!Schema::hasTable(config('aconfig.table'))) {

# Don't crash, Log the error instead
Log::error(sprintf(
get_class($this) . " is missing the the dynamic config table [`%s`]. you might need to do `php artisan vendor:publish` && `php artisan migrate`",
config('emadha.site-config.table'))
config('aconfig.table'))
);

return false;
Expand All @@ -66,11 +66,11 @@ private function initConfig()
collect(config()->all())->each(function ($value, $key) use (&$DefaultConfig) {

# Check if the current config key has dynamic key set to it, and it's true
if (array_key_exists(config('emadha.site-config.dynamic_key'), $value)
&& $value[config('emadha.site-config.dynamic_key')] == true) {
if (array_key_exists(config('aconfig.dynamic_key'), $value)
&& $value[config('aconfig.dynamic_key')] == true) {

# unset that dynamic value
unset($value[config('emadha.site-config.dynamic_key')]);
unset($value[config('aconfig.dynamic_key')]);

# Add that to the DynamicConfig collection
$DefaultConfig->put($key, $value);
Expand All @@ -79,7 +79,7 @@ private function initConfig()
});

# Keep the defaults for reference
config([config('emadha.site-config.defaults_key') => $DefaultConfig]);
config([config('aconfig.defaults_key') => $DefaultConfig]);

# Flatten the config table data
$prefixedKeys = $this->prefixKey(null, $DefaultConfig->all());
Expand All @@ -89,31 +89,31 @@ private function initConfig()

# Get the row from database if it exists,
# If not, add it using the value from the actual config file.
DynamicConfig::firstOrCreate(['k' => $_key], ['v' => $_value]);
AConfig::firstOrCreate(['key' => $_key], ['value' => $_value]);

}

# Build the Config array
$DynamicConfig = DynamicConfig::all();
$AConfig = AConfig::all();

# Check if auto deleting orphan keys is enabled
# and delete those if they don't exists in the actual config file
if (config('emadha.site-config.auto_delete_orphan_keys') == true) {
if (config('aconfig.auto_delete_orphan_keys') == true) {

# Check for orphan keys
$orphanKeys = array_diff_assoc($DynamicConfig->pluck('v', 'k')->toArray(), $prefixedKeys);
$orphanKeys = array_diff_assoc($AConfig->pluck('value', 'key')->toArray(), $prefixedKeys);

# Delete orphan keys
DynamicConfig::whereIn('k', array_keys($orphanKeys))->delete();
AConfig::whereIn('key', array_keys($orphanKeys))->delete();

}

# Store these config into the config() helper, but as model objects
# Thus making Model's method accessible from here
# example: config('app.name')->revert().
# Available methods are `revert`, `default` and `setTo($value)`
$DynamicConfig->map(function ($config) use ($DefaultConfig) {
config([$config->k => $config]);
$AConfig->map(function ($config) use ($DefaultConfig) {
config([$config->key => $config]);
});

}
Expand Down

0 comments on commit 269ab1b

Please sign in to comment.