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\Common\Persistence\Mapping\Driver\DefaultFileLocator;
  14. use Doctrine\ODM\PHPCR\Mapping\Driver\XmlDriver as PHPCRXmlDriver;
  15. use Doctrine\ODM\PHPCR\Version as PHPCRVersion;
  16. use Doctrine\ORM\Mapping\Driver\XmlDriver as ORMXmlDriver;
  17. use Doctrine\ORM\Version as ORMVersion;
  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.     /**
  33.      * @param ContainerBuilder $container
  34.      */
  35.     public function build(ContainerBuilder $container)
  36.     {
  37.         parent::build($container);
  38.         $container->addCompilerPass(new RegisterRoutersPass());
  39.         $container->addCompilerPass(new RegisterRouteEnhancersPass());
  40.         $container->addCompilerPass(new SetRouterPass());
  41.         $container->addCompilerPass(new ValidationPass());
  42.         $container->addCompilerPass(new TemplatingValidatorPass());
  43.         $this->buildPhpcrCompilerPass($container);
  44.         $this->buildOrmCompilerPass($container);
  45.     }
  46.     /**
  47.      * Creates and registers compiler passes for PHPCR-ODM mapping if both the
  48.      * phpcr-odm and the phpcr-bundle are present.
  49.      *
  50.      * @param ContainerBuilder $container
  51.      */
  52.     private function buildPhpcrCompilerPass(ContainerBuilder $container)
  53.     {
  54.         if (!class_exists(PHPCRVersion::class)) {
  55.             return;
  56.         }
  57.         $container->addCompilerPass(
  58.             $this->buildBaseCompilerPass(DoctrinePhpcrMappingsPass::class, PHPCRXmlDriver::class, 'phpcr')
  59.         );
  60.         $container->addCompilerPass(
  61.             DoctrinePhpcrMappingsPass::createXmlMappingDriver(
  62.                 [
  63.                     realpath(__DIR__.'/Resources/config/doctrine-model') => 'Symfony\Cmf\Bundle\RoutingBundle\Model',
  64.                     realpath(__DIR__.'/Resources/config/doctrine-phpcr') => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr',
  65.                 ],
  66.                 ['cmf_routing.dynamic.persistence.phpcr.manager_name'],
  67.                 'cmf_routing.backend_type_phpcr',
  68.                 ['CmfRoutingBundle' => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr']
  69.             )
  70.         );
  71.     }
  72.     /**
  73.      * Creates and registers compiler passes for ORM mappings if both doctrine
  74.      * ORM and a suitable compiler pass implementation are available.
  75.      *
  76.      * @param ContainerBuilder $container
  77.      */
  78.     private function buildOrmCompilerPass(ContainerBuilder $container)
  79.     {
  80.         if (!class_exists(ORMVersion::class)) {
  81.             return;
  82.         }
  83.         $container->addCompilerPass(
  84.             $this->buildBaseCompilerPass(DoctrineOrmMappingsPass::class, ORMXmlDriver::class, '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.                 ['CmfRoutingBundle' => 'Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Orm']
  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.      * @param string $compilerClass the compiler class to instantiate
  114.      * @param string $driverClass   the xml driver class for this backend
  115.      * @param string $type          the backend type name
  116.      *
  117.      * @return CompilerPassInterface
  118.      */
  119.     private function buildBaseCompilerPass($compilerClass$driverClass$type)
  120.     {
  121.         $arguments = [[realpath(__DIR__.'/Resources/config/doctrine-base')], sprintf('.%s.xml'$type)];
  122.         $locator = new Definition(DefaultFileLocator::class, $arguments);
  123.         $driver = new Definition($driverClass, [$locator]);
  124.         return new $compilerClass(
  125.             $driver,
  126.             ['Symfony\Component\Routing'],
  127.             [sprintf('cmf_routing.dynamic.persistence.%s.manager_name'$type)],
  128.             sprintf('cmf_routing.backend_type_%s'$type)
  129.         );
  130.     }
  131. }