<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler;
use Doctrine\Common\EventSubscriber;
use Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler\Helper\TargetEntitiesResolverInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
/**
* Resolves given target entities with container parameters.
* Usable only with *doctrine/orm* driver.
*/
final class DoctrineTargetEntitiesResolverPass implements CompilerPassInterface
{
/** @var TargetEntitiesResolverInterface */
private $targetEntitiesResolver;
public function __construct(TargetEntitiesResolverInterface $targetEntitiesResolver)
{
$this->targetEntitiesResolver = $targetEntitiesResolver;
}
public function process(ContainerBuilder $container): void
{
try {
$resources = $container->getParameter('sylius.resources');
$resolveTargetEntityListener = $container->findDefinition('doctrine.orm.listeners.resolve_target_entity');
} catch (InvalidArgumentException $exception) {
return;
}
$interfaces = $this->targetEntitiesResolver->resolve($resources);
foreach ($interfaces as $interface => $model) {
$resolveTargetEntityListener->addMethodCall('addResolveTargetEntity', [$interface, $model, []]);
}
$resolveTargetEntityListenerClass = $container->getParameterBag()->resolveValue($resolveTargetEntityListener->getClass());
if (is_a($resolveTargetEntityListenerClass, EventSubscriber::class, true)) {
if (!$resolveTargetEntityListener->hasTag('doctrine.event_subscriber')) {
$resolveTargetEntityListener->addTag('doctrine.event_subscriber');
}
} elseif (!$resolveTargetEntityListener->hasTag('doctrine.event_listener')) {
$resolveTargetEntityListener->addTag('doctrine.event_listener', ['event' => 'loadClassMetadata']);
}
}
}