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

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