MappedEventSubscriber.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. private $extensionMetadataFactory;
  38. /**
  39. * List of event adapters used for this listener
  40. *
  41. * @var array
  42. */
  43. private $adapters = array();
  44. /**
  45. * Get an event adapter to handle event specific
  46. * methods
  47. *
  48. * @param EventArgs $args
  49. * @throws \Gedmo\Exception\InvalidArgumentException - if event is not recognized
  50. * @return \Gedmo\Mapping\Event\AdapterInterface
  51. */
  52. protected function getEventAdapter(EventArgs $args)
  53. {
  54. $class = get_class($args);
  55. if (preg_match('@Doctrine\\\([^\\\]+)@', $class, $m) && in_array($m[1], array('ODM', 'ORM'))) {
  56. if (!isset($this->adapters[$m[1]])) {
  57. $adapterClass = $this->getNamespace() . '\\Mapping\\Event\\Adapter\\' . $m[1];
  58. if (!class_exists($adapterClass)) {
  59. $adapterClass = 'Gedmo\\Mapping\\Event\\Adapter\\'.$m[1];
  60. }
  61. $this->adapters[$m[1]] = new $adapterClass;
  62. }
  63. $this->adapters[$m[1]]->setEventArgs($args);
  64. return $this->adapters[$m[1]];
  65. } else {
  66. throw new \Gedmo\Exception\InvalidArgumentException('Event mapper does not support event arg class: '.$class);
  67. }
  68. }
  69. /**
  70. * Get the configuration for specific object class
  71. * if cache driver is present it scans it also
  72. *
  73. * @param ObjectManager $objectManager
  74. * @param string $class
  75. * @return array
  76. */
  77. public function getConfiguration(ObjectManager $objectManager, $class) {
  78. $config = array();
  79. if (isset($this->configurations[$class])) {
  80. $config = $this->configurations[$class];
  81. } else {
  82. $cacheDriver = $objectManager->getMetadataFactory()->getCacheDriver();
  83. $cacheId = ExtensionMetadataFactory::getCacheId($class, $this->getNamespace());
  84. if ($cacheDriver && ($cached = $cacheDriver->fetch($cacheId)) !== false) {
  85. $this->configurations[$class] = $cached;
  86. $config = $cached;
  87. }
  88. }
  89. return $config;
  90. }
  91. /**
  92. * Get extended metadata mapping reader
  93. *
  94. * @param ObjectManager $objectManager
  95. * @return Gedmo\Mapping\ExtensionMetadataFactory
  96. */
  97. public function getExtensionMetadataFactory(ObjectManager $objectManager)
  98. {
  99. if (null === $this->extensionMetadataFactory) {
  100. $this->extensionMetadataFactory = new ExtensionMetadataFactory(
  101. $objectManager,
  102. $this->getNamespace()
  103. );
  104. }
  105. return $this->extensionMetadataFactory;
  106. }
  107. /**
  108. * Scans the objects for extended annotations
  109. * event subscribers must subscribe to loadClassMetadata event
  110. *
  111. * @param ObjectManager $objectManager
  112. * @param ClassMetadata $metadata
  113. * @return void
  114. */
  115. public function loadMetadataForObjectClass(ObjectManager $objectManager, ClassMetadata $metadata)
  116. {
  117. $factory = $this->getExtensionMetadataFactory($objectManager);
  118. $config = $factory->getExtensionMetadata($metadata);
  119. if ($config) {
  120. $this->configurations[$metadata->name] = $config;
  121. }
  122. }
  123. /**
  124. * Get the namespace of extension event subscriber.
  125. * used for cache id of extensions also to know where
  126. * to find Mapping drivers and event adapters
  127. *
  128. * @return string
  129. */
  130. abstract protected function getNamespace();
  131. }