選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

109 行
3.9KB

  1. <?php
  2. namespace Lc\SovBundle\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\SovBundle\Definition\SiteSettingDefinitionInterface;
  5. use Lc\SovBundle\Factory\Setting\SiteSettingFactory;
  6. use Lc\SovBundle\Factory\Site\SiteFactory;
  7. use Lc\SovBundle\Repository\Setting\SiteSettingRepository;
  8. use Lc\SovBundle\Repository\Site\SiteRepository;
  9. use Lc\SovBundle\Repository\Site\SiteStore;
  10. use Lc\SovBundle\Solver\Setting\SettingSolver;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. class SiteSettingEventSubscriber implements EventSubscriberInterface
  14. {
  15. protected EntityManagerInterface $entityManager;
  16. protected SiteSettingDefinitionInterface $siteSettingDefinition;
  17. protected SiteSettingFactory $siteSettingFactory;
  18. protected SiteSettingRepository $siteSettingRepository;
  19. protected SiteStore $siteStore;
  20. protected SiteFactory $siteFactory;
  21. protected SettingSolver $settingSolver;
  22. public function __construct(
  23. EntityManagerInterface $entityManager,
  24. SiteSettingRepository $siteSettingRepository,
  25. SiteStore $siteStore,
  26. SiteSettingDefinitionInterface $siteSettingDefinition,
  27. SiteSettingFactory $siteSettingFactory,
  28. SiteFactory $siteFactory,
  29. SettingSolver $settingSolver
  30. ) {
  31. $this->entityManager = $entityManager;
  32. $this->siteSettingDefinition = $siteSettingDefinition;
  33. $this->siteSettingFactory = $siteSettingFactory;
  34. $this->siteSettingRepository = $siteSettingRepository;
  35. $this->siteStore = $siteStore;
  36. $this->siteFactory = $siteFactory;
  37. $this->settingSolver = $settingSolver;
  38. }
  39. public static function getSubscribedEvents()
  40. {
  41. return [
  42. KernelEvents::CONTROLLER => ['initSettings']
  43. ];
  44. }
  45. public function initSettings()
  46. {
  47. $site = $this->getSiteDefault();
  48. if (!$site) {
  49. $site = $this->siteFactory->create('default');
  50. $this->entityManager->persist($site);
  51. $this->entityManager->flush($site);
  52. }
  53. $settings = $this->siteSettingDefinition->getSettings();
  54. $factory = $this->siteSettingFactory;
  55. foreach ($settings as $category => $settingList) {
  56. foreach ($settingList as $settingName => $setting) {
  57. $entitySetting = $this->settingSolver->getSetting($site, $settingName);
  58. if (!$entitySetting) {
  59. $text = null;
  60. $date = null;
  61. $file = null;
  62. $fieldValue = isset($setting['default']) ? $setting['default'] : null;
  63. if($setting['field'] == 'text') {
  64. $text = $fieldValue;
  65. }
  66. if($setting['field'] == 'date') {
  67. $date = $fieldValue;
  68. }
  69. if($setting['field'] == 'file') {
  70. $file = $fieldValue;
  71. }
  72. $entitySetting = $factory->create($site, $setting['name'], $text, $date, $file);
  73. $this->entityManager->persist($entitySetting);
  74. } else {
  75. $methodGetValue = 'get' . ucfirst($setting['field']);
  76. if ($entitySetting->$methodGetValue() === null
  77. && isset($setting['default'])
  78. && $setting['default'] !== null) {
  79. $methodSetValue = 'set' . ucfirst($setting['field']);
  80. $entitySetting->$methodSetValue($setting['default']);
  81. $this->entityManager->update($entitySetting);
  82. }
  83. }
  84. }
  85. }
  86. $this->entityManager->flush();
  87. }
  88. public function getSiteDefault()
  89. {
  90. return $this->siteStore->getOneByDevAlias('default');
  91. }
  92. }