Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SiteSettingEventSubscriber.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Symfony\Component\Security\Core\Security;
  12. class SiteSettingEventSubscriber implements EventSubscriberInterface
  13. {
  14. protected $em;
  15. protected $siteSettingDefinition;
  16. protected $siteSettingFactory;
  17. protected $siteSettingRepository;
  18. protected $siteRepository;
  19. protected $siteFactory;
  20. public function __construct(
  21. EntityManagerInterface $em,
  22. SiteSettingRepository $siteSettingRepository,
  23. SiteRepository $siteRepository,
  24. SiteSettingDefinitionInterface $siteSettingDefinition,
  25. SiteSettingFactory $siteSettingFactory,
  26. SiteFactory $siteFactory
  27. ) {
  28. $this->em = $em;
  29. $this->siteSettingDefinition = $siteSettingDefinition;
  30. $this->siteSettingFactory = $siteSettingFactory;
  31. $this->siteSettingRepository = $siteSettingRepository;
  32. $this->siteRepository = $siteRepository;
  33. $this->siteFactory = $siteFactory;
  34. }
  35. public static function getSubscribedEvents()
  36. {
  37. return [
  38. KernelEvents::CONTROLLER => ['initSettings']
  39. ];
  40. }
  41. public function initSettings()
  42. {
  43. $site = $this->getSiteDefault();
  44. if (!$site) {
  45. $site = $this->siteFactory->create(
  46. [
  47. 'devAlias' => 'default'
  48. ]
  49. );
  50. $this->em->persist($site);
  51. $this->em->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 = $site->getSetting($settingName);
  58. if (!$entitySetting) {
  59. $entitySetting = $factory->create(
  60. [
  61. 'site' => $site,
  62. 'name' => $setting['name'],
  63. $setting['field'] => isset($setting['default']) ? $setting['default'] : null,
  64. ]
  65. );
  66. $this->em->persist($entitySetting);
  67. } else {
  68. if ($entitySetting->getValue() === null
  69. && isset($setting['default'])
  70. && $setting['default'] !== null) {
  71. $methodSetValue = 'set' . ucfirst($setting['field']);
  72. $entitySetting->$methodSetValue($setting['default']);
  73. $this->em->update($entitySetting);
  74. }
  75. }
  76. }
  77. }
  78. $this->em->flush();
  79. }
  80. public function getSiteDefault()
  81. {
  82. return $this->siteRepository->findOneByDevAlias('default');
  83. }
  84. }