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

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\Common\Persistence\PersistentObject;
  14. use Doctrine\ODM\PHPCR\Mapping\Driver\XmlDriver as PHPCRXmlDriver;
  15. use Doctrine\ODM\PHPCR\Version as PHPCRVersion;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Doctrine\ORM\Mapping\Driver\XmlDriver as ORMXmlDriver;
  18. use Doctrine\Persistence\Mapping\Driver\DefaultFileLocator;
  19. use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler\RedirectableMatcherPass;
  20. use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler\SetRouterPass;
  21. use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\Compiler\ValidationPass;
  22. use Symfony\Cmf\Component\Routing\DependencyInjection\Compiler\RegisterRouteEnhancersPass;
  23. use Symfony\Cmf\Component\Routing\DependencyInjection\Compiler\RegisterRoutersPass;
  24. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  25. use Symfony\Component\DependencyInjection\ContainerBuilder;
  26. use Symfony\Component\DependencyInjection\Definition;
  27. use Symfony\Component\HttpKernel\Bundle\Bundle;
  28. final class CmfRoutingBundle extends Bundle
  29. {
  30.     public function build(ContainerBuilder $container): void
  31.     {
  32.         parent::build($container);
  33.         $container->addCompilerPass(new RegisterRoutersPass());
  34.         $container->addCompilerPass(new RegisterRouteEnhancersPass());
  35.         $container->addCompilerPass(new SetRouterPass());
  36.         $container->addCompilerPass(new ValidationPass());
  37.         $container->addCompilerPass(new RedirectableMatcherPass());
  38.         $this->buildPhpcrCompilerPass($container);
  39.         $this->buildOrmCompilerPass($container);
  40.     }
  41.     /**
  42.      * Creates and registers compiler passes for PHPCR-ODM mapping if both the
  43.      * phpcr-odm and the phpcr-bundle are present.
  44.      */
  45.     private function buildPhpcrCompilerPass(ContainerBuilder $container): void
  46.     {
  47.         if (!class_exists(PHPCRVersion::class)) {
  48.             return;
  49.         }
  50.         $container->addCompilerPass(
  51.             $this->buildBaseCompilerPass(DoctrinePhpcrMappingsPass::class, PHPCRXmlDriver::class, 'phpcr')
  52.         );
  53.         $aliasMap = [];
  54.         // short alias is no longer supported in doctrine/persistence 3, but keep aliasing for BC with old installations
  55.         if (class_exists(PersistentObject::class)) {
  56.             $aliasMap = ['CmfRoutingBundle' => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr'];
  57.         }
  58.         $container->addCompilerPass(
  59.             DoctrinePhpcrMappingsPass::createXmlMappingDriver(
  60.                 [
  61.                     realpath(__DIR__.'/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
  62.                     realpath(__DIR__.'/Resources/config/doctrine-phpcr') => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr',
  63.                 ],
  64.                 ['cmf_routing.dynamic.persistence.phpcr.manager_name'],
  65.                 'cmf_routing.backend_type_phpcr',
  66.                 $aliasMap
  67.             )
  68.         );
  69.     }
  70.     /**
  71.      * Creates and registers compiler passes for ORM mappings if doctrine ORM is available.
  72.      */
  73.     private function buildOrmCompilerPass(ContainerBuilder $container): void
  74.     {
  75.         if (!interface_exists(EntityManagerInterface::class)) {
  76.             return;
  77.         }
  78.         $container->addCompilerPass(
  79.             $this->buildBaseCompilerPass(DoctrineOrmMappingsPass::class, ORMXmlDriver::class, 'orm')
  80.         );
  81.         $aliasMap = [];
  82.         // short alias is no longer supported in doctrine/persistence 3, but keep aliasing for BC with old installations
  83.         if (class_exists(PersistentObject::class)) {
  84.             $aliasMap = ['CmfRoutingBundle' => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm'];
  85.         }
  86.         $container->addCompilerPass(
  87.             DoctrineOrmMappingsPass::createXmlMappingDriver(
  88.                 [
  89.                     realpath(__DIR__.'/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
  90.                     realpath(__DIR__.'/Resources/config/doctrine-orm') => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm',
  91.                 ],
  92.                 ['cmf_routing.dynamic.persistence.orm.manager_name'],
  93.                 'cmf_routing.backend_type_orm_default',
  94.                 $aliasMap
  95.             )
  96.         );
  97.         $container->addCompilerPass(
  98.             DoctrineOrmMappingsPass::createXmlMappingDriver(
  99.                 [
  100.                     realpath(__DIR__.'/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
  101.                 ],
  102.                 ['cmf_routing.dynamic.persistence.orm.manager_name'],
  103.                 'cmf_routing.backend_type_orm_custom',
  104.                 []
  105.             )
  106.         );
  107.     }
  108.     /**
  109.      * Builds the compiler pass for the symfony core routing component. The
  110.      * compiler pass factory method uses the SymfonyFileLocator which does
  111.      * magic with the namespace and thus does not work here.
  112.      */
  113.     private function buildBaseCompilerPass(string $compilerClassstring $driverClassstring $type): CompilerPassInterface
  114.     {
  115.         $arguments = [[realpath(__DIR__.'/Resources/config/doctrine-base')], sprintf('.%s.xml'$type)];
  116.         $locator = new Definition(DefaultFileLocator::class, $arguments);
  117.         $driver = new Definition($driverClass, [$locator]);
  118.         return new $compilerClass(
  119.             $driver,
  120.             ['Symfony\Component\Routing'],
  121.             [sprintf('cmf_routing.dynamic.persistence.%s.manager_name'$type)],
  122.             sprintf('cmf_routing.backend_type_%s'$type)
  123.         );
  124.     }
  125. }