ExtensionMetadataFactory.php 6.0 KB

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