diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Config/ConfigServiceProvider.php | 59 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Globals.php | 40 |
2 files changed, 59 insertions, 40 deletions
diff --git a/src/Config/ConfigServiceProvider.php b/src/Config/ConfigServiceProvider.php index 63f43ced..cb8e96ce 100644 --- a/src/Config/ConfigServiceProvider.php +++ b/src/Config/ConfigServiceProvider.php @@ -2,14 +2,31 @@ namespace Engelsystem\Config; +use Engelsystem\Application; use Engelsystem\Container\ServiceProvider; +use Engelsystem\Models\EventConfig; use Exception; +use Illuminate\Database\QueryException; class ConfigServiceProvider extends ServiceProvider { /** @var array */ protected $configFiles = ['config.default.php', 'config.php']; + /** @var EventConfig */ + protected $eventConfig; + + /** + * @param Application $app + * @param EventConfig $eventConfig + */ + public function __construct(Application $app, EventConfig $eventConfig = null) + { + parent::__construct($app); + + $this->eventConfig = $eventConfig; + } + public function register() { $config = $this->app->make(Config::class); @@ -17,7 +34,7 @@ class ConfigServiceProvider extends ServiceProvider $this->app->instance('config', $config); foreach ($this->configFiles as $file) { - $file = config_path($file); + $file = $this->getConfigPath($file); if (!file_exists($file)) { continue; @@ -33,4 +50,44 @@ class ConfigServiceProvider extends ServiceProvider throw new Exception('Configuration not found'); } } + + public function boot() + { + if (!$this->eventConfig) { + return; + } + + /** @var Config $config */ + $config = $this->app->get('config'); + /** @var EventConfig[] $values */ + try { + $values = $this->eventConfig->newQuery()->get(['name', 'value']); + } catch (QueryException $e) { + return; + } + + foreach ($values as $option) { + $data = $option->value; + + if (is_array($data) && $config->has($option->name)) { + $data = array_replace_recursive( + $config->get($option->name), + $data + ); + } + + $config->set($option->name, $data); + } + } + + /** + * Get the config path + * + * @param string $path + * @return string + */ + protected function getConfigPath($path = ''): string + { + return config_path($path); + } } diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php index 1a4df42c..f9bffbc8 100644 --- a/src/Renderer/Twig/Extensions/Globals.php +++ b/src/Renderer/Twig/Extensions/Globals.php @@ -2,7 +2,6 @@ namespace Engelsystem\Renderer\Twig\Extensions; -use Carbon\Carbon; use Twig_Extension as TwigExtension; use Twig_Extension_GlobalsInterface as GlobalsInterface; @@ -17,45 +16,8 @@ class Globals extends TwigExtension implements GlobalsInterface { global $user; - $eventConfig = $this->getEventConfig(); - if (empty($eventConfig)) { - $eventConfig = []; - } - return [ - 'user' => isset($user) ? $user : [], - 'event_config' => $this->filterEventConfig($eventConfig), + 'user' => isset($user) ? $user : [], ]; } - - /** - * @return array - * @codeCoverageIgnore - */ - protected function getEventConfig() - { - return EventConfig(); - } - - /** - * @param $eventConfig - * @return mixed - */ - protected function filterEventConfig($eventConfig) - { - array_walk($eventConfig, function (&$value, $key) { - if (is_null($value) || !in_array($key, [ - 'buildup_start_date', - 'event_start_date', - 'event_end_date', - 'teardown_end_date', - ])) { - return; - } - - $value = Carbon::createFromTimestamp($value); - }); - - return $eventConfig; - } } |