Skip to content

Commit

Permalink
Merge pull request #2 from dfeyer/task-silently-remove-existing-redirect
Browse files Browse the repository at this point in the history
TASK: Silently remove existing redirect

This change removes the exception when a redirection exist for the current target or source URI. The exception is replaced by a log message.
  • Loading branch information
dfeyer authored Sep 7, 2016
2 parents d542055 + 4c665b5 commit 2c1de85
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
28 changes: 28 additions & 0 deletions Classes/Domain/Repository/RedirectRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
}
36 changes: 20 additions & 16 deletions Classes/RedirectStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -38,12 +36,6 @@ class RedirectStorage implements RedirectStorageInterface
*/
protected $redirectRepository;

/**
* @Flow\Inject
* @var PersistenceManagerInterface
*/
protected $persistenceManager;

/**
* @Flow\Inject
* @var RouterCachingService
Expand Down Expand Up @@ -109,7 +101,8 @@ public function removeAll()
/**
* {@inheritdoc}
*/
public function removeByHost($host = null) {
public function removeByHost($host = null)
{
$this->redirectRepository->removeByHost($host);
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
*
Expand Down

0 comments on commit 2c1de85

Please sign in to comment.