|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EmadHa\DynamicConfig; |
| 4 | + |
| 5 | +use Illuminate\Database\QueryException; |
| 6 | +use Illuminate\Support\Facades\DB; |
| 7 | +use Illuminate\Support\Facades\Log; |
| 8 | +use Illuminate\Support\Facades\Schema; |
| 9 | +use mysql_xdevapi\Exception; |
| 10 | + |
| 11 | +class ServiceProvider extends \Illuminate\Support\ServiceProvider |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Register services. |
| 15 | + * |
| 16 | + * @return void |
| 17 | + */ |
| 18 | + public function register() |
| 19 | + { |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Bootstrap services. |
| 25 | + * |
| 26 | + * @return void |
| 27 | + * @throws \Exception |
| 28 | + */ |
| 29 | + public function boot() |
| 30 | + { |
| 31 | + if ($this->app->runningInConsole()) { |
| 32 | + if (!class_exists('CreateSiteConfigTable')) { |
| 33 | + $timestamp = date('Y_m_d_His', time()); |
| 34 | + $this->publishes([ |
| 35 | + __DIR__ . '/../database/migrations/create_site_config_table.php.stub' => database_path('migrations/' . $timestamp . '_create_site_config_table.php'), |
| 36 | + ], 'migrations'); |
| 37 | + } |
| 38 | + |
| 39 | + $this->publishes([ |
| 40 | + __DIR__ . '/../config/site-config.php' => config_path('emadha/site-config.php'), |
| 41 | + ], 'config'); |
| 42 | + } |
| 43 | + |
| 44 | + $this->initConfig(); |
| 45 | + } |
| 46 | + |
| 47 | + private function initConfig() |
| 48 | + { |
| 49 | + |
| 50 | + # Check if the table exists |
| 51 | + if (!Schema::hasTable(config('emadha.site-config.table'))) { |
| 52 | + |
| 53 | + # Don't crash, Log the error instead |
| 54 | + Log::error(sprintf( |
| 55 | + get_class($this) . " is missing the the dynamic config table [`%s`]. you might need to do `php artisan vendor:publish` && `php artisan migrate`", |
| 56 | + config('emadha.site-config.table')) |
| 57 | + ); |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + # Create a new collection of what's dynamic |
| 63 | + $DefaultConfig = collect([]); |
| 64 | + |
| 65 | + # Return the config entries containing ['dynamic'=>true] key |
| 66 | + collect(config()->all())->each(function ($value, $key) use (&$DefaultConfig) { |
| 67 | + |
| 68 | + # Check if the current config key has dynamic key set to it, and it's true |
| 69 | + if (array_key_exists(config('emadha.site-config.dynamic_key'), $value) |
| 70 | + && $value[config('emadha.site-config.dynamic_key')] == true) { |
| 71 | + |
| 72 | + # unset that dynamic value |
| 73 | + unset($value[config('emadha.site-config.dynamic_key')]); |
| 74 | + |
| 75 | + # Add that to the DynamicConfig collection |
| 76 | + $DefaultConfig->put($key, $value); |
| 77 | + } |
| 78 | + |
| 79 | + }); |
| 80 | + |
| 81 | + # Keep the defaults for reference |
| 82 | + config([config('emadha.site-config.defaults_key') => $DefaultConfig]); |
| 83 | + |
| 84 | + # Flatten the config table data |
| 85 | + $prefixedKeys = $this->prefixKey(null, $DefaultConfig->all()); |
| 86 | + |
| 87 | + # Insert the flattened data into database |
| 88 | + foreach ($prefixedKeys as $_key => $_value) { |
| 89 | + |
| 90 | + # Get the row from database if it exists, |
| 91 | + # If not, add it using the value from the actual config file. |
| 92 | + DynamicConfig::firstOrCreate(['k' => $_key], ['v' => $_value]); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + # Build the Config array |
| 97 | + $DynamicConfig = DynamicConfig::all(); |
| 98 | + |
| 99 | + # Check if auto deleting orphan keys is enabled |
| 100 | + # and delete those if they don't exists in the actual config file |
| 101 | + if (config('emadha.site-config.auto_delete_orphan_keys') == true) { |
| 102 | + |
| 103 | + # Check for orphan keys |
| 104 | + $orphanKeys = array_diff_assoc($DynamicConfig->pluck('v', 'k')->toArray(), $prefixedKeys); |
| 105 | + |
| 106 | + # Delete orphan keys |
| 107 | + DynamicConfig::whereIn('k', array_keys($orphanKeys))->delete(); |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + # Store these config into the config() helper, but as model objects |
| 112 | + # Thus making Model's method accessible from here |
| 113 | + # example: config('app.name')->revert(). |
| 114 | + # Available methods are `revert`, `default` and `setTo($value)` |
| 115 | + $DynamicConfig->map(function ($config) use ($DefaultConfig) { |
| 116 | + config([$config->k => $config]); |
| 117 | + }); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + public function prefixKey($prefix, $array) |
| 122 | + { |
| 123 | + $result = []; |
| 124 | + foreach ($array as $key => $value) { |
| 125 | + if (is_array($value)) { |
| 126 | + $result = array_merge($result, self::prefixKey($prefix . $key . '.', $value)); |
| 127 | + } else { |
| 128 | + $result[$prefix . $key] = $value; |
| 129 | + } |
| 130 | + } |
| 131 | + return $result; |
| 132 | + } |
| 133 | +} |
0 commit comments