MongoDBPurger.php 1.4 KB

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