|
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'), |
|
|
|
|
|
|
19
|
|
|
], 'variables-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
|
|
|
], 'variables-migrations'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if ($this->app['config']->get('variables.config_key_for_vars')) { |
|
30
|
|
|
$this->replaceConfigsWithVariables(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if ($this->app->runningInConsole()) { |
|
34
|
|
|
$this->commands([ |
|
35
|
|
|
Commands\AllVariable::class, |
|
36
|
|
|
Commands\GetVariable::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
|
|
|
return new VariableManager(new Variable, $cacheRepo, $this->app); |
|
53
|
|
|
}); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function replaceConfigsWithVariables() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->app->booted(function () { |
|
59
|
|
|
|
|
60
|
|
|
// package config |
|
61
|
|
|
$config = $this->app['config']->get('variables'); |
|
62
|
|
|
|
|
63
|
|
|
// replace configs with variables |
|
64
|
|
|
$variableConfig = $config['variable_config']; |
|
65
|
|
|
|
|
66
|
|
|
$variables = $this->app->make(VariableManagerContract::class)->all(); |
|
67
|
|
|
|
|
68
|
|
|
foreach ($variables as $varKey => $varValue) { |
|
69
|
|
|
|
|
70
|
|
|
$configKey = $variableConfig[$varKey] ?? ($config['config_key_for_vars'] . '.' . $varKey); |
|
71
|
|
|
|
|
72
|
|
|
$this->app['config']->set($configKey, $varValue); |
|
73
|
|
|
} |
|
74
|
|
|
}); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|