diff --git a/Classes/Domain/Repository/RedirectRepository.php b/Classes/Domain/Repository/RedirectRepository.php index e79fd7e..9d3bb6a 100644 --- a/Classes/Domain/Repository/RedirectRepository.php +++ b/Classes/Domain/Repository/RedirectRepository.php @@ -216,4 +216,32 @@ protected function iterate(IterableResult $iterator, callable $callback = null) $iteration++; } } + + /** + * Persists all entities managed by the repository and all cascading dependencies + * + * @return void + */ + public function persistEntities() + { + foreach ($this->entityManager->getUnitOfWork()->getIdentityMap() as $className => $entities) { + if ($className === $this->entityClassName) { + foreach ($entities as $entityToPersist) { + $this->entityManager->flush($entityToPersist); + } + $this->emitRepositoryObjectsPersisted(); + break; + } + } + } + + /** + * Signals that persistEntities() in this repository finished correctly. + * + * @Flow\Signal + * @return void + */ + protected function emitRepositoryObjectsPersisted() + { + } } diff --git a/Classes/RedirectStorage.php b/Classes/RedirectStorage.php index a8d615e..ae5bf77 100644 --- a/Classes/RedirectStorage.php +++ b/Classes/RedirectStorage.php @@ -11,7 +11,6 @@ * source code. */ -use Doctrine\ORM\OptimisticLockException; use Neos\RedirectHandler\DatabaseStorage\Domain\Model\Redirect; use Neos\RedirectHandler\DatabaseStorage\Domain\Repository\RedirectRepository; use Neos\RedirectHandler\Exception; @@ -21,7 +20,6 @@ use Neos\RedirectHandler\Traits\RedirectSignalTrait; use TYPO3\Flow\Annotations as Flow; use TYPO3\Flow\Mvc\Routing\RouterCachingService; -use TYPO3\Flow\Persistence\PersistenceManagerInterface; /** * Database Storage for the Redirects @@ -38,12 +36,6 @@ class RedirectStorage implements RedirectStorageInterface */ protected $redirectRepository; - /** - * @Flow\Inject - * @var PersistenceManagerInterface - */ - protected $persistenceManager; - /** * @Flow\Inject * @var RouterCachingService @@ -109,7 +101,8 @@ public function removeAll() /** * {@inheritdoc} */ - public function removeByHost($host = null) { + public function removeByHost($host = null) + { $this->redirectRepository->removeByHost($host); } @@ -121,7 +114,7 @@ public function addRedirect($sourceUriPath, $targetUriPath, $statusCode = null, $statusCode = $statusCode ?: (integer)$this->defaultStatusCode['redirect']; $redirects = []; if ($hosts !== []) { - array_map(function($host) use ($sourceUriPath, $targetUriPath, $statusCode, &$redirects) { + array_map(function ($host) use ($sourceUriPath, $targetUriPath, $statusCode, &$redirects) { $redirects[] = $this->addRedirectByHost($sourceUriPath, $targetUriPath, $statusCode, $host); }, $hosts); } else { @@ -165,15 +158,14 @@ protected function updateDependingRedirects(RedirectInterface $newRedirect) $existingRedirectForTargetUriPath = $this->redirectRepository->findOneBySourceUriPathAndHost($newRedirect->getTargetUriPath(), $newRedirect->getHost(), false); if ($existingRedirectForTargetUriPath !== null) { - if ($existingRedirectForTargetUriPath->getTargetUriPath() === $newRedirect->getSourceUriPath()) { - $this->redirectRepository->remove($existingRedirectForTargetUriPath); - } else { - throw new Exception(sprintf('A redirect exists for the target URI path "%s", please remove it first.', $newRedirect->getTargetUriPath()), 1382091526); - } + $this->removeAndLog($existingRedirectForTargetUriPath, sprintf('Existing redirect for the target URI path "%s" removed.', $newRedirect->getTargetUriPath())); + $this->routerCachingService->flushCachesForUriPath($existingRedirectForTargetUriPath); } if ($existingRedirectForSourceUriPath !== null) { - throw new Exception(sprintf('A redirect exists for the source URI path "%s", please remove it first.', $newRedirect->getSourceUriPath()), 1382091456); + $this->removeAndLog($existingRedirectForSourceUriPath, sprintf('Existing redirect for the source URI path "%s" removed.', $newRedirect->getSourceUriPath())); + $this->routerCachingService->flushCachesForUriPath($existingRedirectForSourceUriPath); } + $obsoleteRedirectInstances = $this->redirectRepository->findByTargetUriPathAndHost($newRedirect->getSourceUriPath(), $newRedirect->getHost()); /** @var $obsoleteRedirect Redirect */ foreach ($obsoleteRedirectInstances as $obsoleteRedirect) { @@ -186,6 +178,18 @@ protected function updateDependingRedirects(RedirectInterface $newRedirect) } } + /** + * @param RedirectInterface $redirect + * @param string $message + * @return void + */ + protected function removeAndLog(RedirectInterface $redirect, $message) + { + $this->redirectRepository->remove($redirect); + $this->redirectRepository->persistEntities(); + $this->_logger->log($message, LOG_NOTICE); + } + /** * Increment the hit counter for the given redirect *