entityManager = $entityManager; $this->siteSettingDefinition = $siteSettingDefinition; $this->siteSettingFactory = $siteSettingFactory; $this->siteSettingRepository = $siteSettingRepository; $this->siteStore = $siteStore; $this->siteFactory = $siteFactory; $this->settingSolver = $settingSolver; } public static function getSubscribedEvents() { return [ KernelEvents::CONTROLLER => ['initSettings'] ]; } public function initSettings() { $site = $this->getSiteDefault(); if (!$site) { $site = $this->siteFactory->create('default'); $this->entityManager->persist($site); $this->entityManager->flush($site); } $settings = $this->siteSettingDefinition->getSettings(); $factory = $this->siteSettingFactory; foreach ($settings as $category => $settingList) { foreach ($settingList as $settingName => $setting) { $entitySetting = $this->settingSolver->getSetting($site, $settingName); if (!$entitySetting) { $text = null; $date = null; $file = null; $fieldValue = isset($setting['default']) ? $setting['default'] : null; if($setting['field'] == 'text') { $text = $fieldValue; } if($setting['field'] == 'date') { $date = $fieldValue; } if($setting['field'] == 'file') { $file = $fieldValue; } $entitySetting = $factory->create($site, $setting['name'], $text, $date, $file); $this->entityManager->create($entitySetting); } else { $methodGetValue = 'get' . ucfirst($setting['field']); if ($entitySetting->$methodGetValue() === null && isset($setting['default']) && $setting['default'] !== null) { $methodSetValue = 'set' . ucfirst($setting['field']); $entitySetting->$methodSetValue($setting['default']); $this->entityManager->update($entitySetting); } } } } $this->entityManager->flush(); } public function getSiteDefault() { return $this->siteStore->getOneByDevAlias('default'); } }