ExtensionMetadataFactory.php 6.4KB

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