Forked from MarkusHolstein/gist:27335a360575edb4dc76fd4ed41f572a
Created
August 7, 2025 21:37
-
-
Save tic984/9d8e850184b22b88558cb53ac2f59e02 to your computer and use it in GitHub Desktop.
RegenerateRoutesCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| declare(strict_types=1); | |
| namespace App\Adapter\Symfony\Command; | |
| use Sulu\Bundle\PageBundle\Document\HomeDocument; | |
| use Sulu\Bundle\PageBundle\Document\PageDocument; | |
| use Sulu\Component\Content\Types\ResourceLocator\Strategy\ResourceLocatorStrategyPoolInterface; | |
| use Sulu\Component\DocumentManager\DocumentManagerInterface; | |
| use Symfony\Component\Console\Attribute\AsCommand; | |
| use Symfony\Component\Console\Command\Command; | |
| use Symfony\Component\Console\Input\InputArgument; | |
| use Symfony\Component\Console\Input\InputInterface; | |
| use Symfony\Component\Console\Output\OutputInterface; | |
| #[AsCommand(name: 'perspeqtive:regenerate-routes', description: 'Regeneriert alle Routen auf Basis des Default Verhaltens')] | |
| class RegenerateRoutesCommand extends Command | |
| { | |
| public function __construct( | |
| private readonly DocumentManagerInterface $documentManager, | |
| private readonly ResourceLocatorStrategyPoolInterface $resourceLocatorStrategyPool, | |
| ) { | |
| parent::__construct(); | |
| } | |
| protected function configure(): void | |
| { | |
| $this | |
| ->addArgument( | |
| 'webspace', | |
| InputArgument::REQUIRED, | |
| '', | |
| '', | |
| ) | |
| ->addArgument( | |
| 'locale', | |
| InputArgument::OPTIONAL, | |
| '', | |
| 'de', | |
| ['de'], | |
| ) | |
| ->addArgument( | |
| 'exclude', | |
| InputArgument::OPTIONAL, | |
| 'Kommaseparierte Liste', | |
| '' | |
| ); | |
| } | |
| protected function execute(InputInterface $input, OutputInterface $output): int | |
| { | |
| $locale = $input->getArgument('locale'); | |
| $webspace = $input->getArgument('webspace'); | |
| $exclude = explode(',', $input->getArgument('exclude')); | |
| /** @var HomeDocument $baseDocument */ | |
| $baseDocument = $this->documentManager->find('/cmf/' . $webspace . '/contents', $locale); | |
| $this->rebuild($baseDocument->getChildren()->toArray(), $output, $webspace, $locale, $exclude); | |
| return Command::SUCCESS; | |
| } | |
| /** | |
| * @param PageDocument[] $documents | |
| * @param OutputInterface $output | |
| * @param string $webspace | |
| * @param string $locale | |
| * @return void | |
| */ | |
| private function rebuild(array $documents, OutputInterface $output, string $webspace, string $locale, array $exclude): void | |
| { | |
| foreach ($documents as $document) { | |
| $newPath = $this->generate($webspace, $locale, $document); | |
| $url = $document->getStructure()->toArray()['url']; | |
| $parts = explode('/', trim($url, '/'), 2); | |
| if (in_array($parts[0], $exclude)) { | |
| continue; | |
| } | |
| if ($document->getResourceSegment() !== $newPath || $document->getStructure()->toArray()['url'] !== $newPath) { | |
| $output->writeln('<info>' . $document->getTitle() . ':' . $document->getResourceSegment() . '=>' . $newPath . '</info>'); | |
| $this->applyNewPath($document, $newPath, $locale); | |
| } | |
| $children = $document->getChildren(); | |
| if (count($children) > 0) { | |
| $this->rebuild($children->toArray(), $output, $webspace, $locale, $exclude); | |
| } | |
| } | |
| } | |
| private function applyNewPath(PageDocument $document, string $newPath, string $locale): void | |
| { | |
| $document->setResourceSegment($newPath); | |
| $document->getStructure()->bind(['url' => $newPath]); | |
| $this->documentManager->persist($document, $locale); | |
| if ($document->getPublished()) { | |
| $this->documentManager->publish($document, $locale); | |
| $this->documentManager->flush(); | |
| return; | |
| } | |
| $this->documentManager->unpublish($document, $locale); | |
| $this->documentManager->flush(); | |
| } | |
| private function generate(string $webspace, string $locale, PageDocument $page): string | |
| { | |
| $resourceLocatorStrategy = $this->resourceLocatorStrategyPool->getStrategyByWebspaceKey($webspace); | |
| return $resourceLocatorStrategy->generate( | |
| $page->getTitle(), | |
| $page->getParent()->getUuid(), | |
| $webspace, | |
| $locale, | |
| null, | |
| $page->getUuid() | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment