From 5dc8c85cbc32e53f935f75fcdb4d574e1ec31898 Mon Sep 17 00:00:00 2001 From: Maxime Leclercq Date: Wed, 6 Nov 2024 15:37:54 +0100 Subject: [PATCH] fix: avoid error during warmup e.g. new properties on channel --- src/CacheWarmer/SettingsCacheWarmer.php | 36 ++++++++++++++----------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/CacheWarmer/SettingsCacheWarmer.php b/src/CacheWarmer/SettingsCacheWarmer.php index a8e542e0..afebdcd7 100644 --- a/src/CacheWarmer/SettingsCacheWarmer.php +++ b/src/CacheWarmer/SettingsCacheWarmer.php @@ -14,6 +14,7 @@ namespace MonsieurBiz\SyliusSettingsPlugin\CacheWarmer; use Doctrine\ORM\EntityManagerInterface; +use Exception; use MonsieurBiz\SyliusSettingsPlugin\Settings\RegistryInterface; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ChannelInterface; @@ -46,26 +47,31 @@ public function warmUp(string $cacheDir): array return []; } - $settings = $this->registry->getAllSettings(); - foreach ($settings as $setting) { - $setting->getSettingsByChannelAndLocale(); - $setting->getSettingsByChannelAndLocale(null, null, true); + try { + $settings = $this->registry->getAllSettings(); + foreach ($settings as $setting) { + $setting->getSettingsByChannelAndLocale(); + $setting->getSettingsByChannelAndLocale(null, null, true); - /** @var ChannelInterface $channel */ - foreach ($this->channelRepository->findAll() as $channel) { - if (null === $channel->getCode()) { - continue; - } + /** @var ChannelInterface $channel */ + foreach ($this->channelRepository->findAll() as $channel) { + if (null === $channel->getCode()) { + continue; + } - $setting->getSettingsByChannelAndLocale($channel); - $setting->getSettingsByChannelAndLocale($channel, null, true); + $setting->getSettingsByChannelAndLocale($channel); + $setting->getSettingsByChannelAndLocale($channel, null, true); - /** @var LocaleInterface $locale */ - foreach ($channel->getLocales() as $locale) { - $setting->getSettingsByChannelAndLocale($channel, $locale->getCode()); - $setting->getSettingsByChannelAndLocale($channel, $locale->getCode(), true); + /** @var LocaleInterface $locale */ + foreach ($channel->getLocales() as $locale) { + $setting->getSettingsByChannelAndLocale($channel, $locale->getCode()); + $setting->getSettingsByChannelAndLocale($channel, $locale->getCode(), true); + } } } + } catch (Exception) { + // If an exception is thrown, we just ignore it and return an empty array + // e.g. if upgrade Sylius and a new channel property is added to the ChannelInterface } return [];