<?php
namespace Boldr\Cms\CmsBundle\DependencyInjection;
use Boldr\Cms\CmsBundle\Admin\CrudControllerConfiguratorProvider;
use Boldr\Cms\CmsBundle\Translation\EntityTranslatorProvider;
use Boldr\Cms\CmsBundle\Controller\Admin\AbstractPolymorphicCrudController;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Exception;
class CompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
// Entity translators
$entityTranslatorProvider = $container->getDefinition(EntityTranslatorProvider::class);
foreach ($container->findTaggedServiceIds('boldr_cms.entity_translator') as $id => $tags)
{
foreach ($tags as $attributes)
{
if (!isset($attributes['entity_class']))
throw new Exception('Tag definition "boldr_cms.entity_translator" for service "'.$id.'" is missing required attribute "entity_class".');
$entityTranslatorProvider->addMethodCall('addTranslator', [
$attributes['entity_class'],
new Reference($id)
]);
}
}
// Migrations
$configurationDefinition = $container->getDefinition('doctrine.migrations.configuration');
foreach ($container->getExtensions() as $extension)
{
if ($extension instanceof MigrationsExtensionInterface)
{
$configurationDefinition->addMethodCall('addMigrationsDirectory', [$extension->getMigrationsNamespace(), realpath($extension->getMigrationsPath())]);
}
}
// CrudController configurators
$crudControllerConfiguratorProvider = $container->getDefinition(CrudControllerConfiguratorProvider::class);
foreach ($container->findTaggedServiceIds('boldr_cms.crud_controller_configurator') as $id => $tags)
{
foreach ($tags as $attributes)
{
if (!isset($attributes['crud_controller_class']))
throw new Exception('Tag definition "boldr_cms.crud_controller_configurator" for service "'.$id.'" is missing required attribute "crud_controller_class".');
$crudControllerConfiguratorProvider->addMethodCall('addCrudControllerConfigurator', [
$attributes['crud_controller_class'],
new Reference($id)
]);
}
}
// Polymoprhics
$polymorphicTypeProviders = array_map(fn($id) => $container->getDefinition($id), array_keys($container->findTaggedServiceIds('boldr_cms.polymorphic_type_provider')));
foreach ($container->getDefinitions() as $id => $definition)
{
$class = $definition->getClass();
if ($class && class_exists($class))
{
$parents = class_parents($definition->getClass());
if (isset($parents[AbstractPolymorphicCrudController::class]))
{
$container->getDefinition($id)->addMethodCall('setTypeProviders', [$polymorphicTypeProviders]);
}
}
}
}
}