MappedEventSubscriber.php 9.4KB

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