EntityLastInstalledSchemaRepository.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  4. use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
  5. /**
  6. * Provides a repository for installed entity definitions.
  7. */
  8. class EntityLastInstalledSchemaRepository implements EntityLastInstalledSchemaRepositoryInterface {
  9. /**
  10. * The key-value factory.
  11. *
  12. * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
  13. */
  14. protected $keyValueFactory;
  15. /**
  16. * Constructs a new EntityLastInstalledSchemaRepository.
  17. *
  18. * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
  19. * The key-value factory.
  20. */
  21. public function __construct(KeyValueFactoryInterface $key_value_factory) {
  22. $this->keyValueFactory = $key_value_factory;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function getLastInstalledDefinition($entity_type_id) {
  28. return $this->keyValueFactory->get('entity.definitions.installed')->get($entity_type_id . '.entity_type');
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getLastInstalledDefinitions() {
  34. $all_definitions = $this->keyValueFactory->get('entity.definitions.installed')->getAll();
  35. // Filter out field storage definitions.
  36. $filtered_keys = array_filter(array_keys($all_definitions), function ($key) {
  37. return substr($key, -12) === '.entity_type';
  38. });
  39. $entity_type_definitions = array_intersect_key($all_definitions, array_flip($filtered_keys));
  40. // Ensure that the returned array is keyed by the entity type ID.
  41. $keys = array_keys($entity_type_definitions);
  42. $keys = array_map(function ($key) {
  43. $parts = explode('.', $key);
  44. return $parts[0];
  45. }, $keys);
  46. return array_combine($keys, $entity_type_definitions);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function setLastInstalledDefinition(EntityTypeInterface $entity_type) {
  52. $entity_type_id = $entity_type->id();
  53. $this->keyValueFactory->get('entity.definitions.installed')->set($entity_type_id . '.entity_type', $entity_type);
  54. return $this;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function deleteLastInstalledDefinition($entity_type_id) {
  60. $this->keyValueFactory->get('entity.definitions.installed')->delete($entity_type_id . '.entity_type');
  61. // Clean up field storage definitions as well. Even if the entity type
  62. // isn't currently fieldable, there might be legacy definitions or an
  63. // empty array stored from when it was.
  64. $this->keyValueFactory->get('entity.definitions.installed')->delete($entity_type_id . '.field_storage_definitions');
  65. return $this;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getLastInstalledFieldStorageDefinitions($entity_type_id) {
  71. return $this->keyValueFactory->get('entity.definitions.installed')->get($entity_type_id . '.field_storage_definitions', []);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function setLastInstalledFieldStorageDefinitions($entity_type_id, array $storage_definitions) {
  77. $this->keyValueFactory->get('entity.definitions.installed')->set($entity_type_id . '.field_storage_definitions', $storage_definitions);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function setLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
  83. $entity_type_id = $storage_definition->getTargetEntityTypeId();
  84. $definitions = $this->getLastInstalledFieldStorageDefinitions($entity_type_id);
  85. $definitions[$storage_definition->getName()] = $storage_definition;
  86. $this->setLastInstalledFieldStorageDefinitions($entity_type_id, $definitions);
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function deleteLastInstalledFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
  92. $entity_type_id = $storage_definition->getTargetEntityTypeId();
  93. $definitions = $this->getLastInstalledFieldStorageDefinitions($entity_type_id);
  94. unset($definitions[$storage_definition->getName()]);
  95. $this->setLastInstalledFieldStorageDefinitions($entity_type_id, $definitions);
  96. }
  97. }