<?php


namespace AutoAdmin\Bundle\Content\DependencyInjection\Compiler;

use AutoAdmin\Bundle\Content\DependencyInjection\Traits\RegisterRepositoriesTrait;
use AutoAdmin\Bundle\Content\Model\PageInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/*
 * https://symfony.com/doc/current/doctrine/resolve_target_entity.html
 * https://www.doctrine-project.org/projects/doctrine-orm/en/2.17/cookbook/resolve-target-entity-listener.html
 */

class DoctrineContentInterfacesCompilerPass implements CompilerPassInterface
{
    use RegisterRepositoriesTrait;


    public function process(ContainerBuilder $container): void
    {
        // not working using extension prepending
//        $container->prependExtensionConfig('doctrine', [
//            'orm'=> [
//                'resolve_target_entities'=> [
//                    PageInterface::class => 'App\\Entity\\Page'
//                ]
//            ],
//        ]);
//
//        $doctrineConfigs = $container->getExtensionConfig('doctrine');
//        dump($doctrineConfigs);


        // \Doctrine\ORM\Tools\ResolveTargetEntityListener definition (underlying config node "resolve_target_entities")
        $definition = $container->findDefinition('doctrine.orm.listeners.resolve_target_entity');
        $definition->addMethodCall('addResolveTargetEntity', array(
            PageInterface::class, 'App\\Entity\\Page', []
        ));
        $definition->addTag('doctrine.event_subscriber', ['connection' => 'default']);
    }
}
