ExtensionMetadataFactory.php 5.5 KB

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