vendor/friendsofsymfony/elastica-bundle/src/Index/Resetter.php line 62

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the FOSElasticaBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\ElasticaBundle\Index;
  11. use Elastica\Exception\ResponseException;
  12. use Elastica\Type\Mapping;
  13. use FOS\ElasticaBundle\Configuration\ManagerInterface;
  14. use Elastica\Client;
  15. use FOS\ElasticaBundle\Event\IndexResetEvent;
  16. use FOS\ElasticaBundle\Event\TypeResetEvent;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface as LegacyEventDispatcherInterface;
  18. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  19. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  20. /**
  21. * Deletes and recreates indexes.
  22. */
  23. class Resetter implements ResetterInterface
  24. {
  25. /**
  26. * @var AliasProcessor
  27. */
  28. private $aliasProcessor;
  29. /***
  30. * @var ManagerInterface
  31. */
  32. private $configManager;
  33. /**
  34. * @var EventDispatcherInterface|LegacyEventDispatcherInterface
  35. */
  36. private $dispatcher;
  37. /**
  38. * @var IndexManager
  39. */
  40. private $indexManager;
  41. /**
  42. * @var MappingBuilder
  43. */
  44. private $mappingBuilder;
  45. /**
  46. * @param ManagerInterface $configManager
  47. * @param IndexManager $indexManager
  48. * @param AliasProcessor $aliasProcessor
  49. * @param MappingBuilder $mappingBuilder
  50. * @param EventDispatcherInterface|LegacyEventDispatcherInterface $eventDispatcher
  51. * @param Client $client
  52. */
  53. public function __construct(
  54. ManagerInterface $configManager,
  55. IndexManager $indexManager,
  56. AliasProcessor $aliasProcessor,
  57. MappingBuilder $mappingBuilder,
  58. /* EventDispatcherInterface */ $eventDispatcher
  59. ) {
  60. $this->aliasProcessor = $aliasProcessor;
  61. $this->configManager = $configManager;
  62. $this->dispatcher = $eventDispatcher;
  63. if (class_exists(LegacyEventDispatcherProxy::class)) {
  64. $this->dispatcher = LegacyEventDispatcherProxy::decorate($eventDispatcher);
  65. }
  66. $this->indexManager = $indexManager;
  67. $this->mappingBuilder = $mappingBuilder;
  68. }
  69. /**
  70. * Deletes and recreates all indexes.
  71. *
  72. * @param bool $populating
  73. * @param bool $force
  74. */
  75. public function resetAllIndexes($populating = false, $force = false)
  76. {
  77. foreach ($this->configManager->getIndexNames() as $name) {
  78. $this->resetIndex($name, $populating, $force);
  79. }
  80. }
  81. /**
  82. * Deletes and recreates the named index. If populating, creates a new index
  83. * with a randomised name for an alias to be set after population.
  84. *
  85. * @param string $indexName
  86. * @param bool $populating
  87. * @param bool $force If index exists with same name as alias, remove it
  88. *
  89. * @throws \InvalidArgumentException if no index exists for the given name
  90. */
  91. public function resetIndex($indexName, $populating = false, $force = false)
  92. {
  93. $indexConfig = $this->configManager->getIndexConfiguration($indexName);
  94. $index = $this->indexManager->getIndex($indexName);
  95. if ($indexConfig->isUseAlias()) {
  96. $this->aliasProcessor->setRootName($indexConfig, $index);
  97. }
  98. $event = new IndexResetEvent($indexName, $populating, $force);
  99. $this->dispatch($event, IndexResetEvent::PRE_INDEX_RESET);
  100. $mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
  101. $index->create($mapping, true);
  102. if (!$populating and $indexConfig->isUseAlias()) {
  103. $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
  104. }
  105. $this->dispatch($event, IndexResetEvent::POST_INDEX_RESET);
  106. }
  107. /**
  108. * Deletes and recreates a mapping type for the named index.
  109. *
  110. * @param string $indexName
  111. * @param string $typeName
  112. *
  113. * @throws \InvalidArgumentException if no index or type mapping exists for the given names
  114. * @throws ResponseException
  115. */
  116. public function resetIndexType($indexName, $typeName)
  117. {
  118. $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName);
  119. $this->resetIndex($indexName, true);
  120. $index = $this->indexManager->getIndex($indexName);
  121. $type = $index->getType($typeName);
  122. $event = new TypeResetEvent($indexName, $typeName);
  123. $this->dispatch($event, TypeResetEvent::PRE_TYPE_RESET);
  124. $mapping = new Mapping();
  125. foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) {
  126. $mapping->setParam($name, $field);
  127. }
  128. $type->setMapping($mapping);
  129. $this->dispatch($event, TypeResetEvent::POST_TYPE_RESET);
  130. }
  131. /**
  132. * A command run when a population has finished.
  133. *
  134. * @param string $indexName
  135. *
  136. * @deprecated
  137. */
  138. public function postPopulate($indexName)
  139. {
  140. $this->switchIndexAlias($indexName);
  141. }
  142. /**
  143. * Switching aliases.
  144. *
  145. * @param string $indexName
  146. * @param bool $delete Delete or close index
  147. *
  148. * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException
  149. */
  150. public function switchIndexAlias($indexName, $delete = true)
  151. {
  152. $indexConfig = $this->configManager->getIndexConfiguration($indexName);
  153. if ($indexConfig->isUseAlias()) {
  154. $index = $this->indexManager->getIndex($indexName);
  155. $this->aliasProcessor->switchIndexAlias($indexConfig, $index, false, $delete);
  156. }
  157. }
  158. private function dispatch($event, $eventName): void
  159. {
  160. if ($this->dispatcher instanceof EventDispatcherInterface) {
  161. // Symfony >= 4.3
  162. $this->dispatcher->dispatch($event, $eventName);
  163. } else {
  164. // Symfony 3.4
  165. $this->dispatcher->dispatch($eventName, $event);
  166. }
  167. }
  168. }