MappedEventSubscriber.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Gedmo\Mapping;
  3. use Gedmo\Mapping\ExtensionMetadataFactory,
  4. Doctrine\Common\EventSubscriber,
  5. Doctrine\Common\EventArgs;
  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 implements EventSubscriber
  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 configuration for specific object class
  38. * if cache driver is present it scans it also
  39. *
  40. * @param object $objectManager
  41. * @param string $class
  42. * @return array
  43. */
  44. public function getConfiguration($objectManager, $class) {
  45. $config = array();
  46. if (isset($this->configurations[$class])) {
  47. $config = $this->configurations[$class];
  48. } else {
  49. $cacheDriver = $objectManager->getMetadataFactory()->getCacheDriver();
  50. $cacheId = ExtensionMetadataFactory::getCacheId($class, $this->getNamespace());
  51. if ($cacheDriver && ($cached = $cacheDriver->fetch($cacheId)) !== false) {
  52. $this->configurations[$class] = $cached;
  53. $config = $cached;
  54. }
  55. }
  56. return $config;
  57. }
  58. /**
  59. * Get extended metadata mapping reader
  60. *
  61. * @param object $objectManager
  62. * @return Gedmo\Mapping\ExtensionMetadataFactory
  63. */
  64. public function getExtensionMetadataFactory($objectManager)
  65. {
  66. if (null === $this->extensionMetadataFactory) {
  67. $this->extensionMetadataFactory = new ExtensionMetadataFactory(
  68. $objectManager,
  69. $this->getNamespace()
  70. );
  71. }
  72. return $this->extensionMetadataFactory;
  73. }
  74. /**
  75. * Scans the objects for extended annotations
  76. * event subscribers must subscribe to loadClassMetadata event
  77. *
  78. * @param object $objectManager
  79. * @param object $metadata
  80. * @return void
  81. */
  82. public function loadMetadataForObjectClass($objectManager, $metadata)
  83. {
  84. $factory = $this->getExtensionMetadataFactory($objectManager);
  85. $config = $factory->getExtensionMetadata($metadata);
  86. if ($config) {
  87. $this->configurations[$metadata->name] = $config;
  88. }
  89. }
  90. /**
  91. * Get the namespace of extension event subscriber.
  92. * used for cache id of extensions
  93. *
  94. * @return string
  95. */
  96. abstract protected function getNamespace();
  97. }