Skip to content
Merged
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [0.1.5] - 2025-04-19
### Added
- UUID
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ A set of Doctrine Entity features to automate repetitive entity tasks and ease d
- [x] [Timestamp](docs/timestamp.md)
- [x] [Slug](docs/slug.md)
- [x] [Author (Blame)](docs/author.md)
- [ ] Translation
- [x] [UUID](docs/uuid.md)
- [x] [IP Tagged](docs/ip_tagged.md)
- [ ] Logging
- [ ] Tree
- [ ] UUID
- [ ] Translation

Installation
============
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"doctrine/orm": "^2.10 || ^3.0",
"symfony/property-access": "^6.4 || ^7.0",
"symfony/string": "^6.4 || ^7.0",
"symfony/security-bundle": "^6.4 || ^7.0"
"symfony/security-bundle": "^6.4 || ^7.0",
"symfony/doctrine-bridge": "^7.2",
"symfony/uid": "^6.4 || ^7.0"
},
"require-dev": {
"phpstan/phpstan": "*",
Expand Down
12 changes: 12 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Rami\EntityKitBundle\EventListener\Authored\AuthoredListener;
use Rami\EntityKitBundle\EventListener\IpTagged\IpTaggedListener;
use Rami\EntityKitBundle\EventListener\LoggedEntity\LoggedEntityListener;
use Rami\EntityKitBundle\EventListener\Slugged\SluggedListener;
use Rami\EntityKitBundle\EventListener\TimeStamped\TimeStampedListener;
use Rami\EntityKitBundle\EventListener\Uuid\UuidListener;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down Expand Up @@ -40,4 +43,13 @@
->args([new Reference(RequestStack::class)])
->tag('doctrine.event_listener', ['event' => 'prePersist'])
->tag('doctrine.event_listener', ['event' => 'preUpdate']);

$services
->set(LoggedEntityListener::class)
->args([new Reference(LoggerInterface::class)])
->tag('doctrine.event_listener', ['event' => 'postPersist']);

$services
->set(UuidListener::class)
->tag('doctrine.event_listener', ['event' => 'prePersist']);
};
12 changes: 12 additions & 0 deletions docs/uuid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
To generate a uuid v4 on your entity

```php
use Rami\EntityKitBundle\Common\Interfaces\Uuid\UuidInterface;
use Rami\EntityKitBundle\Entity\Traits\UuidTrait;

class Blog implements UuidInterface
{
use UuidTrait;
}
```
This adds a `$uuid` variable with getter and setter and generates a uuid v4 for the entity
12 changes: 12 additions & 0 deletions src/Common/Attributes/LoggedEntity.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
<?php
/*
* Copyright (c) 2025.
*
* This file is part of the Entity Kit Bundle project
* @author Abdellah Ramadan <ramadanabdel24@gmail.com>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rami\EntityKitBundle\Common\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class LoggedEntity
{

Expand Down
2 changes: 1 addition & 1 deletion src/Common/Attributes/LoggedEntityProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
class LoggedProperty
class LoggedEntityProperty
{

}
21 changes: 21 additions & 0 deletions src/Common/Interfaces/Uuid/UuidInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/*
* Copyright (c) 2025.
*
* This file is part of the Entity Kit Bundle project
* @author Abdellah Ramadan <ramadanabdel24@gmail.com>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rami\EntityKitBundle\Common\Interfaces\Uuid;

use Symfony\Component\Uid\Uuid;

interface UuidInterface
{
public function getUuid(): ?Uuid;

public function setUuid(Uuid $uuid): static;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/*
* Copyright (c) 2025.
*
* This file is part of the Entity Kit Bundle project
* @author Abdellah Ramadan <ramadanabdel24@gmail.com>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rami\EntityKitBundle\DependencyInjection\CompilerPasses;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Types\DateTimeImmutableType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

class DoctrineTypesCompilerPass implements CompilerPassInterface
{
/**
* @throws Exception
*/
public function process(ContainerBuilder $container): void
{
if (!Type::hasType(Types::DATETIME_IMMUTABLE)) {
Type::addType(Types::DATETIME_IMMUTABLE, DateTimeImmutableType::class);
}

}
}
2 changes: 0 additions & 2 deletions src/Entity/LoggedEntity/LoggedEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ORM\Table(name: 'logged_entities')]
class LoggedEntity
{
#[ORM\Id]
Expand Down
33 changes: 33 additions & 0 deletions src/Entity/Traits/UuidTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
* Copyright (c) 2025.
*
* This file is part of the Entity Kit Bundle project
* @author Abdellah Ramadan <ramadanabdel24@gmail.com>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rami\EntityKitBundle\Entity\Traits;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Uid\Uuid;

trait UuidTrait
{
#[ORM\Column(type: UuidType::NAME)]
protected ?Uuid $uuid = null;

public function getUuid(): ?Uuid
{
return $this->uuid;
}

public function setUuid(Uuid $uuid): static
{
$this->uuid = $uuid;
return $this;
}
}
25 changes: 9 additions & 16 deletions src/EntityKitBundle.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?php
/*
* Copyright (c) 2025.
*
* This file is part of the Entity Kit Bundle project
* @author Abdellah Ramadan <ramadanabdel24@gmail.com>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rami\EntityKitBundle;

use Psr\Log\LoggerInterface;
use Rami\EntityKitBundle\DependencyInjection\Compiler\DoctrineEventSubscriberPass;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
use Symfony\Component\HttpKernel\Log\Logger;

class EntityKitBundle extends AbstractBundle
{
// public function getPath(): string
// {
// return __DIR__;
// }

public function getPath(): string
{
$reflected = new \ReflectionObject($this);
Expand All @@ -34,11 +34,4 @@ public function configure(DefinitionConfigurator $definition): void
{
$definition->import('../config/definition.php');
}

public function build(ContainerBuilder $container): void
{
parent::build($container);

//$container->addCompilerPass(new DoctrineEventSubscriberPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
}
}
9 changes: 4 additions & 5 deletions src/EventListener/LoggedEntity/LoggedEntityListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rami\EntityKitBundle\EventListener\Logged;
namespace Rami\EntityKitBundle\EventListener\LoggedEntity;

use Doctrine\ORM\Event\PostPersistEventArgs;
use Psr\Log\LoggerInterface;
use Rami\EntityKitBundle\Common\Attributes\LoggedEntity;
use Rami\EntityKitBundle\Common\Attributes\LoggedProperty;

final readonly class LoggedListener
final readonly class LoggedEntityListener
{
public function __construct(
private LoggerInterface $logger,
Expand All @@ -37,8 +36,8 @@ public function postPersist(PostPersistEventArgs $event): void

$changes = $event->getObjectManager()->getUnitOfWork()->getEntityChangeSet($entity);

foreach ($changes as $change => [$old, $new]) {

foreach ($changes as $field => [$old, $new]) {
$this->logger->notice(sprintf('%s of %s changed from %s to %s', $field, get_class($entity), $old, $new));
}
}
}
31 changes: 31 additions & 0 deletions src/EventListener/Uuid/UuidListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* Copyright (c) 2025.
*
* This file is part of the Entity Kit Bundle project
* @author Abdellah Ramadan <ramadanabdel24@gmail.com>
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code.
*/

namespace Rami\EntityKitBundle\EventListener\Uuid;

use Doctrine\ORM\Event\PrePersistEventArgs;
use Rami\EntityKitBundle\Common\Interfaces\Uuid\UuidInterface;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Uid\UuidV4;

class UuidListener
{
public function prePersist(PrePersistEventArgs $event): void
{
$entity = $event->getObject();

if (!$entity instanceof UuidInterface) {
return;
}

$entity->setUuid(Uuid::v4());
}
}