ExtensionMetadataFactory.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Gedmo\Mapping;
  3. use Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator;
  4. use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;
  5. use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
  6. use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
  7. use Doctrine\Common\Persistence\ObjectManager;
  8. use Doctrine\Common\Version as CommonLibVer;
  9. use Gedmo\Mapping\Driver\File as FileDriver;
  10. use Gedmo\Mapping\Driver\AnnotationDriverInterface;
  11. use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
  12. /**
  13. * The extension metadata factory is responsible for extension driver
  14. * initialization and fully reading the extension metadata
  15. *
  16. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class ExtensionMetadataFactory
  20. {
  21. /**
  22. * Extension driver
  23. * @var \Gedmo\Mapping\Driver
  24. */
  25. protected $driver;
  26. /**
  27. * Object manager, entity or document
  28. * @var object
  29. */
  30. protected $objectManager;
  31. /**
  32. * Extension namespace
  33. *
  34. * @var string
  35. */
  36. protected $extensionNamespace;
  37. /**
  38. * Custom annotation reader
  39. *
  40. * @var object
  41. */
  42. protected $annotationReader;
  43. /**
  44. * Initializes extension driver
  45. *
  46. * @param ObjectManager $objectManager
  47. * @param string $extensionNamespace
  48. * @param object $annotationReader
  49. */
  50. public function __construct(ObjectManager $objectManager, $extensionNamespace, $annotationReader)
  51. {
  52. $this->objectManager = $objectManager;
  53. $this->annotationReader = $annotationReader;
  54. $this->extensionNamespace = $extensionNamespace;
  55. $omDriver = $objectManager->getConfiguration()->getMetadataDriverImpl();
  56. $this->driver = $this->getDriver($omDriver);
  57. }
  58. /**
  59. * Reads extension metadata
  60. *
  61. * @param object $meta
  62. * @return array - the metatada configuration
  63. */
  64. public function getExtensionMetadata($meta)
  65. {
  66. if ($meta->isMappedSuperclass) {
  67. return; // ignore mappedSuperclasses for now
  68. }
  69. $config = array();
  70. $cmf = $this->objectManager->getMetadataFactory();
  71. $useObjectName = $meta->name;
  72. // collect metadata from inherited classes
  73. if (null !== $meta->reflClass) {
  74. foreach (array_reverse(class_parents($meta->name)) as $parentClass) {
  75. // read only inherited mapped classes
  76. if ($cmf->hasMetadataFor($parentClass)) {
  77. $class = $this->objectManager->getClassMetadata($parentClass);
  78. $this->driver->readExtendedMetadata($class, $config);
  79. $isBaseInheritanceLevel = !$class->isInheritanceTypeNone()
  80. && !$class->parentClasses
  81. && $config
  82. ;
  83. if ($isBaseInheritanceLevel) {
  84. $useObjectName = $class->name;
  85. }
  86. }
  87. }
  88. $this->driver->readExtendedMetadata($meta, $config);
  89. }
  90. if ($config) {
  91. $config['useObjectClass'] = $useObjectName;
  92. }
  93. // cache the metadata (even if it's empty)
  94. // caching empty metadata will prevent re-parsing non-existent annotations
  95. $cacheId = self::getCacheId($meta->name, $this->extensionNamespace);
  96. if ($cacheDriver = $cmf->getCacheDriver()) {
  97. $cacheDriver->save($cacheId, $config, null);
  98. }
  99. return $config;
  100. }
  101. /**
  102. * Get the cache id
  103. *
  104. * @param string $className
  105. * @param string $extensionNamespace
  106. * @return string
  107. */
  108. public static function getCacheId($className, $extensionNamespace)
  109. {
  110. return $className . '\\$' . strtoupper(str_replace('\\', '_', $extensionNamespace)) . '_CLASSMETADATA';
  111. }
  112. /**
  113. * Get the extended driver instance which will
  114. * read the metadata required by extension
  115. *
  116. * @param object $omDriver
  117. * @throws \Gedmo\Exception\RuntimeException if driver was not found in extension
  118. * @return \Gedmo\Mapping\Driver
  119. */
  120. protected function getDriver($omDriver)
  121. {
  122. $driver = null;
  123. $className = get_class($omDriver);
  124. $driverName = substr($className, strrpos($className, '\\') + 1);
  125. if ($omDriver instanceof MappingDriverChain || $driverName == 'DriverChain') {
  126. $driver = new Driver\Chain();
  127. foreach ($omDriver->getDrivers() as $namespace => $nestedOmDriver) {
  128. $driver->addDriver($this->getDriver($nestedOmDriver), $namespace);
  129. }
  130. if (version_compare(CommonLibVer::VERSION, '2.3.0', '>=') && $omDriver->getDefaultDriver() !== null) {
  131. $driver->setDefaultDriver($this->getDriver($omDriver->getDefaultDriver()));
  132. }
  133. } else {
  134. $driverName = substr($driverName, 0, strpos($driverName, 'Driver'));
  135. $isSimplified = false;
  136. if (substr($driverName, 0, 10) === 'Simplified') {
  137. // support for simplified file drivers
  138. $driverName = substr($driverName, 10);
  139. $isSimplified = true;
  140. }
  141. // create driver instance
  142. $driverClassName = $this->extensionNamespace . '\Mapping\Driver\\' . $driverName;
  143. if (!class_exists($driverClassName)) {
  144. $driverClassName = $this->extensionNamespace . '\Mapping\Driver\Annotation';
  145. if (!class_exists($driverClassName)) {
  146. throw new \Gedmo\Exception\RuntimeException("Failed to fallback to annotation driver: ({$driverClassName}), extension driver was not found.");
  147. }
  148. }
  149. $driver = new $driverClassName();
  150. $driver->setOriginalDriver($omDriver);
  151. if ($driver instanceof FileDriver) {
  152. /** @var $driver FileDriver */
  153. if ($omDriver instanceof MappingDriver) {
  154. $driver->setLocator($omDriver->getLocator());
  155. // BC for Doctrine 2.2
  156. } elseif ($isSimplified) {
  157. $driver->setLocator(new SymfonyFileLocator($omDriver->getNamespacePrefixes(), $omDriver->getFileExtension()));
  158. } else {
  159. $driver->setLocator(new DefaultFileLocator($omDriver->getPaths(), $omDriver->getFileExtension()));
  160. }
  161. }
  162. if ($driver instanceof AnnotationDriverInterface) {
  163. $driver->setAnnotationReader($this->annotationReader);
  164. }
  165. }
  166. return $driver;
  167. }
  168. }