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

WIP - Dimension Content Versioning #256

Open
wants to merge 2 commits into
base: 0.9
Choose a base branch
from
Open
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
8 changes: 1 addition & 7 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ jobs:
env:
SYMFONY_DEPRECATIONS_HELPER: weak

- php-version: '8.3'
coverage: false
dependency-versions: 'highest'
env:
SYMFONY_DEPRECATIONS_HELPER: weak

services:
mysql:
image: mysql:5.7
Expand Down Expand Up @@ -119,7 +113,7 @@ jobs:
- name: Install and configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
php-version: 8.0
extensions: ctype, iconv, mysql

- name: Install composer dependencies
Expand Down
11 changes: 0 additions & 11 deletions Content/Application/ContentMerger/ContentMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Webmozart\Assert\Assert;

class ContentMerger implements ContentMergerInterface
{
Expand All @@ -42,13 +41,6 @@ public function __construct(
$this->propertyAccessor = $propertyAccessor;
}

/**
* @template T of DimensionContentInterface
*
* @param DimensionContentCollectionInterface<T> $dimensionContentCollection
*
* @return T
*/
public function merge(DimensionContentCollectionInterface $dimensionContentCollection): DimensionContentInterface
{
if (!$dimensionContentCollection->count()) {
Expand All @@ -60,7 +52,6 @@ public function merge(DimensionContentCollectionInterface $dimensionContentColle
foreach ($dimensionContentCollection as $dimensionContent) {
if (!$mergedDimensionContent) {
$contentRichEntity = $dimensionContent->getResource();
/** @var T $mergedDimensionContent */
$mergedDimensionContent = $contentRichEntity->createDimensionContent();
$mergedDimensionContent->markAsMerged();
}
Expand All @@ -78,8 +69,6 @@ public function merge(DimensionContentCollectionInterface $dimensionContentColle
}
}

Assert::notNull($mergedDimensionContent);

return $mergedDimensionContent;
}
}
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 All @@ -40,6 +41,7 @@ public function enhance(object $object, array $normalizedData): array
$normalizedData['id'] = $object->getResource()->getId();
$normalizedData['locale'] = $object->getLocale();
$normalizedData['stage'] = $object->getStage();
$normalizedData['version'] = $object->getVersion();

return $normalizedData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ShadowInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
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 @@ -36,9 +37,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 @@ -77,6 +86,17 @@ public function onPublish(TransitionEvent $transitionEvent): void
$targetDimensionAttributes = $dimensionAttributes;
$targetDimensionAttributes['stage'] = DimensionContentInterface::STAGE_LIVE;

$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' => time()])
);
}

$shadowLocale = $dimensionContent instanceof ShadowInterface
? $dimensionContent->getShadowLocale()
: null;
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 @@ -21,6 +21,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 Down Expand Up @@ -59,6 +61,10 @@ public function getStage(): string;
*/
public function setStage(string $stage): void;

public function setVersion(int $version): void;

public function getVersion(): int;

/**
* @return T
*/
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 @@ -35,6 +35,11 @@ trait DimensionContentTrait
*/
protected $stage = DimensionContentInterface::STAGE_DRAFT;

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

/**
* @var bool
*/
Expand Down Expand Up @@ -114,6 +119,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 @@ -132,6 +147,7 @@ public static function getDefaultDimensionAttributes(): array
return [
'locale' => null,
'stage' => DimensionContentInterface::STAGE_DRAFT,
'version' => DimensionContentInterface::DEFAULT_VERSION,
];
}

Expand Down
4 changes: 2 additions & 2 deletions Content/Domain/Model/TemplateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function getTemplateKey(): ?string;
public function setTemplateKey(string $templateKey): void;

/**
* @return array<string, mixed>
* @return mixed[]
*/
public function getTemplateData(): array;

/**
* @param array<string, mixed> $templateData
* @param mixed[] $templateData
*/
public function setTemplateData(array $templateData): void;
}
5 changes: 1 addition & 4 deletions Content/Domain/Model/TemplateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ trait TemplateTrait
private $templateKey;

/**
* @var array<string, mixed>
* @var mixed[]
*/
private $templateData = [];

Expand All @@ -43,9 +43,6 @@ public function getTemplateData(): array
return $this->templateData;
}

/**
* @param array<string, mixed> $templateData
*/
public function setTemplateData(array $templateData): void
{
$this->templateData = $templateData;
Expand Down
10 changes: 10 additions & 0 deletions Content/Domain/Repository/DimensionContentRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,14 @@ public function load(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): DimensionContentCollectionInterface;

/**
* @param mixed[] $dimensionAttributes
*
* @return string[]
*/
public function getLocales(
ContentRichEntityInterface $contentRichEntity,
array $dimensionAttributes
): array;
}
26 changes: 26 additions & 0 deletions Content/Infrastructure/Doctrine/DimensionContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function load(
$queryBuilder = $this->entityManager->createQueryBuilder()
->from($dimensionContentClass, 'dimensionContent')
->where('dimensionContent.' . $mappingProperty . ' = :id')
->andWhere('dimensionContent.version = 0')
->setParameter('id', $contentRichEntity->getId());

$this->dimensionContentQueryEnhancer->addSelects(
Expand All @@ -84,4 +85,29 @@ public function load(
$dimensionContentClass
);
}

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());
}
}
2 changes: 2 additions & 0 deletions Content/Infrastructure/Doctrine/MetadataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ 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', ['default' => 0, 'nullable' => true]);
$this->addField($metadata, 'ghostLocale', 'string', ['length' => 7, 'nullable' => true]);
$this->addField($metadata, 'availableLocales', 'json', ['nullable' => true, 'options' => ['jsonb' => true]]);
$this->addIndex($metadata, 'idx_dimension', ['stage', 'locale']);
$this->addIndex($metadata, 'idx_locale', ['locale']);
$this->addIndex($metadata, 'idx_version', ['version']);
$this->addIndex($metadata, 'idx_stage', ['stage']);
}

Expand Down
4 changes: 1 addition & 3 deletions Content/Infrastructure/Sulu/Link/ContentLinkProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ public function preload(array $hrefs, $locale, $published = true): array
*/
protected function getTitle(DimensionContentInterface $dimensionContent, array $data): ?string
{
$title = $data['title'] ?? $data['name'] ?? null;

return \is_string($title) ? $title : null;
return $data['title'] ?? $data['name'] ?? null;
}

protected function getEntityIdField(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function getMaxPage($scheme, $host): int
{
$queryBuilder = $this->createRoutesQueryBuilder();
try {
$amount = (int) $queryBuilder
$amount = $queryBuilder
->select('COUNT(' . self::ROUTE_ALIAS . ')')
->getQuery()
->getSingleScalarResult();
Expand Down
6 changes: 2 additions & 4 deletions Content/Infrastructure/Sulu/Teaser/ContentTeaserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected function createTeaser(DimensionContentInterface $dimensionContent, arr

/**
* @param B $dimensionContent
* @param array<string, mixed> $data
* @param mixed[] $data
*/
protected function getTitle(DimensionContentInterface $dimensionContent, array $data): ?string
{
Expand All @@ -172,9 +172,7 @@ protected function getTitle(DimensionContentInterface $dimensionContent, array $
}
}

$title = $data['title'] ?? $data['name'] ?? null;

return \is_string($title) ? $title : null;
return $data['title'] ?? $data['name'] ?? null;
}

/**
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,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 @@ -99,6 +99,7 @@ public function cgetAction(Request $request): Response
$listBuilder = $this->listBuilderFactory->create(Example::class);
$listBuilder->addSelectField($fieldDescriptors['locale']);
$listBuilder->addSelectField($fieldDescriptors['ghostLocale']);
$listBuilder->addSelectField($fieldDescriptors['version']);
$listBuilder->setParameter('locale', $request->query->get('locale'));
$this->restHelper->initializeListBuilder($listBuilder, $fieldDescriptors);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,10 @@ public function getTitle(): ?string
return $this->title;
}

/**
* @param array<string, mixed> $templateData
*/
public function setTemplateData(array $templateData): void
{
if (\array_key_exists('title', $templateData)) {
$this->title = \is_string($templateData['title']) ? $templateData['title'] : null;
$this->title = $templateData['title'];
}

$this->parentSetTemplateData($templateData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function countBy(array $filters = []): int

$queryBuilder->select('COUNT(DISTINCT example.id)');

return (int) $queryBuilder->getQuery()->getSingleScalarResult();
return (int) $queryBuilder->getQuery()->getSingleScalarResult(); // @phpstan-ignore-line
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<entity-name>dimensionContent</entity-name>
<field-name>Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example.dimensionContents</field-name>
<method>LEFT</method>
<condition>dimensionContent.locale = :locale AND dimensionContent.stage = 'draft'</condition>
<condition>dimensionContent.locale = :locale AND dimensionContent.stage = 'draft' AND dimensionContent.version = '0'</condition>
</join>
</joins>

Expand All @@ -16,7 +16,7 @@
<entity-name>unlocalizedDimensionContent</entity-name>
<field-name>Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example.dimensionContents</field-name>
<method>LEFT</method>
<condition>unlocalizedDimensionContent.locale IS NULL AND unlocalizedDimensionContent.stage = 'draft'</condition>
<condition>unlocalizedDimensionContent.locale IS NULL AND unlocalizedDimensionContent.stage = 'draft' AND unlocalizedDimensionContent.version = '0'</condition>
</join>
</joins>

Expand All @@ -25,7 +25,7 @@
<entity-name>ghostDimensionContent</entity-name>
<field-name>Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example.dimensionContents</field-name>
<method>LEFT</method>
<condition>dimensionContent.locale IS NULL AND ghostDimensionContent.locale = unlocalizedDimensionContent.ghostLocale AND ghostDimensionContent.stage = 'draft'</condition>
<condition>dimensionContent.locale IS NULL AND ghostDimensionContent.locale = unlocalizedDimensionContent.ghostLocale AND ghostDimensionContent.stage = 'draft' AND ghostDimensionContent.version = '0'</condition>
</join>
</joins>

Expand Down Expand Up @@ -66,5 +66,12 @@

<joins ref="ghostDimensionContent"/>
</property>

<property name="version" translation="sulu_admin.version" visibility="never">
<field-name>version</field-name>
<entity-name>dimensionContent</entity-name>

<joins ref="dimensionContent"/>
</property>
</properties>
</list>
Loading
Loading