custom/plugins/inoceani18ntraditionalchinese/src/InoceanI18nTraditionalChinese.php line 23

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) inocean <iecsp.com@gmail.com>
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace InoceanI18nTraditionalChinese;
  9. use Shopware\Core\Defaults;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  15. use Shopware\Core\Framework\Plugin;
  16. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  17. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  18. use Shopware\Core\Framework\Uuid\Uuid;
  19. use InoceanI18nTraditionalChinese\Resources\app\core\SnippetFile_zh_TW as SnippetFile;
  20. class InoceanI18nTraditionalChinese extends Plugin
  21. {
  22.     public const INOCEAN_I18N_LOCALE_CODE 'zh-TW';
  23.     public const INOCEAN_I18N_LANGUAGE_NAME '正體中文';
  24.     public function install(InstallContext $context): void
  25.     {
  26.         $this->addLanguage($context->getContext());
  27.         $this->addBaseSnippetSet($context->getContext());
  28.         parent::install($context);
  29.     }
  30.     public function uninstall(UninstallContext $context): void
  31.     {
  32.         $this->deleteLanguage($context->getContext());
  33.         $this->deleteBaseSnippetSet($context->getContext());
  34.         parent::uninstall($context);
  35.     }
  36.     public function rebuildContainer(): bool
  37.     {
  38.         return false;
  39.     }
  40.     private function addLanguage(Context $shopwareContext): void
  41.     {
  42.         $localeId $this->getLocaleId($shopwareContext);
  43.         if (!$this->isNewLanguage($localeId$shopwareContext)) {
  44.             return;
  45.         }
  46.         /** @var EntityRepositoryInterface $languageRepository */
  47.         $languageRepository $this->container->get('language.repository');
  48.         $languageRepository->create([[
  49.             'id' => Uuid::randomHex(),
  50.             'name' => self::INOCEAN_I18N_LANGUAGE_NAME,
  51.             'localeId' => $localeId,
  52.             'translationCodeId' => $localeId,
  53.             'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  54.         ]], $shopwareContext);
  55.     }
  56.     private function addBaseSnippetSet(Context $shopwareContext): void
  57.     {
  58.         /** @var EntityRepositoryInterface $snippetSetRepository */
  59.         $snippetSetRepository $this->container->get('snippet_set.repository');
  60.         $snippetSetRepository->create([[
  61.             'id' => Uuid::randomHex(),
  62.             'name' => 'BASE ' self::INOCEAN_I18N_LOCALE_CODE,
  63.             'baseFile' => (new SnippetFile())->getName(),
  64.             'iso' => self::INOCEAN_I18N_LOCALE_CODE,
  65.             'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  66.         ]], $shopwareContext);
  67.     }
  68.     private function deleteLanguage(Context $shopwareContext): void
  69.     {
  70.         /** @var EntityRepositoryInterface $languageRepository */
  71.         $languageRepository $this->container->get('language.repository');
  72.         $criteria = new Criteria();
  73.         $criteria->addFilter(
  74.             new EqualsFilter('name'self::INOCEAN_I18N_LANGUAGE_NAME)
  75.         );
  76.         $languageIds $languageRepository->searchIds($criteria$shopwareContext)->getData();
  77.         if (empty($languageIds)) {
  78.             return;
  79.         }
  80.         $languageIds array_values($languageIds);
  81.         $languageRepository->delete($languageIds$shopwareContext);
  82.     }
  83.     private function deleteBaseSnippetSet(Context $shopwareContext): void
  84.     {
  85.         /** @var EntityRepositoryInterface $snippetSetRepository */
  86.         $snippetSetRepository $this->container->get('snippet_set.repository');
  87.         $criteria = new Criteria();
  88.         $criteria->addFilter(new MultiFilter('AND', [
  89.             new EqualsFilter('name''BASE ' self::INOCEAN_I18N_LOCALE_CODE),
  90.             new EqualsFilter('baseFile', (new SnippetFile())->getName()),
  91.         ]));
  92.         $setIds $snippetSetRepository->searchIds($criteria$shopwareContext)->getData();
  93.         if (empty($setIds)) {
  94.             return;
  95.         }
  96.         $setIds array_values($setIds);
  97.         $snippetSetRepository->delete($setIds$shopwareContext);
  98.     }
  99.     private function getLocaleId(Context $shopwareContext): string
  100.     {
  101.         /** @var EntityRepositoryInterface $localeRepository */
  102.         $localeRepository $this->container->get('locale.repository');
  103.         $criteria = new Criteria();
  104.         $criteria->addFilter(new EqualsFilter('code'self::INOCEAN_I18N_LOCALE_CODE));
  105.         $localeResult $localeRepository->searchIds($criteria$shopwareContext);
  106.         if ($localeResult->getTotal() === 0) {
  107.             throw new \RuntimeException('Invalid locale. Please make sure you entered an existing locale with the correct format: xx-XX');
  108.         }
  109.         $firstId $localeResult->firstId();
  110.         if ($firstId === null) {
  111.             throw new \RuntimeException('Invalid locale. Please make sure you entered an existing locale with the correct format: xx-XX');
  112.         }
  113.         return $firstId;
  114.     }
  115.     private function isNewLanguage(string $localeIdContext $shopwareContext): bool
  116.     {
  117.         /** @var EntityRepositoryInterface $languageRepository */
  118.         $languageRepository $this->container->get('language.repository');
  119.         $criteria = new Criteria();
  120.         $criteria->addFilter(new EqualsFilter('localeId'$localeId));
  121.         $languageResult $languageRepository->searchIds($criteria$shopwareContext);
  122.         return $languageResult->getTotal() === 0;
  123.     }
  124. }