Implement snip content names

This commit is contained in:
Tim
2025-05-13 13:47:33 +02:00
parent 62136a0ca0
commit 5ae5db985b
8 changed files with 74 additions and 10 deletions

View File

@ -104,7 +104,11 @@ class SnipController extends AbstractController
]);
}
$this->repository->save($snip);
$contentService->update($snip, $form->get('content')->getData());
$contentService->update(
$snip,
$form->get('content')->getData(),
$form->get('contentName')->getData()
);
$this->addFlash('success', sprintf('Snip "%s" saved', $snip));

View File

@ -35,6 +35,9 @@ class SnipContent
#[ORM\Column(nullable: true)]
private ?array $diff = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $name = null;
public function __construct()
{
$this->children = new ArrayCollection();
@ -122,4 +125,16 @@ class SnipContent
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): static
{
$this->name = $name;
return $this;
}
}

View File

@ -6,7 +6,9 @@ use App\Entity\Snip;
use App\Service\SnipParser\ParserFactory;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
@ -31,6 +33,10 @@ class SnipType extends AbstractType
->add('tags', TagsType::class)
->add('public', SwitchType::class)
->add('visible', SwitchType::class)
->add('contentName', TextType::class, [
'label' => 'Change description (optional)',
'mapped' => false,
])
;
}

View File

@ -13,18 +13,19 @@ readonly class SnipContentService
private EntityManagerInterface $em,
) {}
public function update(Snip $snip, string $snipContents): void
public function update(Snip $snip, string $contents, ?string $contentName): void
{
$parentContent = $snip->getActiveVersion();
if (self::rebuildText($parentContent) === $snipContents) {
if (self::rebuildText($parentContent) === $contents) {
return;
}
// Create new snipContent entity with previous one as parent
$content = new SnipContent();
$content
->setText($snipContents)
->setText($contents)
->setSnip($snip)
->setName($contentName)
;
if ($parentContent !== null) {
$content->setParent($parentContent);