custom/plugins/CompraFoundationSW6/src/Administration/Subscriber/AppSubscriber.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Compra\FoundationSW6\Administration\Subscriber;
  3. use Psr\Container\ContainerInterface;
  4. use Shopware\Core\Framework\App\Event\AppInstalledEvent;
  5. use Shopware\Core\Framework\App\Manifest\Manifest;
  6. use Shopware\Core\Framework\App\Manifest\Xml\Module;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Defaults;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\System\Language\LanguageEntity;
  11. use Shopware\Core\System\Locale\LocaleEntity;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Compra\FoundationSW6\Core\System\AppModuleService;
  14. class AppSubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var ContainerInterface  */
  17.     protected $container;
  18.     private $appRepository;
  19.     public function __construct(ContainerInterface $container)
  20.     {
  21.         $this->container $container;
  22.         $this->appRepository $container->get('app.repository');
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             AppInstalledEvent::class => 'onAppInstalled',
  28.         ];
  29.     }
  30.     // adds variant details to product extensions for listing page and ajax loaded listings
  31.     public function onAppInstalled(AppInstalledEvent $event): void
  32.     {
  33.         $app $event->getApp();
  34.         $manifest $event->getManifest();
  35.         $context $event->getContext();
  36.         $defaultLocale $this->getDefaultLocale($context);
  37.         if(!($app->getAppSecret()) && strpos($app->getName(), AppModuleService::COMPRA_PLUGIN_PREFIX) !== false) {
  38.             $this->updateAppData($manifest$app->getId(), $defaultLocale$context);
  39.         }
  40.     }
  41.     protected function getDefaultLocale(Context $context): string
  42.     {
  43.         $criteria = new Criteria([Defaults::LANGUAGE_SYSTEM]);
  44.         $criteria->addAssociation('locale');
  45.         /** @var LanguageEntity $language */
  46.         $language $this->container->get('language.repository')->search($criteria$context)->first();
  47.         /** @var LocaleEntity $locale */
  48.         $locale $language->getLocale();
  49.         return $locale->getCode();
  50.     }
  51.     protected function updateAppData(Manifest $manifeststring $idstring $defaultLocaleContext $context): void
  52.     {
  53.         $payload = [
  54.             'id' => $id,
  55.             'mainModule' => null,
  56.             'modules' => [],
  57.         ];
  58.         // checks if any admin module in registered in the app manifest-file
  59.         if ($manifest->getAdmin() !== null) {
  60.             // checks if any main module is registered (a main module replaces the "config" window for the app with the given iframe)
  61.             if ($manifest->getAdmin()->getMainModule() !== null) {
  62.                 // if configured, set the given source to the app entity
  63.                 $payload['mainModule'] = [
  64.                     'source' => $manifest->getAdmin()->getMainModule()->getSource(),
  65.                 ];
  66.             }
  67.             // loads all configured modules (menu entries) into the payload
  68.             $payload['modules'] = array_reduce(
  69.                 $manifest->getAdmin()->getModules(),
  70.                 static function (array $modulesModule $module) use ($defaultLocale) {
  71.                     $modules[] = $module->toArray($defaultLocale);
  72.                     return $modules;
  73.                 },
  74.                 []
  75.             );
  76.             if(!empty($payload['modules'])) {
  77.                 // checks if module sources are relative and adds the shop url as prefix to compile with the app module loader in vueJS
  78.                 foreach($payload['modules'] as &$module) {
  79.                     if($module['source'] && substr($module['source'][0], 01) === "/") {
  80.                         $module['source'] = $_SERVER['APP_URL'] . $module['source'];
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.         // update general modle information
  86.         $this->appRepository->update([$payload], $context);
  87.         // update app secret key
  88.         $secret AppModuleService::COMPRA_APP_SECRET;
  89.         $update = ['id' => $id'appSecret' => $secret];
  90.         $context->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($update): void {
  91.             $this->appRepository->update([$update], $context);
  92.         });
  93.     }
  94. }