DoctrineExtensions.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace Gedmo;
  3. use Doctrine\Common\Annotations\AnnotationRegistry;
  4. use Doctrine\ORM\Mapping\Driver as DriverORM;
  5. use Doctrine\ODM\MongoDB\Mapping\Driver as DriverMongodbODM;
  6. use Doctrine\Common\Annotations\Reader;
  7. use Doctrine\Common\Annotations\CachedReader;
  8. use Doctrine\Common\Annotations\AnnotationReader;
  9. use Doctrine\Common\Cache\ArrayCache;
  10. /**
  11. * Version class allows to checking the dependencies required
  12. * and the current version of doctrine extensions
  13. *
  14. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  15. * @subpackage DoctrineExtensions
  16. * @package Gedmo
  17. * @link http://www.gediminasm.org
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. final class DoctrineExtensions
  21. {
  22. /**
  23. * Current version of extensions
  24. */
  25. const VERSION = '2.3.0-DEV';
  26. /**
  27. * Flag if annotations were included into registry
  28. * already
  29. *
  30. * @var boolean
  31. */
  32. private static $autoloaded = false;
  33. /**
  34. * Hooks all extensions metadata mapping drivers
  35. * into given $driverChain of drivers for ORM
  36. *
  37. * @param \Doctrine\ORM\Mapping\Driver\DriverChain $driverChain
  38. * @param \Doctrine\Common\Annotations\Reader $reader
  39. */
  40. public static function registerMappingIntoDriverChainORM(DriverORM\DriverChain $driverChain, Reader $reader = null)
  41. {
  42. self::registerAnnotations();
  43. if (!$reader) {
  44. $reader = new CachedReader(new AnnotationReader, new ArrayCache);
  45. }
  46. $annotationDriver = new DriverORM\AnnotationDriver($reader, array(
  47. __DIR__.'/Translatable/Entity',
  48. __DIR__.'/Loggable/Entity',
  49. __DIR__.'/Tree/Entity',
  50. ));
  51. $driverChain->addDriver($annotationDriver, 'Gedmo');
  52. }
  53. /**
  54. * Hooks only superclass metadata mapping drivers
  55. * into given $driverChain of drivers for ORM
  56. *
  57. * @param \Doctrine\ORM\Mapping\Driver\DriverChain $chain
  58. * @param \Doctrine\Common\Annotations\Reader $reader
  59. */
  60. public static function registerAbstractMappingIntoDriverChainORM(DriverORM\DriverChain $driverChain, Reader $reader = null)
  61. {
  62. self::registerAnnotations();
  63. if (!$reader) {
  64. $reader = new CachedReader(new AnnotationReader, new ArrayCache);
  65. }
  66. $annotationDriver = new DriverORM\AnnotationDriver($reader, array(
  67. __DIR__.'/Translatable/Entity/MappedSuperclass',
  68. __DIR__.'/Loggable/Entity/MappedSuperclass',
  69. __DIR__.'/Tree/Entity/MappedSuperclass',
  70. ));
  71. $driverChain->addDriver($annotationDriver, 'Gedmo');
  72. }
  73. /**
  74. * Hooks all extensions metadata mapping drivers
  75. * into given $driverChain of drivers for ODM MongoDB
  76. *
  77. * @param \Doctrine\ODM\MongoDB\Mapping\Driver\DriverChain $driverChain
  78. * @param \Doctrine\Common\Annotations\Reader $reader
  79. */
  80. public static function registerMappingIntoDriverChainMongodbODM(DriverMongodbODM\DriverChain $driverChain, Reader $reader = null)
  81. {
  82. self::registerAnnotations();
  83. if (!$reader) {
  84. $reader = new CachedReader(new AnnotationReader, new ArrayCache);
  85. }
  86. $annotationDriver = new DriverMongodbODM\AnnotationDriver($reader, array(
  87. __DIR__.'/Translatable/Document',
  88. __DIR__.'/Loggable/Document',
  89. ));
  90. $driverChain->addDriver($annotationDriver, 'Gedmo');
  91. }
  92. /**
  93. * Hooks only superclass metadata mapping drivers
  94. * into given $driverChain of drivers for ODM MongoDB
  95. *
  96. * @param \Doctrine\ODM\MongoDB\Mapping\Driver\DriverChain $driverChain
  97. * @param \Doctrine\Common\Annotations\Reader $reader
  98. */
  99. public static function registerAbstractMappingIntoDriverChainMongodbODM(DriverMongodbODM\DriverChain $driverChain, Reader $reader = null)
  100. {
  101. self::registerAnnotations();
  102. if (!$reader) {
  103. $reader = new CachedReader(new AnnotationReader, new ArrayCache);
  104. }
  105. $annotationDriver = new DriverMongodbODM\AnnotationDriver($reader, array(
  106. __DIR__.'/Translatable/Document/MappedSuperclass',
  107. __DIR__.'/Loggable/Document/MappedSuperclass',
  108. ));
  109. $driverChain->addDriver($annotationDriver, 'Gedmo');
  110. }
  111. /**
  112. * Includes all extension annotations once
  113. */
  114. public static function registerAnnotations()
  115. {
  116. if (!self::$autoloaded) {
  117. AnnotationRegistry::registerFile(__DIR__.'/Mapping/Annotation/All.php');
  118. self::$autoloaded = true;
  119. }
  120. }
  121. }