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 versioning to dimension content #202

Draft
wants to merge 1 commit into
base: 0.9
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getIgnoredAttributes(object $object): array
'merged',
'dimension',
'resource',
'version',
];
}

Expand Down
8 changes: 5 additions & 3 deletions Content/Application/ContentWorkflow/ContentWorkflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ private function getWorkflow(): SymfonyWorkflowInterface
// | New |--------->| Unpublished | | Review |---------->| Published | | draft | | Review draft |
// | | | |<---------------------| | | |--------------->| |<----------------------------| |
// +-----+ +-------------+ reject +--------+ +------------+ create draft +-------+ reject draft +---------------+
// A | A |
// +---+ | publish |
// publish +----------------------------------------------------------------+
// A | A | A | A |
// +---+ +---+ | | restore | |
// restore publish | +----------------------+ |
// | publish |
// +--------------------------------------------------------------------+
Copy link
Member Author

@alexander-schranz alexander-schranz Oct 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See whole Graphic

/cc @wachterjohannes not sure if even possible have a transition with the same name with different source and target state?

published     ----restore---> draft
unpublished   ----restore---> unpublished

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Restore version a workflow transition?

Answer in my opinion - yes - it should be if possible

if even possible have a transition with the same name with different source and target state?

Yes it is for multiple sources - but as i know only to a single destination. So your example i dont think its possible (maybe with a guard or a listener which sets the destination dynamically by using the further state?)


// Configures places
$definition = $definitionBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionContentRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\TransitionEvent;

Expand All @@ -29,9 +30,17 @@ class PublishTransitionSubscriber implements EventSubscriberInterface
*/
private $contentCopier;

public function __construct(ContentCopierInterface $contentCopier)
{
/**
* @var DimensionContentRepositoryInterface
*/
private $dimensionContentRepository;

public function __construct(
ContentCopierInterface $contentCopier,
DimensionContentRepositoryInterface $dimensionContentRepository
) {
$this->contentCopier = $contentCopier;
$this->dimensionContentRepository = $dimensionContentRepository;
}

public function onPublish(TransitionEvent $transitionEvent): void
Expand Down Expand Up @@ -68,6 +77,21 @@ public function onPublish(TransitionEvent $transitionEvent): void

$dimensionAttributes['stage'] = DimensionContentInterface::STAGE_LIVE;

// create new version
// TODO optimize latest version and publish locales on write process to avoid loading them here?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 we should optimize this information

$version = 1 + $this->dimensionContentRepository->getLatestVersion($contentRichEntity);
$publishLocales = $this->dimensionContentRepository->getLocales($contentRichEntity, $dimensionAttributes);

foreach ($publishLocales as $publishLocale) {
$this->contentCopier->copy(
$contentRichEntity,
\array_merge($dimensionAttributes, ['locale' => $publishLocale]),
$contentRichEntity,
\array_merge($dimensionAttributes, ['locale' => $publishLocale, 'version' => $version])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 nice solution with the version as attribute

);
}

// publish content into live workspace
$this->contentCopier->copyFromDimensionContentCollection(
$dimensionContentCollection,
$contentRichEntity,
Expand Down
6 changes: 6 additions & 0 deletions Content/Domain/Model/DimensionContentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface DimensionContentInterface
public const STAGE_DRAFT = 'draft';
public const STAGE_LIVE = 'live';

public const DEFAULT_VERSION = 0;

public static function getResourceKey(): string;

public function getLocale(): ?string;
Expand All @@ -28,6 +30,10 @@ public function getStage(): string;

public function setStage(string $stage): void;

public function getVersion(): int;

public function setVersion(int $version): void;

public function getResource(): ContentRichEntityInterface;

public function isMerged(): bool;
Expand Down
16 changes: 16 additions & 0 deletions Content/Domain/Model/DimensionContentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ trait DimensionContentTrait
*/
protected $stage = DimensionContentInterface::STAGE_DRAFT;

/**
* @var int
*/
protected $version = DimensionContentInterface::DEFAULT_VERSION;

/**
* @var bool
*/
Expand All @@ -50,6 +55,16 @@ public function getStage(): string
return $this->stage;
}

public function setVersion(int $version): void
{
$this->version = $version;
}

public function getVersion(): int
{
return $this->version;
}

public function isMerged(): bool
{
return $this->isMerged;
Expand All @@ -65,6 +80,7 @@ public static function getDefaultDimensionAttributes(): array
return [
'locale' => null,
'stage' => DimensionContentInterface::STAGE_DRAFT,
'version' => DimensionContentInterface::DEFAULT_VERSION,
];
}

Expand Down
12 changes: 12 additions & 0 deletions Content/Domain/Repository/DimensionContentRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ public function load(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): DimensionContentCollectionInterface;

public function getLatestVersion(ContentRichEntityInterface $contentRichEntity): int;

/**
* @param mixed[] $dimensionAttributes
*
* @return string[]
*/
public function getLocales(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): array;
}
44 changes: 42 additions & 2 deletions Content/Infrastructure/Doctrine/DimensionContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public function load(

$queryBuilder = $this->entityManager->createQueryBuilder()
->from($dimensionContentClass, 'dimensionContent')
->innerJoin('dimensionContent.' . $mappingProperty, 'content')
->where('content.id = :id')
->where('IDENTITY(dimensionContent.' . $mappingProperty . ') = :id')
->setParameter('id', $contentRichEntity->getId());

$this->dimensionContentQueryEnhancer->addSelects(
Expand All @@ -77,4 +76,45 @@ public function load(
$dimensionContentClass
);
}

public function getLatestVersion(ContentRichEntityInterface $contentRichEntity): int
{
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass(\get_class($contentRichEntity));
$mappingProperty = $this->contentMetadataInspector->getDimensionContentPropertyName(\get_class($contentRichEntity));

$queryBuilder = $this->entityManager->createQueryBuilder()
->from($dimensionContentClass, 'dimensionContent')
->select('dimensionContent.version')
->orderBy('dimensionContent.version', 'DESC')
->setMaxResults(1)
->where('IDENTITY(dimensionContent.' . $mappingProperty . ') = :id')
->setParameter('id', $contentRichEntity->getId());

return (int) $queryBuilder->getQuery()->getSingleScalarResult();
}

public function getLocales(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): array {
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass(\get_class($contentRichEntity));
$mappingProperty = $this->contentMetadataInspector->getDimensionContentPropertyName(\get_class($contentRichEntity));

$queryBuilder = $this->entityManager->createQueryBuilder()
->from($dimensionContentClass, 'dimensionContent')
->select('dimensionContent.locale')
->where('IDENTITY(dimensionContent.' . $mappingProperty . ') = :id')
->andWhere('dimensionContent.locale IS NOT NULL')
->setParameter('id', $contentRichEntity->getId());

unset($dimensionAttributes['locale']);
foreach ($dimensionAttributes as $key => $value) {
$queryBuilder->andWhere('dimensionContent.' . $key . ' = :' . $key)
->setParameter(':' . $key, $value);
}

return \array_map(function($row) {
return $row['locale'];
}, $queryBuilder->getQuery()->getArrayResult());
}
}
1 change: 1 addition & 0 deletions Content/Infrastructure/Doctrine/MetadataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $event): void
if ($reflection->implementsInterface(DimensionContentInterface::class)) {
$this->addField($metadata, 'stage', 'string', ['length' => 16, 'nullable' => false]);
$this->addField($metadata, 'locale', 'string', ['length' => 7, 'nullable' => true]);
$this->addField($metadata, 'version', 'integer', ['nullable' => false, 'default' => 0]);
}

if ($reflection->implementsInterface(SeoInterface::class)) {
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@

<service id="sulu_content.publish_transition_subscriber" class="Sulu\Bundle\ContentBundle\Content\Application\ContentWorkflow\Subscriber\PublishTransitionSubscriber">
<argument type="service" id="sulu_content.content_copier"/>
<argument type="service" id="sulu_content.dimension_content_repository"/>

<tag name="kernel.event_subscriber"/>
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,46 @@ public function testLoadExistOrderedDifferent(): void
}, \iterator_to_array($dimensionContentCollection)));
}

public function testGetLatestVersion(): void
{
// prepare database
$contentRichEntity = $this->createContentRichEntity();
$this->createContentDimension($contentRichEntity, []);
$this->createContentDimension($contentRichEntity, ['locale' => 'de']);
$this->createContentDimension($contentRichEntity, ['locale' => 'de', 'version' => 1]);

$this->getEntityManager()->flush();
$this->getEntityManager()->clear();

// test functionality
$dimensionContentRepository = $this->createContentDimensionRepository();
$version = $dimensionContentRepository->getLatestVersion($contentRichEntity);

// assert result
$this->assertSame(1, $version);
}

public function testGetLocales(): void
{
// prepare database
$contentRichEntity = $this->createContentRichEntity();
$this->createContentDimension($contentRichEntity, []);
$this->createContentDimension($contentRichEntity, ['locale' => 'de']);
$this->createContentDimension($contentRichEntity, ['locale' => 'en']);
$this->createContentDimension($contentRichEntity, ['locale' => 'en', 'stage' => 'live']);
$this->createContentDimension($contentRichEntity, ['locale' => 'fr', 'stage' => 'live']);

$this->getEntityManager()->flush();
$this->getEntityManager()->clear();

// test functionality
$dimensionContentRepository = $this->createContentDimensionRepository();
$locales = $dimensionContentRepository->getLocales($contentRichEntity, ['stage' => 'live']);

// assert result
$this->assertSame(['en', 'fr'], $locales);
}

private function createContentRichEntity(): Example
{
$example = new Example();
Expand All @@ -123,6 +163,7 @@ private function createContentDimension(Example $example, array $dimensionAttrib
$exampleDimension = new ExampleDimensionContent($example);
$exampleDimension->setStage($dimensionAttributes['stage'] ?? DimensionContentInterface::STAGE_DRAFT);
$exampleDimension->setLocale($dimensionAttributes['locale'] ?? null);
$exampleDimension->setVersion($dimensionAttributes['version'] ?? 0);

$this->getEntityManager()->persist($exampleDimension);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public function testMerge(): void
->shouldBeCalled();
$mergedDimensionContent->setStage('draft')
->shouldBeCalled();
$mergedDimensionContent->setVersion(0)
->shouldBeCalled();
$mergedDimensionContent->markAsMerged()
->shouldBeCalled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testIgnoredAttributes(): void
$object = $this->prophesize(DimensionContentInterface::class);

$this->assertSame(
['id', 'merged', 'dimension', 'resource'],
['id', 'merged', 'dimension', 'resource', 'version'],
$normalizer->getIgnoredAttributes($object->reveal())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ public function testTransitionNoWorkflowInterface(): void
$dimensionContent1 = $this->prophesize(DimensionContentInterface::class);
$dimensionContent1->getStage()->willReturn('draft');
$dimensionContent1->getLocale()->willReturn(null);
$dimensionContent1->getVersion()->willReturn(0);
$dimensionContent2 = $this->prophesize(DimensionContentInterface::class);
$dimensionContent1->getStage()->willReturn('draft');
$dimensionContent1->getLocale()->willReturn('de');
$dimensionContent2->getStage()->willReturn('draft');
$dimensionContent2->getLocale()->willReturn('de');
$dimensionContent2->getVersion()->willReturn(0);

$this->expectExceptionMessage(\sprintf(
'Expected "%s" but "%s" given.',
Expand Down Expand Up @@ -166,11 +168,13 @@ public function testNotExistTransition(): void
$dimensionContent1->willImplement(WorkflowInterface::class);
$dimensionContent1->getLocale()->willReturn(null);
$dimensionContent1->getStage()->willReturn('draft');
$dimensionContent1->getVersion()->willReturn(0);

$dimensionContent2 = $this->prophesize(DimensionContentInterface::class);
$dimensionContent2->willImplement(WorkflowInterface::class);
$dimensionContent2->getLocale()->willReturn('de');
$dimensionContent2->getStage()->willReturn('draft');
$dimensionContent2->getVersion()->willReturn(0);

$dimensionContent2->getWorkflowPlace()
->willReturn('unpublished')
Expand Down Expand Up @@ -219,10 +223,12 @@ public function testTransitions(
$dimensionContent1->willImplement(WorkflowInterface::class);
$dimensionContent1->getLocale()->willReturn(null);
$dimensionContent1->getStage()->willReturn('draft');
$dimensionContent1->getVersion()->willReturn(0);
$dimensionContent2 = $this->prophesize(DimensionContentInterface::class);
$dimensionContent2->willImplement(WorkflowInterface::class);
$dimensionContent2->getLocale()->willReturn('de');
$dimensionContent2->getStage()->willReturn('draft');
$dimensionContent2->getVersion()->willReturn(0);

$dimensionContent2->getWorkflowPlace()
->willReturn($currentPlace)
Expand Down
Loading