Skip to content

Commit

Permalink
TASK: Adjust to media changes for asset replacement follow up
Browse files Browse the repository at this point in the history
Moves handling of asset redirects after resource replacement
to the redirect package.

See:
neos/neos-development-collection#4815
neos/neos-development-collection#4816
  • Loading branch information
kitsunet committed Jan 4, 2024
1 parent 07eba25 commit 07f9c34
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Classes/AssetRedirectAfterReplacement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace Neos\RedirectHandler;

use GuzzleHttp\Psr7\Uri;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ResourceManagement\ResourceManager;
use Neos\Media\Domain\Model\Dto\AssetResourceReplaced;
use Neos\Media\Domain\Service\AssetResourceReplacementFollowUpInterface;
use Neos\RedirectHandler\Storage\RedirectStorageInterface;

#[Flow\Scope("singleton")]
class AssetRedirectAfterReplacement implements AssetResourceReplacementFollowUpInterface
{
#[Flow\Inject]
protected RedirectStorageInterface $redirectStorage;

#[Flow\Inject]
protected ResourceManager $resourceManager;

public function handle(AssetResourceReplaced $assetResourceReplaced): void
{
$originalResourceUriString = $this->resourceManager->getPublicPersistentResourceUri($assetResourceReplaced->previousResource);
$newResourceUriString = $this->resourceManager->getPublicPersistentResourceUri($assetResourceReplaced->newResource);

if (!is_string($originalResourceUriString) || !is_string($newResourceUriString)) {
return;
}

$existingRedirect = $this->redirectStorage->getOneBySourceUriPathAndHost($originalResourceUriString);
if ($existingRedirect === null && $originalResourceUriString !== $newResourceUriString) {
$this->redirectStorage->addRedirect(new Uri($originalResourceUriString), new Uri($newResourceUriString), 301);
}
}
}

0 comments on commit 07f9c34

Please sign in to comment.