MappedEventSubscriber.php 8.9 KB

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