vendor/shopware/core/Profiling/Subscriber/ActiveRulesDataCollectorSubscriber.php line 66

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Profiling\Subscriber;
  3. use Shopware\Core\Content\Rule\RuleEntity;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  10. use Symfony\Bundle\FrameworkBundle\DataCollector\AbstractDataCollector;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\VarDumper\Cloner\Data;
  15. use Symfony\Contracts\Service\ResetInterface;
  16. /**
  17.  * @internal
  18.  */
  19. #[Package('core')]
  20. class ActiveRulesDataCollectorSubscriber extends AbstractDataCollector implements EventSubscriberInterfaceResetInterface
  21. {
  22.     /**
  23.      * @var array<string>
  24.      */
  25.     private array $ruleIds = [];
  26.     public function __construct(private readonly EntityRepository $ruleRepository)
  27.     {
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             SalesChannelContextResolvedEvent::class => 'onContextResolved',
  33.         ];
  34.     }
  35.     public function reset(): void
  36.     {
  37.         parent::reset();
  38.         $this->ruleIds = [];
  39.     }
  40.     /**
  41.      * @return array<string, RuleEntity>|Data<string, RuleEntity>
  42.      */
  43.     public function getData(): array|Data
  44.     {
  45.         return $this->data;
  46.     }
  47.     public function getMatchingRuleCount(): int
  48.     {
  49.         if ($this->data instanceof Data) {
  50.             return $this->data->count();
  51.         }
  52.         return \count($this->data);
  53.     }
  54.     public function collect(Request $requestResponse $response, ?\Throwable $exception null): void
  55.     {
  56.         $this->data $this->getMatchingRules();
  57.     }
  58.     public static function getTemplate(): string
  59.     {
  60.         return '@Profiling/Collector/rules.html.twig';
  61.     }
  62.     public function onContextResolved(SalesChannelContextResolvedEvent $event): void
  63.     {
  64.         $this->ruleIds $event->getContext()->getRuleIds();
  65.     }
  66.     /**
  67.      * @return array<string, Entity>
  68.      */
  69.     private function getMatchingRules(): array
  70.     {
  71.         if (empty($this->ruleIds)) {
  72.             return [];
  73.         }
  74.         $criteria = new Criteria($this->ruleIds);
  75.         return $this->ruleRepository->search($criteriaContext::createDefaultContext())->getElements();
  76.     }
  77. }