Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property for the routePathName #683

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Document/Subscriber/RoutableSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class RoutableSubscriber implements EventSubscriberInterface
{
public const ROUTE_FIELD = 'routePath';

public const ROUTE_FIELD_NAME = self::ROUTE_FIELD . 'Name';

public const ROUTES_PROPERTY = 'suluRoutes';

public const TAG_NAME = 'sulu_article.article_route';
Expand Down Expand Up @@ -308,6 +310,7 @@ private function updateRoute(RoutablePageBehavior $document): void

$node = $this->documentInspector->getNode($document);
$node->setProperty($propertyName, $route->getPath());
$node->setProperty($this->propertyEncoder->localizedContentName(self::ROUTE_FIELD_NAME, (string) $locale), $propertyName);
}

private function updateChildRoutes(ChildrenBehavior $document): void
Expand Down
137 changes: 137 additions & 0 deletions Resources/phpcr-migrations/Version202407111600.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle;

use Jackalope\Query\Row;
use PHPCR\Migrations\VersionInterface;
use PHPCR\PhpcrMigrationsBundle\ContainerAwareInterface;
use PHPCR\SessionInterface;
use Sulu\Bundle\ArticleBundle\Document\Subscriber\RoutableSubscriber;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\DocumentManager\PropertyEncoder;
use Sulu\Component\Localization\Localization;
use Symfony\Component\DependencyInjection\ContainerInterface;

class Version202407111600 implements VersionInterface, ContainerAwareInterface
{
/**
* @var PropertyEncoder
*/
private $propertyEncoder;

/**
* @var StructureMetadataFactoryInterface
*/
private $metadataFactory;

/**
* @var ContainerInterface
*/
private $container;

public function setContainer(?ContainerInterface $container = null): void
{
if (null === $container) {
throw new \RuntimeException('Container is required to run this migration.');
}

$this->container = $container;
}

public function up(SessionInterface $session)
{
$this->propertyEncoder = $this->container->get('sulu_document_manager.property_encoder');
$this->metadataFactory = $this->container->get('sulu_page.structure.factory');

$liveSession = $this->container->get('sulu_document_manager.live_session');
$this->upgrade($liveSession);
$this->upgrade($session);

$liveSession->save();
$session->save();
}

public function down(SessionInterface $session)
{
$this->propertyEncoder = $this->container->get('sulu_document_manager.property_encoder');
$this->metadataFactory = $this->container->get('sulu_page.structure.factory');

$liveSession = $this->container->get('sulu_document_manager.live_session');
$this->downgrade($liveSession);
$this->downgrade($session);

$liveSession->save();
$session->save();
}

private function upgrade(SessionInterface $session): void
{
$queryManager = $session->getWorkspace()->getQueryManager();
$localizations = $this->container->get('sulu_core.webspace.webspace_manager')->getAllLocalizations();

$query = 'SELECT * FROM [nt:unstructured] WHERE ([jcr:mixinTypes] = "sulu:article" OR [jcr:mixinTypes] = "sulu:articlepage")';
$rows = $queryManager->createQuery($query, 'JCR-SQL2')->execute();

/** @var Localization $localization */
foreach ($localizations as $localization) {
$locale = $localization->getLocale();
$templateKey = $this->propertyEncoder->localizedContentName('template', $locale);

/** @var Row<mixed> $row */
foreach ($rows as $row) {
$node = $row->getNode();
$structureType = $node->getPropertyValue($templateKey);
$routePathPropertyName = $this->getRoutePathPropertyName($structureType, $locale);

$propertyName = $this->propertyEncoder->localizedContentName(RoutableSubscriber::ROUTE_FIELD_NAME, $locale);
$node->setProperty($propertyName, $routePathPropertyName);
}
}
}

private function downgrade(SessionInterface $session)
{
$queryManager = $session->getWorkspace()->getQueryManager();
$localizations = $this->container->get('sulu_core.webspace.webspace_manager')->getAllLocalizations();

$query = 'SELECT * FROM [nt:unstructured] WHERE ([jcr:mixinTypes] = "sulu:article" OR [jcr:mixinTypes] = "sulu:articlepage")';
$rows = $queryManager->createQuery($query, 'JCR-SQL2')->execute();

/** @var Localization $localization */
foreach ($localizations as $localization) {
$locale = $localization->getLocale();

/** @var Row<mixed> $row */
foreach ($rows as $row) {
$node = $row->getNode();
$propertyName = $this->propertyEncoder->localizedContentName(RoutableSubscriber::ROUTE_FIELD_NAME, $locale);
$node->setProperty($propertyName, null);
}
}
}

private function getRoutePathPropertyName(string $structureType, string $locale): string
{
$metadata = $this->metadataFactory->getStructureMetadata('article', $structureType);

if ($metadata->hasPropertyWithTagName(RoutableSubscriber::TAG_NAME)) {
return $this->getPropertyName($locale, $metadata->getPropertyByTagName(RoutableSubscriber::TAG_NAME)->getName());
}

return $this->getPropertyName($locale, RoutableSubscriber::ROUTE_FIELD);
}

private function getPropertyName(string $locale, string $field): string
{
return $this->propertyEncoder->localizedSystemName($field, $locale);
}
}
8 changes: 8 additions & 0 deletions Tests/Unit/Document/Subscriber/RoutableSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public function testHandlePersist()

$this->metadataFactory->getStructureMetadata('article', 'default')->willReturn($metadata->reveal());
$this->propertyEncoder->localizedSystemName('routePath', 'de')->willReturn('i18n:de-routePath');
$this->propertyEncoder->localizedContentName('routePathName', 'de')->willReturn('i18n:de-routePathName');

$children = [
$this->prophesize(RoutablePageBehavior::class),
Expand Down Expand Up @@ -200,8 +201,11 @@ public function testHandlePersist()
$children[2]->setRoutePath('/test-3')->shouldBeCalled();

$nodes[0]->setProperty('i18n:de-routePath', '/test-1')->shouldBeCalled();
$nodes[0]->setProperty('i18n:de-routePathName', 'i18n:de-routePath')->shouldBeCalled();
$nodes[1]->setProperty('i18n:de-routePath', '/test-2')->shouldBeCalled();
$nodes[1]->setProperty('i18n:de-routePathName', 'i18n:de-routePath')->shouldBeCalled();
$nodes[2]->setProperty('i18n:de-routePath', '/test-3')->shouldBeCalled();
$nodes[2]->setProperty('i18n:de-routePathName', 'i18n:de-routePath')->shouldBeCalled();

$this->routableSubscriber->handlePersist($event->reveal());
}
Expand Down Expand Up @@ -360,6 +364,7 @@ public function testHandleReorder()

$this->metadataFactory->getStructureMetadata('article', 'default')->willReturn($metadata->reveal());
$this->propertyEncoder->localizedSystemName('routePath', 'de')->willReturn('i18n:de-routePath');
$this->propertyEncoder->localizedContentName('routePathName', 'de')->willReturn('i18n:de-routePathName');

$children = [
$this->prophesize(RoutablePageBehavior::class),
Expand Down Expand Up @@ -401,8 +406,11 @@ public function testHandleReorder()
$children[2]->setRoutePath('/test-3')->shouldBeCalled();

$nodes[0]->setProperty('i18n:de-routePath', '/test-1')->shouldBeCalled();
$nodes[0]->setProperty('i18n:de-routePathName', 'i18n:de-routePath')->shouldBeCalled();
$nodes[1]->setProperty('i18n:de-routePath', '/test-2')->shouldBeCalled();
$nodes[1]->setProperty('i18n:de-routePathName', 'i18n:de-routePath')->shouldBeCalled();
$nodes[2]->setProperty('i18n:de-routePath', '/test-3')->shouldBeCalled();
$nodes[2]->setProperty('i18n:de-routePathName', 'i18n:de-routePath')->shouldBeCalled();

$this->routableSubscriber->handleReorder($event->reveal());
}
Expand Down
Loading