MappedEventSubscriber.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\ClassLoader,
  8. Doctrine\Common\EventArgs;
  9. /**
  10. * This is extension of event subscriber class and is
  11. * used specifically for handling the extension metadata
  12. * mapping for extensions.
  13. *
  14. * It dries up some reusable code which is common for
  15. * all extensions who mapps additional metadata through
  16. * extended drivers
  17. *
  18. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  19. * @package Gedmo.Mapping
  20. * @subpackage MappedEventSubscriber
  21. * @link http://www.gediminasm.org
  22. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  23. */
  24. abstract class MappedEventSubscriber implements EventSubscriber
  25. {
  26. /**
  27. * List of cached object configurations
  28. *
  29. * @var array
  30. */
  31. protected $configurations = array();
  32. /**
  33. * ExtensionMetadataFactory used to read the extension
  34. * metadata through the extension drivers
  35. *
  36. * @var Gedmo\Mapping\ExtensionMetadataFactory
  37. */
  38. private $extensionMetadataFactory;
  39. /**
  40. * List of event adapters used for this listener
  41. *
  42. * @var array
  43. */
  44. private $adapters = array();
  45. /**
  46. * Get an event adapter to handle event specific
  47. * methods
  48. *
  49. * @param EventArgs $args
  50. * @throws \Gedmo\Exception\InvalidArgumentException - if event is not recognized
  51. * @return \Gedmo\Mapping\Event\AdapterInterface
  52. */
  53. protected function getEventAdapter(EventArgs $args)
  54. {
  55. $class = get_class($args);
  56. if (preg_match('@Doctrine\\\([^\\\]+)@', $class, $m) && in_array($m[1], array('ODM', 'ORM'))) {
  57. if (!isset($this->adapters[$m[1]])) {
  58. $adapterClass = $this->getNamespace() . '\\Mapping\\Event\\Adapter\\' . $m[1];
  59. if (!class_exists($adapterClass)) {
  60. $adapterClass = 'Gedmo\\Mapping\\Event\\Adapter\\'.$m[1];
  61. }
  62. $this->adapters[$m[1]] = new $adapterClass;
  63. }
  64. $this->adapters[$m[1]]->setEventArgs($args);
  65. return $this->adapters[$m[1]];
  66. } else {
  67. throw new \Gedmo\Exception\InvalidArgumentException('Event mapper does not support event arg class: '.$class);
  68. }
  69. }
  70. /**
  71. * Get the configuration for specific object class
  72. * if cache driver is present it scans it also
  73. *
  74. * @param ObjectManager $objectManager
  75. * @param string $class
  76. * @return array
  77. */
  78. public function getConfiguration(ObjectManager $objectManager, $class) {
  79. $config = array();
  80. if (isset($this->configurations[$class])) {
  81. $config = $this->configurations[$class];
  82. } else {
  83. $cacheDriver = $objectManager->getMetadataFactory()->getCacheDriver();
  84. $cacheId = ExtensionMetadataFactory::getCacheId($class, $this->getNamespace());
  85. if ($cacheDriver && ($cached = $cacheDriver->fetch($cacheId)) !== false) {
  86. $this->configurations[$class] = $cached;
  87. $config = $cached;
  88. }
  89. }
  90. return $config;
  91. }
  92. /**
  93. * Get extended metadata mapping reader
  94. *
  95. * @param ObjectManager $objectManager
  96. * @return Gedmo\Mapping\ExtensionMetadataFactory
  97. */
  98. public function getExtensionMetadataFactory(ObjectManager $objectManager)
  99. {
  100. if (null === $this->extensionMetadataFactory) {
  101. $this->extensionMetadataFactory = new ExtensionMetadataFactory(
  102. $objectManager,
  103. $this->getNamespace()
  104. );
  105. }
  106. return $this->extensionMetadataFactory;
  107. }
  108. /**
  109. * Scans the objects for extended annotations
  110. * event subscribers must subscribe to loadClassMetadata event
  111. *
  112. * @param ObjectManager $objectManager
  113. * @param ClassMetadata $metadata
  114. * @return void
  115. */
  116. public function loadMetadataForObjectClass(ObjectManager $objectManager, ClassMetadata $metadata)
  117. {
  118. $factory = $this->getExtensionMetadataFactory($objectManager);
  119. $config = $factory->getExtensionMetadata($metadata);
  120. if ($config) {
  121. $this->configurations[$metadata->name] = $config;
  122. }
  123. }
  124. /**
  125. * Get the namespace of extension event subscriber.
  126. * used for cache id of extensions also to know where
  127. * to find Mapping drivers and event adapters
  128. *
  129. * @return string
  130. */
  131. abstract protected function getNamespace();
  132. }