PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Gedmo/Mapping/ExtensionMetadataFactory.php

http://github.com/l3pp4rd/DoctrineExtensions
PHP | 180 lines | 118 code | 11 blank | 51 comment | 15 complexity | a98a827538d9c9dd1d9381b8d1b9d497 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. namespace Gedmo\Mapping;
  3. use Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator;
  4. use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;
  5. use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
  6. use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
  7. use Doctrine\Common\Persistence\ObjectManager;
  8. use Doctrine\Common\Version as CommonLibVer;
  9. use Gedmo\Mapping\Driver\File as FileDriver;
  10. use Gedmo\Mapping\Driver\AnnotationDriverInterface;
  11. /**
  12. * The extension metadata factory is responsible for extension driver
  13. * initialization and fully reading the extension metadata
  14. *
  15. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class ExtensionMetadataFactory
  19. {
  20. /**
  21. * Extension driver
  22. * @var \Gedmo\Mapping\Driver
  23. */
  24. protected $driver;
  25. /**
  26. * Object manager, entity or document
  27. * @var object
  28. */
  29. protected $objectManager;
  30. /**
  31. * Extension namespace
  32. *
  33. * @var string
  34. */
  35. protected $extensionNamespace;
  36. /**
  37. * Custom annotation reader
  38. *
  39. * @var object
  40. */
  41. protected $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 \Gedmo\Exception\RuntimeException if driver was not found in extension
  117. * @return \Gedmo\Mapping\Driver
  118. */
  119. protected function getDriver($omDriver)
  120. {
  121. $driver = null;
  122. $className = get_class($omDriver);
  123. $driverName = substr($className, strrpos($className, '\\') + 1);
  124. if ($omDriver instanceof MappingDriverChain || $driverName == 'DriverChain') {
  125. $driver = new Driver\Chain();
  126. foreach ($omDriver->getDrivers() as $namespace => $nestedOmDriver) {
  127. $driver->addDriver($this->getDriver($nestedOmDriver), $namespace);
  128. }
  129. if (version_compare(CommonLibVer::VERSION, '2.3.0', '>=') && $omDriver->getDefaultDriver() !== null) {
  130. $driver->setDefaultDriver($this->getDriver($omDriver->getDefaultDriver()));
  131. }
  132. } else {
  133. $driverName = substr($driverName, 0, strpos($driverName, 'Driver'));
  134. $isSimplified = false;
  135. if (substr($driverName, 0, 10) === 'Simplified') {
  136. // support for simplified file drivers
  137. $driverName = substr($driverName, 10);
  138. $isSimplified = true;
  139. }
  140. // create driver instance
  141. $driverClassName = $this->extensionNamespace.'\Mapping\Driver\\'.$driverName;
  142. if (!class_exists($driverClassName)) {
  143. $driverClassName = $this->extensionNamespace.'\Mapping\Driver\Annotation';
  144. if (!class_exists($driverClassName)) {
  145. throw new \Gedmo\Exception\RuntimeException("Failed to fallback to annotation driver: ({$driverClassName}), extension driver was not found.");
  146. }
  147. }
  148. $driver = new $driverClassName();
  149. $driver->setOriginalDriver($omDriver);
  150. if ($driver instanceof FileDriver) {
  151. /** @var $driver FileDriver */
  152. if ($omDriver instanceof MappingDriver) {
  153. $driver->setLocator($omDriver->getLocator());
  154. // BC for Doctrine 2.2
  155. } elseif ($isSimplified) {
  156. $driver->setLocator(new SymfonyFileLocator($omDriver->getNamespacePrefixes(), $omDriver->getFileExtension()));
  157. } else {
  158. $driver->setLocator(new DefaultFileLocator($omDriver->getPaths(), $omDriver->getFileExtension()));
  159. }
  160. }
  161. if ($driver instanceof AnnotationDriverInterface) {
  162. $driver->setAnnotationReader($this->annotationReader);
  163. }
  164. }
  165. return $driver;
  166. }
  167. }