vendor/symfony-cmf/routing-bundle/src/CmfRoutingBundle.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony CMF package.
  4.  *
  5.  * (c) Symfony CMF
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Cmf\Bundle\RoutingBundle;
  11. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
  12. use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
  13. use Doctrine\ODM\PHPCR\Mapping\Driver\XmlDriver as PHPCRXmlDriver;
  14. use Doctrine\ODM\PHPCR\Version as PHPCRVersion;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Doctrine\ORM\Mapping\Driver\XmlDriver as ORMXmlDriver;
  17. use Doctrine\Persistence\Mapping\Driver\DefaultFileLocator;
  18. use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler\RedirectableMatcherPass;
  19. use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler\SetRouterPass;
  20. use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler\ValidationPass;
  21. use Symfony\Cmf\Component\Routing\DependencyInjection\Compiler\RegisterRouteEnhancersPass;
  22. use Symfony\Cmf\Component\Routing\DependencyInjection\Compiler\RegisterRoutersPass;
  23. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  24. use Symfony\Component\DependencyInjection\ContainerBuilder;
  25. use Symfony\Component\DependencyInjection\Definition;
  26. use Symfony\Component\HttpKernel\Bundle\Bundle;
  27. final class CmfRoutingBundle extends Bundle
  28. {
  29.     public function build(ContainerBuilder $container): void
  30.     {
  31.         parent::build($container);
  32.         $container->addCompilerPass(new RegisterRoutersPass());
  33.         $container->addCompilerPass(new RegisterRouteEnhancersPass());
  34.         $container->addCompilerPass(new SetRouterPass());
  35.         $container->addCompilerPass(new ValidationPass());
  36.         $container->addCompilerPass(new RedirectableMatcherPass());
  37.         $this->buildPhpcrCompilerPass($container);
  38.         $this->buildOrmCompilerPass($container);
  39.     }
  40.     /**
  41.      * Creates and registers compiler passes for PHPCR-ODM mapping if both the
  42.      * phpcr-odm and the phpcr-bundle are present.
  43.      */
  44.     private function buildPhpcrCompilerPass(ContainerBuilder $container): void
  45.     {
  46.         if (!class_exists(PHPCRVersion::class)) {
  47.             return;
  48.         }
  49.         $container->addCompilerPass(
  50.             $this->buildBaseCompilerPass(DoctrinePhpcrMappingsPass::class, PHPCRXmlDriver::class, 'phpcr')
  51.         );
  52.         $container->addCompilerPass(
  53.             DoctrinePhpcrMappingsPass::createXmlMappingDriver(
  54.                 [
  55.                     realpath(__DIR__.'/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
  56.                     realpath(__DIR__.'/Resources/config/doctrine-phpcr') => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr',
  57.                 ],
  58.                 ['cmf_routing.dynamic.persistence.phpcr.manager_name'],
  59.                 'cmf_routing.backend_type_phpcr',
  60.                 ['CmfRoutingBundle' => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr']
  61.             )
  62.         );
  63.     }
  64.     /**
  65.      * Creates and registers compiler passes for ORM mappings if doctrine ORM is available.
  66.      */
  67.     private function buildOrmCompilerPass(ContainerBuilder $container): void
  68.     {
  69.         if (!interface_exists(EntityManagerInterface::class)) {
  70.             return;
  71.         }
  72.         $container->addCompilerPass(
  73.             $this->buildBaseCompilerPass(DoctrineOrmMappingsPass::class, ORMXmlDriver::class, 'orm')
  74.         );
  75.         $container->addCompilerPass(
  76.             DoctrineOrmMappingsPass::createXmlMappingDriver(
  77.                 [
  78.                     realpath(__DIR__.'/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
  79.                     realpath(__DIR__.'/Resources/config/doctrine-orm') => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm',
  80.                 ],
  81.                 ['cmf_routing.dynamic.persistence.orm.manager_name'],
  82.                 'cmf_routing.backend_type_orm_default',
  83.                 ['CmfRoutingBundle' => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm']
  84.             )
  85.         );
  86.         $container->addCompilerPass(
  87.             DoctrineOrmMappingsPass::createXmlMappingDriver(
  88.                 [
  89.                     realpath(__DIR__.'/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
  90.                 ],
  91.                 ['cmf_routing.dynamic.persistence.orm.manager_name'],
  92.                 'cmf_routing.backend_type_orm_custom',
  93.                 []
  94.             )
  95.         );
  96.     }
  97.     /**
  98.      * Builds the compiler pass for the symfony core routing component. The
  99.      * compiler pass factory method uses the SymfonyFileLocator which does
  100.      * magic with the namespace and thus does not work here.
  101.      */
  102.     private function buildBaseCompilerPass(string $compilerClassstring $driverClassstring $type): CompilerPassInterface
  103.     {
  104.         $arguments = [[realpath(__DIR__.'/Resources/config/doctrine-base')], sprintf('.%s.xml'$type)];
  105.         $locator = new Definition(DefaultFileLocator::class, $arguments);
  106.         $driver = new Definition($driverClass, [$locator]);
  107.         return new $compilerClass(
  108.             $driver,
  109.             ['Symfony\Component\Routing'],
  110.             [sprintf('cmf_routing.dynamic.persistence.%s.manager_name'$type)],
  111.             sprintf('cmf_routing.backend_type_%s'$type)
  112.         );
  113.     }
  114. }