ExtensionMetadataFactory.php 5.6 KB

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