MappedEventSubscriber.php 3.1 KB

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