MongoDBPurger.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Common\DataFixtures\Purger;
  4. use Doctrine\ODM\MongoDB\DocumentManager;
  5. /**
  6. * Class responsible for purging databases of data before reloading data fixtures.
  7. */
  8. class MongoDBPurger implements PurgerInterface
  9. {
  10. /** @var DocumentManager|null */
  11. private $dm;
  12. /**
  13. * Construct new purger instance.
  14. *
  15. * @param DocumentManager $dm DocumentManager instance used for persistence.
  16. */
  17. public function __construct(?DocumentManager $dm = null)
  18. {
  19. $this->dm = $dm;
  20. }
  21. /**
  22. * Set the DocumentManager instance this purger instance should use.
  23. */
  24. public function setDocumentManager(DocumentManager $dm)
  25. {
  26. $this->dm = $dm;
  27. }
  28. /**
  29. * Retrieve the DocumentManager instance this purger instance is using.
  30. *
  31. * @return DocumentManager
  32. */
  33. public function getObjectManager()
  34. {
  35. return $this->dm;
  36. }
  37. /** @inheritDoc */
  38. public function purge()
  39. {
  40. $metadatas = $this->dm->getMetadataFactory()->getAllMetadata();
  41. foreach ($metadatas as $metadata) {
  42. if ($metadata->isMappedSuperclass) {
  43. continue;
  44. }
  45. $this->dm->getDocumentCollection($metadata->name)->drop();
  46. }
  47. $this->dm->getSchemaManager()->ensureIndexes();
  48. }
  49. }