MappedEventSubscriber.php 9.2 KB

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