MappedEventSubscriber.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace Gedmo\Mapping;
  3. use Doctrine\Common\Annotations\AnnotationReader;
  4. use Doctrine\Common\Annotations\CachedReader;
  5. use Doctrine\Common\Cache\ArrayCache;
  6. use Doctrine\Common\Annotations\Reader;
  7. use Gedmo\Mapping\ExtensionMetadataFactory;
  8. use Doctrine\Common\EventSubscriber;
  9. use Doctrine\Common\Persistence\ObjectManager;
  10. use Doctrine\Common\EventArgs;
  11. /**
  12. * This is extension of event subscriber class and is
  13. * used specifically for handling the extension metadata
  14. * mapping for extensions.
  15. *
  16. * It dries up some reusable code which is common for
  17. * all extensions who mapps additional metadata through
  18. * extended drivers
  19. *
  20. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  21. * @package Gedmo.Mapping
  22. * @subpackage MappedEventSubscriber
  23. * @link http://www.gediminasm.org
  24. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  25. */
  26. abstract class MappedEventSubscriber implements EventSubscriber
  27. {
  28. /**
  29. * List of cached object configurations
  30. *
  31. * @var array
  32. */
  33. protected $configurations = array();
  34. /**
  35. * ExtensionMetadataFactory used to read the extension
  36. * metadata through the extension drivers
  37. *
  38. * @var Gedmo\Mapping\ExtensionMetadataFactory
  39. */
  40. private $extensionMetadataFactory = array();
  41. /**
  42. * List of event adapters used for this listener
  43. *
  44. * @var array
  45. */
  46. private $adapters = array();
  47. /**
  48. * Custom annotation reader
  49. *
  50. * @var object
  51. */
  52. private $annotationReader;
  53. /**
  54. * @var \Doctrine\Common\Annotations\AnnotationReader
  55. */
  56. private static $defaultAnnotationReader;
  57. /**
  58. * Get an event adapter to handle event specific
  59. * methods
  60. *
  61. * @param EventArgs $args
  62. * @throws \Gedmo\Exception\InvalidArgumentException - if event is not recognized
  63. * @return \Gedmo\Mapping\Event\AdapterInterface
  64. */
  65. protected function getEventAdapter(EventArgs $args)
  66. {
  67. $class = get_class($args);
  68. if (preg_match('@Doctrine\\\([^\\\]+)@', $class, $m) && in_array($m[1], array('ODM', 'ORM'))) {
  69. if (!isset($this->adapters[$m[1]])) {
  70. $adapterClass = $this->getNamespace() . '\\Mapping\\Event\\Adapter\\' . $m[1];
  71. if (!class_exists($adapterClass)) {
  72. $adapterClass = 'Gedmo\\Mapping\\Event\\Adapter\\'.$m[1];
  73. }
  74. $this->adapters[$m[1]] = new $adapterClass;
  75. }
  76. $this->adapters[$m[1]]->setEventArgs($args);
  77. return $this->adapters[$m[1]];
  78. } else {
  79. throw new \Gedmo\Exception\InvalidArgumentException('Event mapper does not support event arg class: '.$class);
  80. }
  81. }
  82. /**
  83. * Get the configuration for specific object class
  84. * if cache driver is present it scans it also
  85. *
  86. * @param ObjectManager $objectManager
  87. * @param string $class
  88. * @return array
  89. */
  90. public function getConfiguration(ObjectManager $objectManager, $class) {
  91. $config = array();
  92. if (isset($this->configurations[$class])) {
  93. $config = $this->configurations[$class];
  94. } else {
  95. $factory = $objectManager->getMetadataFactory();
  96. $cacheDriver = $factory->getCacheDriver();
  97. if ($cacheDriver) {
  98. $cacheId = ExtensionMetadataFactory::getCacheId($class, $this->getNamespace());
  99. if (($cached = $cacheDriver->fetch($cacheId)) !== false) {
  100. $this->configurations[$class] = $cached;
  101. $config = $cached;
  102. } else {
  103. // re-generate metadata on cache miss
  104. $this->loadMetadataForObjectClass($objectManager, $factory->getMetadataFor($class));
  105. if (isset($this->configurations[$class])) {
  106. $config = $this->configurations[$class];
  107. }
  108. }
  109. }
  110. }
  111. return $config;
  112. }
  113. /**
  114. * Get extended metadata mapping reader
  115. *
  116. * @param ObjectManager $objectManager
  117. * @return Gedmo\Mapping\ExtensionMetadataFactory
  118. */
  119. public function getExtensionMetadataFactory(ObjectManager $objectManager)
  120. {
  121. $oid = spl_object_hash($objectManager);
  122. if (!isset($this->extensionMetadataFactory[$oid])) {
  123. if (is_null($this->annotationReader)) {
  124. // create default annotation reader for extensions
  125. $this->annotationReader = $this->getDefaultAnnotationReader();
  126. }
  127. $this->extensionMetadataFactory[$oid] = new ExtensionMetadataFactory(
  128. $objectManager,
  129. $this->getNamespace(),
  130. $this->annotationReader
  131. );
  132. }
  133. return $this->extensionMetadataFactory[$oid];
  134. }
  135. /**
  136. * Set annotation reader class
  137. * since older doctrine versions do not provide an interface
  138. * it must provide these methods:
  139. * getClassAnnotations([reflectionClass])
  140. * getClassAnnotation([reflectionClass], [name])
  141. * getPropertyAnnotations([reflectionProperty])
  142. * getPropertyAnnotation([reflectionProperty], [name])
  143. *
  144. * @param object $reader - annotation reader class
  145. */
  146. public function setAnnotationReader($reader)
  147. {
  148. $this->annotationReader = $reader;
  149. }
  150. /**
  151. * Scans the objects for extended annotations
  152. * event subscribers must subscribe to loadClassMetadata event
  153. *
  154. * @param ObjectManager $objectManager
  155. * @param object $metadata
  156. * @return void
  157. */
  158. public function loadMetadataForObjectClass(ObjectManager $objectManager, $metadata)
  159. {
  160. $factory = $this->getExtensionMetadataFactory($objectManager);
  161. $config = $factory->getExtensionMetadata($metadata);
  162. if ($config) {
  163. $this->configurations[$metadata->name] = $config;
  164. }
  165. }
  166. /**
  167. * Get the namespace of extension event subscriber.
  168. * used for cache id of extensions also to know where
  169. * to find Mapping drivers and event adapters
  170. *
  171. * @return string
  172. */
  173. abstract protected function getNamespace();
  174. /**
  175. * Create default annotation reader for extensions
  176. *
  177. * @return \Doctrine\Common\Annotations\AnnotationReader
  178. */
  179. private function getDefaultAnnotationReader()
  180. {
  181. if (null === self::$defaultAnnotationReader) {
  182. if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
  183. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  184. \Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace(
  185. 'Gedmo\\Mapping\\Annotation',
  186. __DIR__ . '/../../'
  187. );
  188. $reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache());
  189. } else if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0RC4-DEV', '>=')) {
  190. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  191. \Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace(
  192. 'Gedmo\\Mapping\\Annotation',
  193. __DIR__ . '/../../'
  194. );
  195. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  196. $reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache());
  197. } else if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-BETA3-DEV', '>=')) {
  198. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  199. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  200. $reader->setIgnoreNotImportedAnnotations(true);
  201. $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Annotation\\', 'gedmo');
  202. $reader->setEnableParsePhpImports(false);
  203. $reader->setAutoloadAnnotations(true);
  204. $reader = new \Doctrine\Common\Annotations\CachedReader(
  205. new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()
  206. );
  207. } else {
  208. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  209. $reader->setAutoloadAnnotations(true);
  210. $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Annotation\\', 'gedmo');
  211. $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
  212. }
  213. self::$defaultAnnotationReader = $reader;
  214. }
  215. return self::$defaultAnnotationReader;
  216. }
  217. }