vendor/store.shopware.com/tcinnthemewarestrong/src/TcinnThemeWareStrong.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TcinnThemeWareStrong;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  11. use Shopware\Storefront\Framework\ThemeInterface;
  12. use TcinnThemeWareStrong\CustomFields\CustomFieldUpdater;
  13. /* ThemeWare: Handle cms media services */
  14. use Symfony\Component\Config\FileLocator;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  17. class TcinnThemeWareStrong extends Plugin implements ThemeInterface
  18. {
  19.     // ToDo: Remove for apps in deployment
  20.     private function getCustomFieldUpdater()
  21.     {
  22.         /**
  23.          * @var EntityRepositoryInterface $customFieldSetRepository
  24.          */
  25.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  26.         /**
  27.          * @var EntityRepositoryInterface $customFieldRepository
  28.          */
  29.         $customFieldRepository $this->container->get('custom_field.repository');
  30.         return new CustomFieldUpdater(
  31.             $customFieldSetRepository,
  32.             $customFieldRepository,
  33.             $this->path
  34.         );
  35.     }
  36.     public function getThemeConfigPath(): string
  37.     {
  38.         return 'theme.json';
  39.     }
  40.     public function install(InstallContext $installContext): void
  41.     {
  42.         parent::install($installContext);
  43.         $this->getCustomFieldUpdater()->sync();
  44.     }
  45.     /**
  46.      * Copy base_config from main theme to child themes to
  47.      * ensure that child themes get new features from updates.
  48.      *
  49.      * @param UpdateContext $updateContext
  50.      */
  51.     public function postUpdate(UpdateContext $updateContext): void
  52.     {
  53.         /** @var EntityRepository $themeRepository */
  54.         $themeRepository $this->container->get('theme.repository');
  55.         $parentThemeCollection $themeRepository->search(
  56.             (new Criteria())->addFilter(new EqualsFilter('technicalName''TcinnThemeWareStrong')),
  57.             \Shopware\Core\Framework\Context::createDefaultContext()
  58.         );
  59.         if(!$parentThemeCollection) {
  60.             return;
  61.         }
  62.         $parentTheme $parentThemeCollection->first();
  63.         $childThemesCollection $themeRepository->search(
  64.             (new Criteria())->addFilter(new EqualsFilter('parentThemeId'$parentTheme->get('id'))),
  65.             \Shopware\Core\Framework\Context::createDefaultContext()
  66.         );
  67.         if(!$childThemesCollection) {
  68.             return;
  69.         }
  70.         foreach ($childThemesCollection->getElements() as $childTheme) {
  71.             if(!$childTheme->get('previewMediaId')) {
  72.                 $data = [
  73.                     [
  74.                         'id' => $childTheme->get('id'),
  75.                         'previewMediaId' => $parentTheme->get('previewMediaId'),
  76.                         'baseConfig' => $parentTheme->get('baseConfig')
  77.                     ]
  78.                 ];
  79.             } else {
  80.                 $data = [
  81.                     [
  82.                         'id' => $childTheme->get('id'),
  83.                         'baseConfig' => $parentTheme->get('baseConfig')
  84.                     ]
  85.                 ];
  86.             }
  87.             $themeRepository->update($data\Shopware\Core\Framework\Context::createDefaultContext());
  88.         }
  89.     }
  90.     public function uninstall(UninstallContext $uninstallContext): void
  91.     {
  92.         parent::uninstall($uninstallContext);
  93.         if (!$uninstallContext->keepUserData()) {
  94.             $this->getCustomFieldUpdater()->remove();
  95.         }
  96.     }
  97.     public function update(UpdateContext $updateContext): void
  98.     {
  99.         parent::update($updateContext);
  100.         $this->getCustomFieldUpdater()->sync();
  101.     }
  102.     /**
  103.      * Skip rebuild container on activate/deactivate process
  104.      * to speedup Shopware Cloud bundle integration.
  105.      *
  106.      * @return bool
  107.      */
  108.     public function rebuildContainer(): bool
  109.     {
  110.         return false;
  111.     }
  112.     /**
  113.      * Load media.xml to add cms data resolver services
  114.      *
  115.      * @param ContainerBuilder $container
  116.      */
  117.     public function build(ContainerBuilder $container): void
  118.     {
  119.         parent::build($container);
  120.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__ '/Core/Content/DependencyInjection'));
  121.         $loader->load('media.xml');
  122.     }
  123. }