1 | <?php |
||
2 | |||
3 | namespace Fomvasss\Variable; |
||
4 | |||
5 | use Fomvasss\Variable\Models\Variable; |
||
6 | use Illuminate\Support\ServiceProvider; |
||
7 | |||
8 | class VariableServiceProvider extends ServiceProvider |
||
9 | { |
||
10 | /** |
||
11 | * Bootstrap the application services. |
||
12 | * |
||
13 | * @return void |
||
14 | */ |
||
15 | public function boot() |
||
16 | { |
||
17 | $this->publishes([ |
||
18 | __DIR__.'/../config/variables.php' => config_path('variables.php'), |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
19 | ], 'config'); |
||
20 | |||
21 | if (! class_exists('CreateVariablesTable')) { |
||
22 | $timestamp = date('Y_m_d_His', time()); |
||
23 | |||
24 | $this->publishes([ |
||
25 | __DIR__.'/../database/migrations/create_variables_table.php.stub' => $this->app->databasePath()."/migrations/{$timestamp}_create_variables_table.php", |
||
26 | ], 'migrations'); |
||
27 | } |
||
28 | |||
29 | $this->replaceConfigsWithVariables(); |
||
30 | |||
31 | if ($this->app->runningInConsole()) { |
||
32 | $this->commands([ |
||
33 | Commands\AllVariable::class, |
||
34 | Commands\GetVariable::class, |
||
35 | Commands\SaveVariable::class, |
||
36 | Commands\CacheClearVariable::class, |
||
37 | ]); |
||
38 | } |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Register the application services. |
||
43 | * |
||
44 | * @return void |
||
45 | */ |
||
46 | public function register() |
||
47 | { |
||
48 | $this->mergeConfigFrom(__DIR__.'/../config/variables.php', 'variables'); |
||
49 | |||
50 | $this->app->singleton(VariableManagerContract::class, function () { |
||
51 | $cacheRepo = $this->app->make(\Illuminate\Cache\Repository::class); |
||
52 | $variableModel = $this->app['config']->get('variables.model_name'); |
||
53 | |||
54 | return new VariableManager(new $variableModel, $cacheRepo, $this->app); |
||
55 | }); |
||
56 | } |
||
57 | |||
58 | protected function replaceConfigsWithVariables() |
||
59 | { |
||
60 | $this->app->booted(function () { |
||
61 | |||
62 | // Package config |
||
63 | $config = $this->app['config']->get('variables'); |
||
64 | |||
65 | // Dynamic replace config keys with variables |
||
66 | $variableConfig = $config['variable_config']; |
||
67 | |||
68 | try { |
||
69 | $variables = $this->app->make(VariableManagerContract::class)->all(); |
||
70 | |||
71 | foreach ($variables as $var) { |
||
72 | if (! empty($variableConfig[$var->key])) { |
||
73 | $this->app['config']->set($variableConfig[$var->key], $var->value); |
||
74 | } |
||
75 | if (! empty($config['config_key_for_vars'])) { |
||
76 | $this->app['config']->set($config['config_key_for_vars'] . '.' . $var->key, $var->value); |
||
77 | } |
||
78 | } |
||
79 | } catch (\Exception $exception) { |
||
80 | $this->app['log']->warning($exception->getMessage()); |
||
81 | } |
||
82 | }); |
||
83 | } |
||
84 | } |
||
85 |