From 52bf0d299c6ec2d097bfc3802659309a316bb5e4 Mon Sep 17 00:00:00 2001 From: Abdellah Ramadan Date: Sat, 19 Apr 2025 14:49:53 +0100 Subject: [PATCH 1/2] Add UUID --- README.md | 4 +-- composer.json | 4 ++- config/services.php | 12 +++++++ docs/uuid.md | 12 +++++++ src/Common/Attributes/LoggedEntity.php | 12 +++++++ .../Attributes/LoggedEntityProperty.php | 2 +- src/Common/Interfaces/Uuid/UuidInterface.php | 21 +++++++++++ .../DoctrineTypesCompilerPass.php | 35 +++++++++++++++++++ src/Entity/LoggedEntity/LoggedEntity.php | 2 -- src/Entity/Traits/UuidTrait.php | 33 +++++++++++++++++ src/EntityKitBundle.php | 25 +++++-------- .../LoggedEntity/LoggedEntityListener.php | 9 +++-- src/EventListener/Uuid/UuidListener.php | 31 ++++++++++++++++ 13 files changed, 175 insertions(+), 27 deletions(-) create mode 100644 docs/uuid.md create mode 100644 src/Common/Interfaces/Uuid/UuidInterface.php create mode 100644 src/DependencyInjection/CompilerPasses/DoctrineTypesCompilerPass.php create mode 100644 src/Entity/Traits/UuidTrait.php create mode 100644 src/EventListener/Uuid/UuidListener.php diff --git a/README.md b/README.md index 811031c..5319924 100644 --- a/README.md +++ b/README.md @@ -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 ============ diff --git a/composer.json b/composer.json index 030495d..2480fbd 100644 --- a/composer.json +++ b/composer.json @@ -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": "*", diff --git a/config/services.php b/config/services.php index 16753d7..ed9b9a2 100644 --- a/config/services.php +++ b/config/services.php @@ -1,10 +1,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']); }; \ No newline at end of file diff --git a/docs/uuid.md b/docs/uuid.md new file mode 100644 index 0000000..bd06d00 --- /dev/null +++ b/docs/uuid.md @@ -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 \ No newline at end of file diff --git a/src/Common/Attributes/LoggedEntity.php b/src/Common/Attributes/LoggedEntity.php index ac26db6..b0b03d6 100644 --- a/src/Common/Attributes/LoggedEntity.php +++ b/src/Common/Attributes/LoggedEntity.php @@ -1,7 +1,19 @@ + * + * 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 { diff --git a/src/Common/Attributes/LoggedEntityProperty.php b/src/Common/Attributes/LoggedEntityProperty.php index 07f6d5b..a00dfda 100644 --- a/src/Common/Attributes/LoggedEntityProperty.php +++ b/src/Common/Attributes/LoggedEntityProperty.php @@ -14,7 +14,7 @@ use Attribute; #[Attribute(Attribute::TARGET_PROPERTY)] -class LoggedProperty +class LoggedEntityProperty { } \ No newline at end of file diff --git a/src/Common/Interfaces/Uuid/UuidInterface.php b/src/Common/Interfaces/Uuid/UuidInterface.php new file mode 100644 index 0000000..a1a35c2 --- /dev/null +++ b/src/Common/Interfaces/Uuid/UuidInterface.php @@ -0,0 +1,21 @@ + + * + * 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; +} \ No newline at end of file diff --git a/src/DependencyInjection/CompilerPasses/DoctrineTypesCompilerPass.php b/src/DependencyInjection/CompilerPasses/DoctrineTypesCompilerPass.php new file mode 100644 index 0000000..1cb2dac --- /dev/null +++ b/src/DependencyInjection/CompilerPasses/DoctrineTypesCompilerPass.php @@ -0,0 +1,35 @@ + + * + * 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); + } + + } +} diff --git a/src/Entity/LoggedEntity/LoggedEntity.php b/src/Entity/LoggedEntity/LoggedEntity.php index 3f02ede..4aabe53 100644 --- a/src/Entity/LoggedEntity/LoggedEntity.php +++ b/src/Entity/LoggedEntity/LoggedEntity.php @@ -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] diff --git a/src/Entity/Traits/UuidTrait.php b/src/Entity/Traits/UuidTrait.php new file mode 100644 index 0000000..0bd9e59 --- /dev/null +++ b/src/Entity/Traits/UuidTrait.php @@ -0,0 +1,33 @@ + + * + * 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; + } +} \ No newline at end of file diff --git a/src/EntityKitBundle.php b/src/EntityKitBundle.php index 0ff6422..4831494 100644 --- a/src/EntityKitBundle.php +++ b/src/EntityKitBundle.php @@ -1,23 +1,23 @@ + * + * 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); @@ -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); - } } \ No newline at end of file diff --git a/src/EventListener/LoggedEntity/LoggedEntityListener.php b/src/EventListener/LoggedEntity/LoggedEntityListener.php index 043d2ed..e55aa2c 100644 --- a/src/EventListener/LoggedEntity/LoggedEntityListener.php +++ b/src/EventListener/LoggedEntity/LoggedEntityListener.php @@ -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, @@ -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)); } } } \ No newline at end of file diff --git a/src/EventListener/Uuid/UuidListener.php b/src/EventListener/Uuid/UuidListener.php new file mode 100644 index 0000000..2692356 --- /dev/null +++ b/src/EventListener/Uuid/UuidListener.php @@ -0,0 +1,31 @@ + + * + * 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()); + } +} \ No newline at end of file From ce4079ff62d03cff893a27ee4ccab8069e321163 Mon Sep 17 00:00:00 2001 From: Abdellah Ramadan Date: Sat, 19 Apr 2025 14:52:59 +0100 Subject: [PATCH 2/2] add changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2b55fdc --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## [0.1.5] - 2025-04-19 +### Added +- UUID \ No newline at end of file