ExtensionMetadataFactory.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. * @var string
  28. */
  29. private $_extensionNamespace;
  30. /**
  31. * Initializes extension driver
  32. *
  33. * @param object $objectManager
  34. * @param string $extensionNamespace
  35. */
  36. public function __construct($objectManager, $extensionNamespace)
  37. {
  38. $this->_objectManager = $objectManager;
  39. $this->_extensionNamespace = $extensionNamespace;
  40. $omDriver = $objectManager->getConfiguration()->getMetadataDriverImpl();
  41. $this->_driver = $this->_getDriver($omDriver);
  42. }
  43. /**
  44. * Reads extension metadata
  45. *
  46. * @param object $meta
  47. * @return array - the metatada configuration
  48. */
  49. public function getExtensionMetadata($meta)
  50. {
  51. if ($meta->isMappedSuperclass) {
  52. return; // ignore mappedSuperclasses for now
  53. }
  54. $config = array();
  55. // collect metadata from inherited classes
  56. foreach (array_reverse(class_parents($meta->name)) as $parentClass) {
  57. // read only inherited mapped classes
  58. if ($this->_objectManager->getMetadataFactory()->hasMetadataFor($parentClass)) {
  59. $this->_driver->readExtendedMetadata($this->_objectManager->getClassMetadata($parentClass), $config);
  60. }
  61. }
  62. $this->_driver->readExtendedMetadata($meta, $config);
  63. $this->_driver->validateFullMetadata($meta, $config);
  64. if ($config) {
  65. // cache the metadata
  66. $cacheId = self::getCacheId($meta->name, $this->_extensionNamespace);
  67. if ($cacheDriver = $this->_objectManager->getMetadataFactory()->getCacheDriver()) {
  68. $cacheDriver->save($cacheId, $config, null);
  69. }
  70. }
  71. return $config;
  72. }
  73. /**
  74. * Get the cache id
  75. *
  76. * @param string $className
  77. * @param string $extensionNamespace
  78. * @return string
  79. */
  80. public static function getCacheId($className, $extensionNamespace)
  81. {
  82. return $className . '\\$' . strtoupper(str_replace('\\', '_', $extensionNamespace)) . '_CLASSMETADATA';
  83. }
  84. /**
  85. * Get the extended driver instance which will
  86. * read the metadata required by extension
  87. *
  88. * @param object $omDriver
  89. * @throws DriverException if driver was not found in extension
  90. * @return Gedmo\Mapping\Driver
  91. */
  92. private function _getDriver($omDriver)
  93. {
  94. $driver = null;
  95. $className = get_class($omDriver);
  96. $driverName = substr($className, strrpos($className, '\\') + 1);
  97. if ($driverName == 'DriverChain') {
  98. $driver = new Driver\Chain();
  99. foreach ($omDriver->getDrivers() as $namespace => $nestedOmDriver) {
  100. $driver->addDriver($this->_getDriver($nestedOmDriver), $namespace);
  101. }
  102. } else {
  103. $driverName = substr($driverName, 0, strpos($driverName, 'Driver'));
  104. // create driver instance
  105. $driverClassName = $this->_extensionNamespace . '\Mapping\Driver\\' . $driverName;
  106. if (!class_exists($driverClassName)) {
  107. // @TODO: implement XML driver also
  108. $driverClassName = $this->_extensionNamespace . '\Mapping\Driver\Annotation';
  109. if (!class_exists($driverClassName)) {
  110. throw new \Gedmo\Exception\RuntimeException("Failed to fallback to annotation driver: ({$driverClassName}), extension driver was not found.");
  111. }
  112. }
  113. $driver = new $driverClassName();
  114. if ($driver instanceof \Gedmo\Mapping\Driver\File) {
  115. $driver->setPaths($omDriver->getPaths());
  116. }
  117. }
  118. return $driver;
  119. }
  120. }