Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
34 / 34 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ConfigServiceProvider | |
100.00% |
34 / 34 |
|
100.00% |
4 / 4 |
13 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
5 | |||
| boot | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
6 | |||
| getConfigPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Engelsystem\Config; |
| 6 | |
| 7 | use Engelsystem\Application; |
| 8 | use Engelsystem\Container\ServiceProvider; |
| 9 | use Engelsystem\Models\EventConfig; |
| 10 | use Exception; |
| 11 | use Illuminate\Database\QueryException; |
| 12 | |
| 13 | class ConfigServiceProvider extends ServiceProvider |
| 14 | { |
| 15 | protected array $configFiles = ['app.php', 'config.default.php', 'config.php']; |
| 16 | |
| 17 | // Remember to update ConfigServiceProviderTest, config.default.php, and README.md |
| 18 | protected array $configVarsToPruneNulls = [ |
| 19 | 'themes', |
| 20 | 'tshirt_sizes', |
| 21 | 'headers', |
| 22 | 'header_items', |
| 23 | 'footer_items', |
| 24 | 'locales', |
| 25 | 'contact_options', |
| 26 | ]; |
| 27 | |
| 28 | public function __construct(Application $app, protected ?EventConfig $eventConfig = null) |
| 29 | { |
| 30 | parent::__construct($app); |
| 31 | } |
| 32 | |
| 33 | public function register(): void |
| 34 | { |
| 35 | $config = $this->app->make(Config::class); |
| 36 | $this->app->instance(Config::class, $config); |
| 37 | $this->app->instance('config', $config); |
| 38 | |
| 39 | // Load configuration from files |
| 40 | foreach ($this->configFiles as $file) { |
| 41 | $file = $this->getConfigPath($file); |
| 42 | |
| 43 | if (!file_exists($file)) { |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | $configuration = array_replace_recursive( |
| 48 | $config->get(null), |
| 49 | require $file |
| 50 | ); |
| 51 | $config->set($configuration); |
| 52 | } |
| 53 | |
| 54 | if (empty($config->get(null))) { |
| 55 | throw new Exception('Configuration not found'); |
| 56 | } |
| 57 | |
| 58 | // Prune values with null to remove them |
| 59 | foreach ($this->configVarsToPruneNulls as $key) { |
| 60 | $config->set($key, array_filter($config->get($key), function ($v) { |
| 61 | return !is_null($v); |
| 62 | })); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public function boot(): void |
| 67 | { |
| 68 | if (!$this->eventConfig) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | /** @var Config $config */ |
| 73 | $config = $this->app->get('config'); |
| 74 | try { |
| 75 | /** @var EventConfig[] $values */ |
| 76 | $values = $this->eventConfig->newQuery()->get(['name', 'value']); |
| 77 | } catch (QueryException) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | foreach ($values as $option) { |
| 82 | $data = $option->value; |
| 83 | |
| 84 | if (is_array($data) && $config->has($option->name)) { |
| 85 | $data = array_replace_recursive( |
| 86 | $config->get($option->name), |
| 87 | $data |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | $config->set($option->name, $data); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the config path |
| 97 | */ |
| 98 | protected function getConfigPath(string $path = ''): string |
| 99 | { |
| 100 | return config_path($path); |
| 101 | } |
| 102 | } |