ExtensionMetadataFactory.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 (!$cmf instanceof DisconnectedClassMetadataFactory) {
  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. if (!$class->isInheritanceTypeNone() && !$class->parentClasses && $config) {
  78. $useObjectName = $class->name;
  79. }
  80. }
  81. }
  82. }
  83. $this->driver->readExtendedMetadata($meta, $config);
  84. if ($config) {
  85. $config['useObjectClass'] = $useObjectName;
  86. }
  87. // cache the metadata (even if it's empty)
  88. // caching empty metadata will prevent re-parsing non-existent annotations
  89. $cacheId = self::getCacheId($meta->name, $this->extensionNamespace);
  90. if ($cacheDriver = $cmf->getCacheDriver()) {
  91. $cacheDriver->save($cacheId, $config, null);
  92. }
  93. return $config;
  94. }
  95. /**
  96. * Get the cache id
  97. *
  98. * @param string $className
  99. * @param string $extensionNamespace
  100. * @return string
  101. */
  102. public static function getCacheId($className, $extensionNamespace)
  103. {
  104. return $className . '\\$' . strtoupper(str_replace('\\', '_', $extensionNamespace)) . '_CLASSMETADATA';
  105. }
  106. /**
  107. * Get the extended driver instance which will
  108. * read the metadata required by extension
  109. *
  110. * @param object $omDriver
  111. * @throws DriverException if driver was not found in extension
  112. * @return Gedmo\Mapping\Driver
  113. */
  114. private function getDriver($omDriver)
  115. {
  116. $driver = null;
  117. $className = get_class($omDriver);
  118. $driverName = substr($className, strrpos($className, '\\') + 1);
  119. if ($driverName == 'DriverChain') {
  120. $driver = new Driver\Chain();
  121. foreach ($omDriver->getDrivers() as $namespace => $nestedOmDriver) {
  122. $driver->addDriver($this->getDriver($nestedOmDriver), $namespace);
  123. }
  124. } else {
  125. $driverName = substr($driverName, 0, strpos($driverName, 'Driver'));
  126. if (substr($driverName, 0, 10) === 'Simplified') {
  127. // support for simplified file drivers
  128. $driverName = substr($driverName, 10);
  129. }
  130. // create driver instance
  131. $driverClassName = $this->extensionNamespace . '\Mapping\Driver\\' . $driverName;
  132. if (!class_exists($driverClassName)) {
  133. $driverClassName = $this->extensionNamespace . '\Mapping\Driver\Annotation';
  134. if (!class_exists($driverClassName)) {
  135. throw new \Gedmo\Exception\RuntimeException("Failed to fallback to annotation driver: ({$driverClassName}), extension driver was not found.");
  136. }
  137. }
  138. $driver = new $driverClassName();
  139. $driver->setOriginalDriver($omDriver);
  140. if ($driver instanceof FileDriver) {
  141. $driver->setPaths($omDriver->getPaths());
  142. $driver->setExtension($omDriver->getFileExtension());
  143. }
  144. if ($driver instanceof AnnotationDriverInterface) {
  145. $driver->setAnnotationReader($this->annotationReader);
  146. }
  147. }
  148. return $driver;
  149. }
  150. }