ExtensionMetadataFactory.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /**
  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 ClassMetadata $meta
  60. * @return array - the metatada configuration
  61. */
  62. public function getExtensionMetadata(ClassMetadata $meta)
  63. {
  64. if ($meta->isMappedSuperclass) {
  65. return; // ignore mappedSuperclasses for now
  66. }
  67. $config = $supperclass = array();
  68. $useObjectName = $meta->name;
  69. // collect metadata from inherited classes
  70. foreach (array_reverse(class_parents($meta->name)) as $parentClass) {
  71. // read only inherited mapped classes
  72. if ($this->objectManager->getMetadataFactory()->hasMetadataFor($parentClass)) {
  73. $class = $this->objectManager->getClassMetadata($parentClass);
  74. $partial = array();
  75. $this->driver->readExtendedMetadata($class, $partial);
  76. if ($class->isMappedSuperclass) {
  77. $supperclass += $partial;
  78. } elseif (!$class->isInheritanceTypeNone()) {
  79. $this->driver->validateFullMetadata($class, $supperclass + $partial);
  80. if ($partial) {
  81. $useObjectName = $class->name;
  82. }
  83. }
  84. $config += $partial;
  85. }
  86. }
  87. $this->driver->readExtendedMetadata($meta, $config);
  88. if ($config) {
  89. $this->driver->validateFullMetadata($meta, $config);
  90. $config['useObjectClass'] = $useObjectName;
  91. // cache the metadata
  92. $cacheId = self::getCacheId($meta->name, $this->extensionNamespace);
  93. if ($cacheDriver = $this->objectManager->getMetadataFactory()->getCacheDriver()) {
  94. $cacheDriver->save($cacheId, $config, null);
  95. }
  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. // create driver instance
  131. $driverClassName = $this->extensionNamespace . '\Mapping\Driver\\' . $driverName;
  132. if (!class_exists($driverClassName)) {
  133. // @TODO: implement XML driver also
  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. if ($driver instanceof FileDriver) {
  141. $driver->setPaths($omDriver->getPaths());
  142. }
  143. if ($driver instanceof AnnotationDriverInterface) {
  144. $driver->setAnnotationReader($this->annotationReader);
  145. }
  146. }
  147. return $driver;
  148. }
  149. }