Skip to content

Commit

Permalink
Merge pull request #70 from softspring/BUNDLES-171-cms-route-groups-w…
Browse files Browse the repository at this point in the history
…ith-parents

BUNDLES-171 - add route groups
  • Loading branch information
javihgil committed May 19, 2023
2 parents 67ea7d8 + 1cdd582 commit 31d8083
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 20 deletions.
1 change: 1 addition & 0 deletions cms/contents/page/translations/sfs_cms_contents.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ admin_page:
label: "Routes"
id.label: "Route id"
site.label: "Site"
parent.label: "Parent route"
paths:
label: "URLs"
path.label: "URL segment"
Expand Down
1 change: 1 addition & 0 deletions cms/contents/page/translations/sfs_cms_contents.es.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ admin_page:
label: "Rutas"
id.label: "Identificador"
site.label: "Site"
parent.label: "Ruta padre"
paths:
label: "URLs"
path.label: "Segmento de URL"
Expand Down
5 changes: 5 additions & 0 deletions config/doctrine-mapping/entities/Route.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
</cascade>
</one-to-many>

<one-to-many field="children" target-entity="Softspring\CmsBundle\Model\RouteInterface" mapped-by="parent" orphan-removal="true">
<cascade>
<cascade-persist/>
</cascade>
</one-to-many>
</entity>

</doctrine-mapping>
4 changes: 4 additions & 0 deletions config/doctrine-mapping/model/Route.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<many-to-one field="content" target-entity="Softspring\CmsBundle\Model\ContentInterface" inversed-by="routes">
<join-column name="content_id" referenced-column-name="id" on-delete="CASCADE" />
</many-to-one>

<many-to-one field="parent" target-entity="Softspring\CmsBundle\Model\RouteInterface" inversed-by="children">
<join-column name="parent_id" referenced-column-name="id" on-delete="RESTRICT" />
</many-to-one>
</mapped-superclass>

</doctrine-mapping>
6 changes: 6 additions & 0 deletions config/doctrine-mapping/model/RoutePath.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
</options>
</field>

<field name="compiledPath" column="compiled_path" nullable="true" length="255">
<options>
<option name="fixed">true</option>
</options>
</field>

<field name="cacheTtl" column="cache_ttl" type="integer" nullable="true">
<options>
<option name="unsigned">true</option>
Expand Down
4 changes: 2 additions & 2 deletions config/validation/RoutePathInterface.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Softspring\CmsBundle\Model\RoutePathInterface:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: ['path', 'locale', 'site']
ignoreNull: false
fields: ['compiledPath', 'locale', 'site']
ignoreNull: true
5 changes: 5 additions & 0 deletions src/Data/DataExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function exportContent(ContentInterface $content, ?ContentVersionInterfac
$filePath = YamlContent::save($transformer->export($content, $files, $contentVersion, $contentType), $file);

foreach ($content->getRoutes() as $route) {
$parentRoute = $route->getParent();
while ($parentRoute) {
self::exportRoute($parentRoute, $path);
$parentRoute = $parentRoute->getParent();
}
self::exportRoute($route, $path);
}

Expand Down
16 changes: 14 additions & 2 deletions src/Data/DataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Softspring\CmsBundle\Data\EntityTransformer\EntityTransformerInterface;
use Softspring\CmsBundle\Data\Exception\DataTransformerNotFoundException;
use Softspring\CmsBundle\Model\RouteInterface;

class DataImporter extends AbstractDataImportExport
{
Expand Down Expand Up @@ -42,11 +43,22 @@ public function import(array $contents, array $options = []): void
$this->em->flush();
unset($contents['media']);

// import parent routes before to allow import children
foreach ($contents['routes'] ?? [] as $routeData) {
if (RouteInterface::TYPE_PARENT_ROUTE === $routeData['route']['type']) {
$parentRoute = $this->getDataTransformer('routes', $routeData)->import($routeData, $this->referenceRepository, $options);
$this->em->persist($parentRoute);
}
}
$this->em->flush();

// import rest of contents
foreach ($contents as $type => $elements) {
foreach ($elements as $data) {
$entity = $this->getDataTransformer($type, $data)->import($data, $this->referenceRepository, $options);
$this->em->persist($entity);
if ('routes' !== $type || RouteInterface::TYPE_PARENT_ROUTE !== $data['route']['type']) {
$entity = $this->getDataTransformer($type, $data)->import($data, $this->referenceRepository, $options);
$this->em->persist($entity);
}
}
}
$this->em->flush();
Expand Down
14 changes: 12 additions & 2 deletions src/Data/EntityTransformer/RouteEntityTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function export(object $element, &$files = []): array
'id' => $route->getId(),
'site' => $route->getSite(),
'type' => $route->getType(),
'parent' => $route->getParent()?->getId(),
'symfony_route' => $route->getSymfonyRoute(),
'content' => null,
'redirect_type' => $route->getRedirectType(),
Expand Down Expand Up @@ -91,14 +92,16 @@ public function import(array $data, ReferencesRepository $referencesRepository,
$route->setSite($routeData['site']);
$route->setType(RouteInterface::TYPE_UNKNOWN);

if ($routeData['parent']) {
$route->setParent($referencesRepository->getReference("route___{$routeData['parent']}", true));
}

switch ($routeData['type']) {
case RouteInterface::TYPE_CONTENT:
$route->setType(RouteInterface::TYPE_CONTENT);
$route->setContent($referencesRepository->getReference("content___{$routeData['content']}", true));
break;

case RouteInterface::TYPE_REDIRECT_TO_URL:
$route->setType(RouteInterface::TYPE_REDIRECT_TO_URL);
$route->setRedirectUrl($routeData['redirect_url']);
$route->setRedirectType($routeData['redirect_type']);
break;
Expand All @@ -107,10 +110,17 @@ public function import(array $data, ReferencesRepository $referencesRepository,
$route->setSymfonyRoute($routeData['symfony_route']);
break;

case RouteInterface::TYPE_PARENT_ROUTE:
// nothing to do
break;

default:
throw new \Exception(sprintf('Route type %u not yet implemented', $routeData['type']));
}

// store valid type
$route->setType($routeData['type']);

foreach ($route->getPaths() as $existingPath) {
$route->removePath($existingPath);
}
Expand Down
19 changes: 18 additions & 1 deletion src/Form/Admin/Route/RouteForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ public function buildForm(FormBuilderInterface $builder, array $options)
],
]);

$builder->add('parent', EntityType::class, [
'class' => RouteInterface::class,
'required' => false,
'em' => $this->em,
'choice_filter' => function (?RouteInterface $parent) {
return !$parent || RouteInterface::TYPE_PARENT_ROUTE == $parent->getType();
},
'choice_label' => function (RouteInterface $parent) {
return $parent->getId();
},
'choice_attr' => function (RouteInterface $parent) {
return [
'data-site' => $parent->getSite(),
];
},
]);

if (!$options['content_relative']) {
$builder->add('site', SiteChoiceType::class);

Expand All @@ -69,7 +86,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
// 'NOT_FOUND' => RouteInterface::TYPE_NOT_FOUND,
'admin_routes.form.type.values.redirect_to_route' => RouteInterface::TYPE_REDIRECT_TO_ROUTE,
'admin_routes.form.type.values.redirect_to_url' => RouteInterface::TYPE_REDIRECT_TO_URL,
// 'PARENT_ROUTE' => RouteInterface::TYPE_PARENT_ROUTE,
'admin_routes.form.type.values.parent_route' => RouteInterface::TYPE_PARENT_ROUTE,
],
'choice_attr' => function ($value) {
return [
Expand Down
33 changes: 33 additions & 0 deletions src/Migrations/Version20230328091638.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Softspring\CmsBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20230328091638 extends AbstractMigration
{
public function getDescription(): string
{
return 'Support parent routes';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE cms_route ADD parent_id VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE cms_route ADD CONSTRAINT FK_2CB7BB55727ACA70 FOREIGN KEY (parent_id) REFERENCES cms_route (id) ON DELETE RESTRICT');
$this->addSql('CREATE INDEX IDX_2CB7BB55727ACA70 ON cms_route (parent_id)');
$this->addSql('ALTER TABLE cms_route_path ADD compiled_path CHAR(255) DEFAULT NULL');
$this->addSql('UPDATE cms_route_path SET compiled_path = path');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE cms_route DROP FOREIGN KEY FK_2CB7BB55727ACA70');
$this->addSql('DROP INDEX IDX_2CB7BB55727ACA70 ON cms_route');
$this->addSql('ALTER TABLE cms_route DROP parent_id');
$this->addSql('ALTER TABLE cms_route_path DROP compiled_path');
}
}
60 changes: 60 additions & 0 deletions src/Model/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ abstract class Route implements RouteInterface
*/
protected Collection $paths;

/**
* @psalm-var RouteInterface[]|Collection
*/
protected Collection $children;

protected ?RouteInterface $parent = null;
protected ?ContentInterface $content = null;
protected ?string $redirectUrl = null;
protected ?array $symfonyRoute = null;
Expand All @@ -24,6 +30,7 @@ abstract class Route implements RouteInterface
public function __construct()
{
$this->paths = new ArrayCollection();
$this->children = new ArrayCollection();
}

public function __toString()
Expand Down Expand Up @@ -65,6 +72,9 @@ public function setSite(?string $site): void
}
}

/**
* @psalm-return RoutePathInterface[]|Collection
*/
public function getPaths(): Collection
{
return $this->paths;
Expand Down Expand Up @@ -127,4 +137,54 @@ public function setRedirectType(?int $redirectType): void
{
$this->redirectType = $redirectType;
}

public function getParent(): ?RouteInterface
{
return $this->parent;
}

public function setParent(?RouteInterface $parent): void
{
$this->parent = $parent;
$this->compilePaths();
}

/**
* @return RouteInterface[]|Collection
*/
public function getChildren(): ?Collection
{
return $this->children;
}

public function addChild(RouteInterface $child): void
{
if (!$this->children->contains($child)) {
$this->children->add($child);
$child->setParent($this);
}
}

public function removeChild(RouteInterface $child): void
{
if ($this->children->contains($child)) {
$this->children->removeElement($child);
$child->setParent(null);
$child->compilePaths();
}
}

public function compilePaths(): void
{
foreach ($this->getPaths() as $path) {
$path->compilePath();
}
}

public function compileChildrenPaths(): void
{
foreach ($this->getChildren() as $child) {
$child->compilePaths();
}
}
}
17 changes: 17 additions & 0 deletions src/Model/RouteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ public function getSite(): ?string;

public function setSite(?string $site): void;

public function getParent(): ?RouteInterface;

public function setParent(?RouteInterface $parent): void;

/**
* @psalm-return RouteInterface[]|Collection|null
*/
public function getChildren(): ?Collection;

public function addChild(RouteInterface $child): void;

public function removeChild(RouteInterface $child): void;

/**
* @psalm-return RoutePathInterface[]|Collection
*/
Expand All @@ -48,4 +61,8 @@ public function setSymfonyRoute(?array $symfonyRoute): void;
public function getRedirectType(): ?int;

public function setRedirectType(?int $redirectType): void;

public function compilePaths(): void;

public function compileChildrenPaths(): void;
}
Loading

0 comments on commit 31d8083

Please sign in to comment.