custom/plugins/DneCustomCssJs/src/Subscriber/ThemeCompilerSubscriber.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dne\CustomCssJs\Subscriber;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\TermsAggregation;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Bucket\TermsResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  11. use Shopware\Storefront\Event\ThemeCompilerConcatenatedScriptsEvent;
  12. use Shopware\Storefront\Event\ThemeCompilerConcatenatedStylesEvent;
  13. class ThemeCompilerSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var EntityRepositoryInterface
  17.      */
  18.     private $repository;
  19.     public function __construct(
  20.         EntityRepositoryInterface $repository
  21.     ) {
  22.         $this->repository $repository;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             ThemeCompilerConcatenatedStylesEvent::class => 'onGetConcatenatedStyles',
  28.             ThemeCompilerConcatenatedScriptsEvent::class => 'onGetConcatenatedScripts',
  29.         ];
  30.     }
  31.     public function onGetConcatenatedStyles(ThemeCompilerConcatenatedStylesEvent $event): void
  32.     {
  33.         $styles $event->getConcatenatedStyles();
  34.         $additionalStyles $this->getResources('css'$event->getSalesChannelId());
  35.         $event->setConcatenatedStyles($styles . \PHP_EOL $additionalStyles);
  36.     }
  37.     public function onGetConcatenatedScripts(ThemeCompilerConcatenatedScriptsEvent $event): void
  38.     {
  39.         $scripts $event->getConcatenatedScripts();
  40.         $additionalScripts $this->getResources('js'$event->getSalesChannelId());
  41.         $event->setConcatenatedScripts($scripts . \PHP_EOL $additionalScripts);
  42.     }
  43.     public function getResources(string $fieldstring $salesChannelId): string
  44.     {
  45.         $criteria = new Criteria();
  46.         $criteria->setLimit(1);
  47.         $criteria->addFilter(
  48.             new MultiFilter(MultiFilter::CONNECTION_AND, [
  49.                 new EqualsFilter('active'true),
  50.                 new MultiFilter(MultiFilter::CONNECTION_OR, [
  51.                     new EqualsFilter('salesChannels.id'$salesChannelId),
  52.                     new EqualsFilter('salesChannels.id'null),
  53.                 ]),
  54.             ])
  55.         );
  56.         $criteria->addAggregation(
  57.             new TermsAggregation(
  58.                 'resources',
  59.                 $field,
  60.                 0,
  61.                 new FieldSorting('name'FieldSorting::ASCENDING)
  62.             )
  63.         );
  64.         $result $this->repository->search($criteriaContext::createDefaultContext());
  65.         /** @var TermsResult $aggregation */
  66.         $aggregation $result->getAggregations()->get('resources');
  67.         $contents '';
  68.         foreach ($aggregation->getBuckets() as $bucket) {
  69.             $contents .= $bucket->getKey() . \PHP_EOL;
  70.         }
  71.         return $contents;
  72.     }
  73. }