/** * Bulk generates and inserts full version records for the provided versionable entities * in MongoDB. * Return an array of ids of documents that have really changed since the last version. * * @param array $versionables * * @return array */ public function bulkPersist(array $versionables) { $versions = []; $changedDocIds = []; $author = VersionManager::DEFAULT_SYSTEM_USER; $event = $this->eventDispatcher->dispatch(BuildVersionEvents::PRE_BUILD, new BuildVersionEvent()); if (null !== $event && null !== $event->getUsername()) { $author = $event->getUsername(); } foreach ($versionables as $versionable) { $previousVersion = $this->getPreviousVersion($versionable); $context = $this->versionContext->getContextInfo(get_class($versionable)); $newVersion = $this->versionBuilder->buildVersion($versionable, $author, $previousVersion, $context); if (count($newVersion->getChangeSet()) > 0) { $versions[] = $newVersion; $changedDocIds[] = $versionable->getId(); } if (null !== $previousVersion) { $this->documentManager->detach($previousVersion); } } $mongodbVersions = []; foreach ($versions as $version) { $mongodbVersions[] = $this->normalizer->normalize($version, VersionNormalizer::FORMAT); } if (count($mongodbVersions) > 0) { $collection = $this->documentManager->getDocumentCollection($this->versionClass); $collection->batchInsert($mongodbVersions); } return $changedDocIds; }
/** * Create the pending versions for the versionable provided * * @param VersionableInterface[] $versionables */ public function persistPendingVersions(array $versionables) { $author = $this->versionManager->getUsername(); $context = $this->versionContext->getContextInfo(); $pendingVersions = []; foreach ($versionables as $versionable) { $changeset = $this->normalizer->normalize($versionable, 'csv', ['versioning' => true]); $pendingVersions[] = $this->versionBuilder->createPendingVersion($versionable, $author, $changeset, $context); } if (count($pendingVersions) > 0) { $this->batchInsertPendingVersions($pendingVersions); } }
/** * Build a version from a versionable entity * * @param object $versionable * @param array $changeset * * @return Version[] */ public function buildVersion($versionable, array $changeset = []) { $createdVersions = []; $event = $this->eventDispatcher->dispatch(BuildVersionEvents::PRE_BUILD, new BuildVersionEvent()); if (null !== $event && null !== $event->getUsername()) { $this->username = $event->getUsername(); } if ($this->realTimeVersioning) { $manager = $this->registry->getManagerForClass(ClassUtils::getClass($versionable)); $manager->refresh($versionable); $createdVersions = $this->buildPendingVersions($versionable); $builtVersions = array_filter($createdVersions, function ($version) { return count($version->getChangeset()) > 0; }); if (!empty($builtVersions)) { $previousVersion = end($builtVersions); } else { $previousVersion = $this->getNewestLogEntry($versionable); } $createdVersions[] = $this->versionBuilder->buildVersion($versionable, $this->username, $previousVersion, $this->versionContext->getContextInfo(ClassUtils::getClass($versionable))); if (null !== $previousVersion) { $manager->detach($previousVersion); } } else { $createdVersions[] = $this->versionBuilder->createPendingVersion($versionable, $this->username, $changeset, $this->versionContext->getContextInfo(ClassUtils::getClass($versionable))); } return $createdVersions; }
/** * {@inheritdoc} * * If a versionable contains no change (i.e. has nothing in its changeset) no version will be built for it. */ public function buildVersions(array $versionables) { $author = VersionManager::DEFAULT_SYSTEM_USER; $event = $this->eventDispatcher->dispatch(BuildVersionEvents::PRE_BUILD, new BuildVersionEvent()); if (null !== $event && null !== $event->getUsername()) { $author = $event->getUsername(); } $versions = []; foreach ($versionables as $versionable) { $context = $this->versionContext->getContextInfo(ClassUtils::getClass($versionable)); $previousVersion = $this->getPreviousVersion($versionable); $newVersion = $this->versionBuilder->buildVersion($versionable, $author, $previousVersion, $context); if (0 < count($newVersion->getChangeSet())) { $versions[] = $newVersion; } if (null !== $previousVersion) { $this->documentManager->detach($previousVersion); } } return $versions; }
/** * Build a version from a versionable entity * * @param object $versionable * @param array $changeset * * @return Version[] */ public function buildVersion($versionable, array $changeset = []) { $createdVersions = []; if ($this->realTimeVersioning) { $this->registry->getManagerForClass(ClassUtils::getClass($versionable))->refresh($versionable); $createdVersions = $this->buildPendingVersions($versionable); $builtVersions = array_filter($createdVersions, function ($version) { return count($version->getChangeset()) > 0; }); if (!empty($builtVersions)) { $previousVersion = end($builtVersions); } else { $previousVersion = $this->getNewestLogEntry($versionable); } $createdVersions[] = $this->versionBuilder->buildVersion($versionable, $this->username, $previousVersion, $this->versionContext->getContextInfo(ClassUtils::getClass($versionable))); } else { $createdVersions[] = $this->versionBuilder->createPendingVersion($versionable, $this->username, $changeset, $this->versionContext->getContextInfo(ClassUtils::getClass($versionable))); } return $createdVersions; }
/** * Get an object hash, provides different hashes depending on version manager context to allows to log different * versions of a same object during a request * * @param object $object * * @return string */ protected function getObjectHash($object) { return sprintf('%s#%s', spl_object_hash($object), sha1($this->versionContext->getContextInfo())); }