DoctrineOrmMappingsPass.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;
  3. use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterMappingsPass;
  4. use Symfony\Component\DependencyInjection\Definition;
  5. use Symfony\Component\DependencyInjection\Reference;
  6. /**
  7. * Class for Symfony bundles to configure mappings for model classes not in the
  8. * auto-mapped folder.
  9. *
  10. * NOTE: alias is only supported by Symfony 2.6+ and will be ignored with older versions.
  11. */
  12. class DoctrineOrmMappingsPass extends RegisterMappingsPass
  13. {
  14. /**
  15. * You should not directly instantiate this class but use one of the
  16. * factory methods.
  17. *
  18. * @param Definition|Reference $driver Driver DI definition or reference.
  19. * @param string[] $namespaces List of namespaces handled by $driver.
  20. * @param string[] $managerParameters Ordered list of container parameters that
  21. * could hold the manager name.
  22. * doctrine.default_entity_manager is appended
  23. * automatically.
  24. * @param string|false $enabledParameter If specified, the compiler pass only executes
  25. * if this parameter is defined in the service
  26. * container.
  27. * @param string[] $aliasMap Map of alias to namespace.
  28. */
  29. public function __construct($driver, array $namespaces, array $managerParameters, $enabledParameter = false, array $aliasMap = [])
  30. {
  31. $managerParameters[] = 'doctrine.default_entity_manager';
  32. parent::__construct(
  33. $driver,
  34. $namespaces,
  35. $managerParameters,
  36. 'doctrine.orm.%s_metadata_driver',
  37. $enabledParameter,
  38. 'doctrine.orm.%s_configuration',
  39. 'addEntityNamespace',
  40. $aliasMap
  41. );
  42. }
  43. /**
  44. * @param string[] $namespaces Hashmap of directory path to namespace.
  45. * @param string[] $managerParameters List of parameters that could which object manager name
  46. * your bundle uses. This compiler pass will automatically
  47. * append the parameter name for the default entity manager
  48. * to this list.
  49. * @param string|false $enabledParameter Service container parameter that must be present to
  50. * enable the mapping. Set to false to not do any check,
  51. * optional.
  52. * @param string[] $aliasMap Map of alias to namespace.
  53. *
  54. * @return self
  55. */
  56. public static function createXmlMappingDriver(array $namespaces, array $managerParameters = [], $enabledParameter = false, array $aliasMap = [])
  57. {
  58. $locator = new Definition('Doctrine\Persistence\Mapping\Driver\SymfonyFileLocator', [$namespaces, '.orm.xml']);
  59. $driver = new Definition('Doctrine\ORM\Mapping\Driver\XmlDriver', [$locator]);
  60. return new DoctrineOrmMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap);
  61. }
  62. /**
  63. * @param string[] $namespaces Hashmap of directory path to namespace
  64. * @param string[] $managerParameters List of parameters that could which object manager name
  65. * your bundle uses. This compiler pass will automatically
  66. * append the parameter name for the default entity manager
  67. * to this list.
  68. * @param string|false $enabledParameter Service container parameter that must be present to
  69. * enable the mapping. Set to false to not do any check,
  70. * optional.
  71. * @param string[] $aliasMap Map of alias to namespace.
  72. *
  73. * @return self
  74. */
  75. public static function createYamlMappingDriver(array $namespaces, array $managerParameters = [], $enabledParameter = false, array $aliasMap = [])
  76. {
  77. $locator = new Definition('Doctrine\Persistence\Mapping\Driver\SymfonyFileLocator', [$namespaces, '.orm.yml']);
  78. $driver = new Definition('Doctrine\ORM\Mapping\Driver\YamlDriver', [$locator]);
  79. return new DoctrineOrmMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap);
  80. }
  81. /**
  82. * @param string[] $namespaces Hashmap of directory path to namespace
  83. * @param string[] $managerParameters List of parameters that could which object manager name
  84. * your bundle uses. This compiler pass will automatically
  85. * append the parameter name for the default entity manager
  86. * to this list.
  87. * @param string|false $enabledParameter Service container parameter that must be present to
  88. * enable the mapping. Set to false to not do any check,
  89. * optional.
  90. * @param string[] $aliasMap Map of alias to namespace.
  91. *
  92. * @return self
  93. */
  94. public static function createPhpMappingDriver(array $namespaces, array $managerParameters = [], $enabledParameter = false, array $aliasMap = [])
  95. {
  96. $locator = new Definition('Doctrine\Persistence\Mapping\Driver\SymfonyFileLocator', [$namespaces, '.php']);
  97. $driver = new Definition('Doctrine\Persistence\Mapping\Driver\PHPDriver', [$locator]);
  98. return new DoctrineOrmMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap);
  99. }
  100. /**
  101. * @param string[] $namespaces List of namespaces that are handled with annotation mapping
  102. * @param string[] $directories List of directories to look for annotated classes
  103. * @param string[] $managerParameters List of parameters that could which object manager name
  104. * your bundle uses. This compiler pass will automatically
  105. * append the parameter name for the default entity manager
  106. * to this list.
  107. * @param string|false $enabledParameter Service container parameter that must be present to
  108. * enable the mapping. Set to false to not do any check,
  109. * optional.
  110. * @param string[] $aliasMap Map of alias to namespace.
  111. *
  112. * @return self
  113. */
  114. public static function createAnnotationMappingDriver(array $namespaces, array $directories, array $managerParameters = [], $enabledParameter = false, array $aliasMap = [])
  115. {
  116. $reader = new Reference('annotation_reader');
  117. $driver = new Definition('Doctrine\ORM\Mapping\Driver\AnnotationDriver', [$reader, $directories]);
  118. return new DoctrineOrmMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap);
  119. }
  120. /**
  121. * @param string[] $namespaces List of namespaces that are handled with static php mapping
  122. * @param string[] $directories List of directories to look for static php mapping files
  123. * @param string[] $managerParameters List of parameters that could which object manager name
  124. * your bundle uses. This compiler pass will automatically
  125. * append the parameter name for the default entity manager
  126. * to this list.
  127. * @param string|false $enabledParameter Service container parameter that must be present to
  128. * enable the mapping. Set to false to not do any check,
  129. * optional.
  130. * @param string[] $aliasMap Map of alias to namespace.
  131. *
  132. * @return self
  133. */
  134. public static function createStaticPhpMappingDriver(array $namespaces, array $directories, array $managerParameters = [], $enabledParameter = false, array $aliasMap = [])
  135. {
  136. $driver = new Definition('Doctrine\Persistence\Mapping\Driver\StaticPHPDriver', [$directories]);
  137. return new DoctrineOrmMappingsPass($driver, $namespaces, $managerParameters, $enabledParameter, $aliasMap);
  138. }
  139. }