MappedEventSubscriber.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Gedmo\Mapping;
  3. use Gedmo\Mapping\ExtensionMetadataFactory,
  4. Doctrine\Common\EventSubscriber,
  5. Doctrine\Common\Persistence\ObjectManager,
  6. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  7. Doctrine\Common\EventArgs;
  8. /**
  9. * This is extension of event subscriber class and is
  10. * used specifically for handling the extension metadata
  11. * mapping for extensions.
  12. *
  13. * It dries up some reusable code which is common for
  14. * all extensions who mapps additional metadata through
  15. * extended drivers
  16. *
  17. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  18. * @package Gedmo.Mapping
  19. * @subpackage MappedEventSubscriber
  20. * @link http://www.gediminasm.org
  21. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  22. */
  23. abstract class MappedEventSubscriber implements EventSubscriber
  24. {
  25. /**
  26. * List of cached object configurations
  27. *
  28. * @var array
  29. */
  30. protected $configurations = array();
  31. /**
  32. * ExtensionMetadataFactory used to read the extension
  33. * metadata through the extension drivers
  34. *
  35. * @var Gedmo\Mapping\ExtensionMetadataFactory
  36. */
  37. protected $extensionMetadataFactory = null;
  38. /**
  39. * Get the configuration for specific object class
  40. * if cache driver is present it scans it also
  41. *
  42. * @param ObjectManager $objectManager
  43. * @param string $class
  44. * @return array
  45. */
  46. public function getConfiguration(ObjectManager $objectManager, $class) {
  47. $config = array();
  48. if (isset($this->configurations[$class])) {
  49. $config = $this->configurations[$class];
  50. } else {
  51. $cacheDriver = $objectManager->getMetadataFactory()->getCacheDriver();
  52. $cacheId = ExtensionMetadataFactory::getCacheId($class, $this->getNamespace());
  53. if ($cacheDriver && ($cached = $cacheDriver->fetch($cacheId)) !== false) {
  54. $this->configurations[$class] = $cached;
  55. $config = $cached;
  56. }
  57. }
  58. return $config;
  59. }
  60. /**
  61. * Get extended metadata mapping reader
  62. *
  63. * @param ObjectManager $objectManager
  64. * @return Gedmo\Mapping\ExtensionMetadataFactory
  65. */
  66. public function getExtensionMetadataFactory(ObjectManager $objectManager)
  67. {
  68. if (null === $this->extensionMetadataFactory) {
  69. $this->extensionMetadataFactory = new ExtensionMetadataFactory(
  70. $objectManager,
  71. $this->getNamespace()
  72. );
  73. }
  74. return $this->extensionMetadataFactory;
  75. }
  76. /**
  77. * Scans the objects for extended annotations
  78. * event subscribers must subscribe to loadClassMetadata event
  79. *
  80. * @param ObjectManager $objectManager
  81. * @param ClassMetadata $metadata
  82. * @return void
  83. */
  84. public function loadMetadataForObjectClass(ObjectManager $objectManager, ClassMetadata $metadata)
  85. {
  86. $factory = $this->getExtensionMetadataFactory($objectManager);
  87. $config = $factory->getExtensionMetadata($metadata);
  88. if ($config) {
  89. $this->configurations[$metadata->name] = $config;
  90. }
  91. }
  92. /**
  93. * Get the namespace of extension event subscriber.
  94. * used for cache id of extensions
  95. *
  96. * @return string
  97. */
  98. abstract protected function getNamespace();
  99. }