Skip to content
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
56 changes: 56 additions & 0 deletions src/DependencyInjection/Compiler/ResolveTargetEntitiesPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusCMSPlugin\DependencyInjection\Compiler;

use Setono\SyliusCMSPlugin\Model\SlideInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Webmozart\Assert\Assert;

/**
* This will make sure Doctrine can resolve child interfaces of the SlideInterface to concrete entities
*/
final class ResolveTargetEntitiesPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->hasParameter('sylius.resources') || !$container->hasDefinition('doctrine.orm.listeners.resolve_target_entity')) {
return;
}

/** @var array<string, array{classes: array{model: class-string}}> $resources */
$resources = $container->getParameter('sylius.resources');

/** @var array<class-string, class-string> $targets */
$targets = [];

foreach ($resources as $resource) {
['model' => $model] = $resource['classes'];

if (!is_a($model, SlideInterface::class, true)) {
continue;
}

$interfaces = class_implements($model);
if (false !== ($parent = get_parent_class($model))) {
$interfaces = array_diff($interfaces, class_implements($parent));
}

$interfaces = array_values(array_filter($interfaces, static function (string $interface): bool {
return is_a($interface, SlideInterface::class, true);
}));

Assert::count($interfaces, 1);

$targets[$interfaces[0]] = $model;
}

$resolver = $container->getDefinition('doctrine.orm.listeners.resolve_target_entity');

foreach ($targets as $interface => $target) {
$resolver->addMethodCall('addResolveTargetEntity', [$interface, $target, []]);
}
}
}
197 changes: 57 additions & 140 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,31 @@
use Setono\SyliusCMSPlugin\Form\Type\AssetType;
use Setono\SyliusCMSPlugin\Form\Type\BlockTranslationType;
use Setono\SyliusCMSPlugin\Form\Type\BlockType;
use Setono\SyliusCMSPlugin\Form\Type\CarouselBlockType;
use Setono\SyliusCMSPlugin\Form\Type\CarouselType;
use Setono\SyliusCMSPlugin\Form\Type\PageTranslationType;
use Setono\SyliusCMSPlugin\Form\Type\PageType;
use Setono\SyliusCMSPlugin\Form\Type\SlideType;
use Setono\SyliusCMSPlugin\Form\Type\TemplateType;
use Setono\SyliusCMSPlugin\Model\Asset;
use Setono\SyliusCMSPlugin\Model\Block;
use Setono\SyliusCMSPlugin\Model\BlockTranslation;
use Setono\SyliusCMSPlugin\Model\Carousel;
use Setono\SyliusCMSPlugin\Model\CarouselBlock;
use Setono\SyliusCMSPlugin\Model\EmbeddedVideoSlide;
use Setono\SyliusCMSPlugin\Model\Page;
use Setono\SyliusCMSPlugin\Model\PageTranslation;
use Setono\SyliusCMSPlugin\Model\Slide;
use Setono\SyliusCMSPlugin\Model\Template;
use Setono\SyliusCMSPlugin\Repository\AssetRepository;
use Setono\SyliusCMSPlugin\Repository\BlockRepository;
use Setono\SyliusCMSPlugin\Repository\CarouselRepository;
use Setono\SyliusCMSPlugin\Repository\PageRepository;
use Setono\SyliusCMSPlugin\Repository\TemplateRepository;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
use Sylius\Bundle\ResourceBundle\Form\Type\DefaultResourceType;
use Sylius\Component\Resource\Factory\Factory;
use Sylius\Component\Resource\Factory\TranslatableFactory;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -39,9 +42,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$treeBuilder = new TreeBuilder('setono_sylius_cms');
$rootNode = $treeBuilder->getRootNode();

/**
* @psalm-suppress MixedMethodCall,PossiblyUndefinedMethod,PossiblyNullReference,UndefinedInterfaceMethod
*/
/** @psalm-suppress MixedMethodCall,PossiblyUndefinedMethod,PossiblyNullReference,UndefinedInterfaceMethod */
$rootNode
->addDefaultsIfNotSet()
->children()
Expand All @@ -65,145 +66,61 @@ public function getConfigTreeBuilder(): TreeBuilder
->scalarNode('description')->end()
;

$this->addResourcesSection($rootNode);
$resources = $rootNode->children()->arrayNode('resources')->addDefaultsIfNotSet()->children();

self::addResource($resources, 'asset', Asset::class, AssetRepository::class, AssetType::class);
self::addResource($resources, 'block', Block::class, BlockRepository::class, BlockType::class, BlockTranslation::class, BlockTranslationType::class);
self::addResource($resources, 'carousel', Carousel::class, CarouselRepository::class, CarouselType::class);
self::addResource($resources, 'embedded_video_slide', EmbeddedVideoSlide::class);
self::addResource($resources, 'page', Page::class, PageRepository::class, PageType::class, PageTranslation::class, PageTranslationType::class);
self::addResource($resources, 'slide', Slide::class, null, SlideType::class);
self::addResource($resources, 'template', Template::class, TemplateRepository::class, TemplateType::class);

return $treeBuilder;
}

private function addResourcesSection(ArrayNodeDefinition $node): void
{
/**
* @psalm-suppress MixedMethodCall,PossiblyUndefinedMethod,PossiblyNullReference,UndefinedInterfaceMethod
*/
$node
->children()
->arrayNode('resources')
->addDefaultsIfNotSet()
->children()
->arrayNode('asset')
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(Asset::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(AssetRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(AssetType::class)->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
->end()
->end()
->end()
->end()
->arrayNode('block')
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(Block::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(BlockRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(BlockType::class)->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
->end()
->end()
->arrayNode('translation')
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(BlockTranslation::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(BlockTranslationType::class)->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(TranslatableFactory::class)->end()
->end()
->end()
->end()
->end()
->end()
->end()
->arrayNode('carousel')
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(Carousel::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(CarouselRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(CarouselType::class)->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
->end()
->end()
->end()
->end()
->arrayNode('carousel_block')
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(CarouselBlock::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(CarouselBlockType::class)->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
->end()
->end()
->end()
->end()
->arrayNode('page')
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(Page::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(PageRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(PageType::class)->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
->end()
->end()
->arrayNode('translation')
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(PageTranslation::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(PageTranslationType::class)->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(TranslatableFactory::class)->end()
->end()
->end()
->end()
->end()
->end()
private static function addResource(
NodeBuilder $nodeBuilder,
string $name,
string $class,
string $repository = null,
string $form = null,
string $translationClass = null,
string $translationForm = null,
): void {
/** @psalm-suppress MixedMethodCall,PossiblyUndefinedMethod,PossiblyNullReference,UndefinedInterfaceMethod,MixedAssignment */
$node = $nodeBuilder
->arrayNode($name)
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue($class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue($repository ?? EntityRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue($form ?? DefaultResourceType::class)->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
->end()
->arrayNode('template')
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(Template::class)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->defaultValue(TemplateRepository::class)->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue(TemplateType::class)->end()
->scalarNode('factory')->defaultValue(Factory::class)->end()
->end()
;

if (null !== $translationClass) {
/** @psalm-suppress MixedMethodCall,PossiblyUndefinedMethod,PossiblyNullReference,UndefinedInterfaceMethod */
$node->arrayNode('translation')
->addDefaultsIfNotSet()
->children()
->variableNode('options')->end()
->arrayNode('classes')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue($translationClass)->cannotBeEmpty()->end()
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
->scalarNode('repository')->cannotBeEmpty()->end()
->scalarNode('form')->defaultValue($translationForm ?? DefaultResourceType::class)->cannotBeEmpty()->end()
->scalarNode('factory')->defaultValue(TranslatableFactory::class)->end()
;
}
}
}
58 changes: 58 additions & 0 deletions src/EventListener/Doctrine/SlideDiscriminatorMapListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusCMSPlugin\EventListener\Doctrine;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Setono\SyliusCMSPlugin\Model\Slide;
use Setono\SyliusCMSPlugin\Model\SlideInterface;
use function Symfony\Component\String\u;

final class SlideDiscriminatorMapListener
{
public function __construct(
/** @var array<string, array{classes: array{model: class-string}}> $resources */
private readonly array $resources,
) {
}

public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
{
$metadata = $eventArgs->getClassMetadata();
if ($metadata->getName() !== Slide::class) {
return;
}

$metadata->discriminatorMap = $this->getDiscriminatorMap();
}

/**
* @return array<string, class-string>
*/
private function getDiscriminatorMap(): array
{
$children = [];

foreach ($this->resources as $resource) {
['model' => $model] = $resource['classes'];

// todo this is a naïve approach. We need to find child entities of either Slide or SlideInterface
if (Slide::class === $model || !is_a($model, SlideInterface::class, true)) {
continue;
}

$children[self::getDiscriminatorKey($model)] = $model;
}

return $children;
}

/**
* @param class-string $class
*/
private static function getDiscriminatorKey(string $class): string
{
return u((new \ReflectionClass($class))->getShortName())->snake()->toString();
}
}
1 change: 0 additions & 1 deletion src/Fixture/CarouselFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ protected function configureResourceNode(ArrayNodeDefinition $resourceNode): voi
$child = $resourceNode->children();
$child->scalarNode('code')->cannotBeEmpty();
$child->variableNode('configuration')->cannotBeEmpty()->defaultValue([]);
$child->variableNode('carouselBlocks')->cannotBeEmpty()->defaultValue([]);

$this->configureInternalDescriptionResourceNode($resourceNode);
}
Expand Down
Loading