vendor/coreshop/rule-bundle/DependencyInjection/Compiler/TraceableValidationProcessorPass.php line 50

Open in your IDE?
  1. <?php
  2. /**
  3.  * CoreShop.
  4.  *
  5.  * This source file is subject to the GNU General Public License version 3 (GPLv3)
  6.  * For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
  7.  * files that are distributed with this source code.
  8.  *
  9.  * @copyright  Copyright (c) CoreShop GmbH (https://www.coreshop.org)
  10.  * @license    https://www.coreshop.org/license     GNU General Public License version 3 (GPLv3)
  11.  */
  12. declare(strict_types=1);
  13. namespace CoreShop\Bundle\RuleBundle\DependencyInjection\Compiler;
  14. use CoreShop\Bundle\RuleBundle\Collector\RuleCollector;
  15. use CoreShop\Component\Rule\Condition\RuleConditionsValidationProcessorInterface;
  16. use CoreShop\Component\Rule\Condition\TraceableRuleConditionsValidationProcessor;
  17. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. use Symfony\Component\DependencyInjection\Definition;
  20. use Symfony\Component\DependencyInjection\Reference;
  21. final class TraceableValidationProcessorPass implements CompilerPassInterface
  22. {
  23.     public function process(ContainerBuilder $container): void
  24.     {
  25.         if (!$container->getParameter('kernel.debug')) {
  26.             return;
  27.         }
  28.         $validationProcessors = [];
  29.         foreach ($container->getServiceIds() as $serviceId) {
  30.             if (!$container->hasDefinition($serviceId)) {
  31.                 continue;
  32.             }
  33.             $definition $container->getDefinition($serviceId);
  34.             if (null === $definition->getClass()) {
  35.                 continue;
  36.             }
  37.             if ($definition->isDeprecated() || $definition->isAbstract()) {
  38.                 continue;
  39.             }
  40.             if (!@class_exists($definition->getClass())) {
  41.                 continue;
  42.             }
  43.             if (in_array(RuleConditionsValidationProcessorInterface::class, class_implements($definition->getClass()))) {
  44.                 //Add Decorator
  45.                 $newServiceId sprintf('%s.decorated'$serviceId);
  46.                 $serviceIdInner sprintf('%s.inner'$newServiceId);
  47.                 $decorator = new Definition(TraceableRuleConditionsValidationProcessor::class);
  48.                 $decorator->setDecoratedService($serviceId);
  49.                 $decorator->setArguments([new Reference($serviceIdInner)]);
  50.                 $decorator->setPublic(true);
  51.                 $container->setDefinition($newServiceId$decorator);
  52.                 $validationProcessors[] = new Reference($newServiceId);
  53.             }
  54.         }
  55.         if (count($validationProcessors) > 0) {
  56.             $collector = new Definition(RuleCollector::class);
  57.             $collector->setArguments([
  58.                 $validationProcessors,
  59.             ]);
  60.             $collector->addTag('data_collector', [
  61.                 'template' => '@CoreShopRule/Collector/rule.html.twig',
  62.                 'id' => 'coreshop.rule_collector',
  63.             ]);
  64.             $container->setDefinition('coreshop.rule_collector'$collector);
  65.         }
  66.     }
  67. }