MappedEventSubscriber.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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($objectManager, $this->_getNamespace());
  68. }
  69. return $this->_extensionMetadataFactory;
  70. }
  71. /**
  72. * Scans the objects for extended annotations
  73. * event subscribers must subscribe to loadClassMetadata event
  74. *
  75. * @param object $objectManager
  76. * @param object $metadata
  77. * @return void
  78. */
  79. public function loadMetadataForObjectClass($objectManager, $metadata)
  80. {
  81. $factory = $this->getExtensionMetadataFactory($objectManager);
  82. $config = $factory->getExtensionMetadata($metadata);
  83. if ($config) {
  84. $this->_configurations[$metadata->name] = $config;
  85. }
  86. }
  87. /**
  88. * Get the namespace of extension event subscriber.
  89. * used for loading mapping drivers and cache of
  90. * extensions
  91. *
  92. * @return string
  93. */
  94. abstract protected function _getNamespace();
  95. }