ExtensionMetadataFactory.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Gedmo\Mapping;
  3. /**
  4. * The extension metadata factory is responsible for extension driver
  5. * initialization and fully reading the extension metadata
  6. *
  7. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  8. * @package Gedmo.Mapping
  9. * @subpackage ExtensionMetadataFactory
  10. * @link http://www.gediminasm.org
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class ExtensionMetadataFactory
  14. {
  15. /**
  16. * Extension driver
  17. * @var Gedmo\Mapping\Driver
  18. */
  19. protected $driver;
  20. /**
  21. * Object manager, entity or document
  22. * @var object
  23. */
  24. private $objectManager;
  25. /**
  26. * Extension namespace
  27. *
  28. * @var string
  29. */
  30. private $extensionNamespace;
  31. /**
  32. * Initializes extension driver
  33. *
  34. * @param object $objectManager
  35. * @param string $extensionNamespace
  36. */
  37. public function __construct($objectManager, $extensionNamespace)
  38. {
  39. $this->objectManager = $objectManager;
  40. $this->extensionNamespace = $extensionNamespace;
  41. $omDriver = $objectManager->getConfiguration()->getMetadataDriverImpl();
  42. $this->driver = $this->getDriver($omDriver);
  43. }
  44. /**
  45. * Reads extension metadata
  46. *
  47. * @param object $meta
  48. * @return array - the metatada configuration
  49. */
  50. public function getExtensionMetadata($meta)
  51. {
  52. if ($meta->isMappedSuperclass) {
  53. return; // ignore mappedSuperclasses for now
  54. }
  55. $config = array();
  56. // collect metadata from inherited classes
  57. foreach (array_reverse(class_parents($meta->name)) as $parentClass) {
  58. // read only inherited mapped classes
  59. if ($this->objectManager->getMetadataFactory()->hasMetadataFor($parentClass)) {
  60. $this->driver->readExtendedMetadata($this->objectManager->getClassMetadata($parentClass), $config);
  61. }
  62. }
  63. $this->driver->readExtendedMetadata($meta, $config);
  64. $this->driver->validateFullMetadata($meta, $config);
  65. if ($config) {
  66. // cache the metadata
  67. $cacheId = self::getCacheId($meta->name, $this->extensionNamespace);
  68. if ($cacheDriver = $this->objectManager->getMetadataFactory()->getCacheDriver()) {
  69. $cacheDriver->save($cacheId, $config, null);
  70. }
  71. }
  72. return $config;
  73. }
  74. /**
  75. * Get the cache id
  76. *
  77. * @param string $className
  78. * @param string $extensionNamespace
  79. * @return string
  80. */
  81. public static function getCacheId($className, $extensionNamespace)
  82. {
  83. return $className . '\\$' . strtoupper(str_replace('\\', '_', $extensionNamespace)) . '_CLASSMETADATA';
  84. }
  85. /**
  86. * Get the extended driver instance which will
  87. * read the metadata required by extension
  88. *
  89. * @param object $omDriver
  90. * @throws DriverException if driver was not found in extension
  91. * @return Gedmo\Mapping\Driver
  92. */
  93. private function getDriver($omDriver)
  94. {
  95. $driver = null;
  96. $className = get_class($omDriver);
  97. $driverName = substr($className, strrpos($className, '\\') + 1);
  98. if ($driverName == 'DriverChain') {
  99. $driver = new Driver\Chain();
  100. foreach ($omDriver->getDrivers() as $namespace => $nestedOmDriver) {
  101. $driver->addDriver($this->getDriver($nestedOmDriver), $namespace);
  102. }
  103. } else {
  104. $driverName = substr($driverName, 0, strpos($driverName, 'Driver'));
  105. // create driver instance
  106. $driverClassName = $this->extensionNamespace . '\Mapping\Driver\\' . $driverName;
  107. if (!class_exists($driverClassName)) {
  108. // @TODO: implement XML driver also
  109. $driverClassName = $this->extensionNamespace . '\Mapping\Driver\Annotation';
  110. if (!class_exists($driverClassName)) {
  111. throw new \Gedmo\Exception\RuntimeException("Failed to fallback to annotation driver: ({$driverClassName}), extension driver was not found.");
  112. }
  113. }
  114. $driver = new $driverClassName();
  115. if ($driver instanceof \Gedmo\Mapping\Driver\File) {
  116. $driver->setPaths($omDriver->getPaths());
  117. }
  118. }
  119. return $driver;
  120. }
  121. }