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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.gitignore export-ignore
/test export-ignore
/phpunit.xml.dist export-ignore
/phpcs.xml.dist export-ignore
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
php-versions: ['7.3', '7.4', '8.0']
php-versions: ['8.3', '8.4']
name: PHP ${{ matrix.php-versions }}
steps:
- uses: actions/checkout@v2
Expand Down
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
"license": "MIT",
"minimum-stability": "stable",
"require": {
"php": "^7.3|^8.0",
"php": "^8.3",
"doctrine/annotations": "1.*,>=1.2.0",
"doctrine/common": "^2.13.3|^3.0.0",
"doctrine/orm": "^2.7.5",
"psr/log": "^1.0.0|^2.0.0|^3.0.0"
"doctrine/common": "^3.4.4",
"doctrine/orm": "^2.19.7",
"doctrine/persistence": "^2.5.7|^3.4.0",
"psr/log": "^1.0.0|^2.0.0|^3.0.0",
"symfony/cache": "^6.4|^7.4"
},
"require-dev": {
"hostnet/database-test-lib": "^2.0.2",
"hostnet/phpcs-tool": "^9.1.0",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.4",
"symfony/cache": "^5.3"
"phpspec/prophecy-phpunit": "^2.5.0",
"phpunit/phpunit": "^9.4"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 5 additions & 1 deletion src/Annotation/Tracked.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

namespace Hostnet\Component\EntityTracker\Annotation;

use Hostnet\Component\EntityTracker\Attributes\Tracked as TrackedAttribute;

/**
* @Annotation
* @Target({"CLASS"})
*
* @deprecated Please use the Attribute instead
*/
class Tracked
class Tracked extends TrackedAttribute
{
}
12 changes: 12 additions & 0 deletions src/Attributes/Tracked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* @copyright 2026-present Hostnet B.V.
*/
declare(strict_types=1);

namespace Hostnet\Component\EntityTracker\Attributes;

#[\Attribute(\Attribute::TARGET_CLASS)]
class Tracked
{
}
93 changes: 45 additions & 48 deletions src/Listener/EntityChangedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,78 +9,84 @@
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Proxy\Proxy;
use Doctrine\Persistence\ObjectManager;
use Hostnet\Component\EntityTracker\Attributes\Tracked;
use Hostnet\Component\EntityTracker\Event\EntityChangedEvent;
use Hostnet\Component\EntityTracker\Events;
use Hostnet\Component\EntityTracker\Provider\EntityAnnotationMetadataProvider;
use Hostnet\Component\EntityTracker\Provider\EntityMetadataProvider;
use Hostnet\Component\EntityTracker\Provider\EntityMutationMetadataProvider;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

/**
* Listener for entities that use the Tracked Annotation.
* Listener for entities that use the Tracked Annotation or attribute.
*
* This listener will fire an "Events::ENTITY_CHANGED" event
* per entity that is changed.
*/
class EntityChangedListener
{
/**
* @var EntityAnnotationMetadataProvider
*/
private $meta_annotation_provider;
public function __construct(
private EntityMetadataProvider $meta_annotation_provider,
private EntityMutationMetadataProvider $meta_mutation_provider,
private ?LoggerInterface $logger = null,
private CacheItemPoolInterface $is_tracked_cache = new ArrayAdapter()
) {
$this->logger = $logger ? : new NullLogger();
}

/**
* @var EntityMutationMetadataProvider
*/
private $meta_mutation_provider;
private function isTracked(ObjectManager $em, mixed $entity): bool
{
$cache_key = base64_encode('TRACKED-' . get_class($entity));
$cached_item = $this->is_tracked_cache->getItem($cache_key);

/**
* @var string[]
*/
private $annotations = [];
if ($cached_item->isHit()) {
return $cached_item->get();
}

/**
* @var LoggerInterface
*/
private $logger;
if (null !== $this->meta_annotation_provider->getAttributeFromEntity(Tracked::class, $em, $entity)) {
return $this->save($cached_item, true);
}

/**
* @param EntityAnnotationMetadataProvider $meta_annotation_provider
* @param EntityMutationMetadataProvider $meta_mutation_provider
* @param LoggerInterface $logger
*/
public function __construct(
EntityAnnotationMetadataProvider $meta_annotation_provider,
EntityMutationMetadataProvider $meta_mutation_provider,
LoggerInterface $logger = null
) {
$this->meta_annotation_provider = $meta_annotation_provider;
$this->meta_mutation_provider = $meta_mutation_provider;
$this->logger = $logger ? : new NullLogger();
if ($this->meta_annotation_provider->isTracked($em, $entity)) {
return $this->save($cached_item, true);
}

return $this->save($cached_item, false);
}

private function save(CacheItemInterface $item, bool $value): bool
{
$item->set($value);
$this->is_tracked_cache->save($item);

return $value;
}

/**
* Pre Flush event callback
*
* Checks if the entity contains an @Tracked (or derived)
* annotation. If so, it will attempt to calculate changes
* annotation or attribute. If so, it will attempt to calculate changes
* made and dispatch 'Events::ENTITY_CHANGED' with the current
* and original entity states. Note that the original entity
* is not managed.
*
* @param PreFlushEventArgs $event
*/
public function preFlush(PreFlushEventArgs $event)
public function preFlush(PreFlushEventArgs $event): void
{
$em = $event->getEntityManager();
$em = $event->getObjectManager();
$changes = $this->meta_mutation_provider->getFullChangeSet($em);

foreach ($changes as $updates) {
if (0 === count($updates)) {
continue;
}

if (false === $this->meta_annotation_provider->isTracked($em, current($updates))) {
$entity = current($updates);
if (!$this->isTracked($em, $entity)) {
continue;
}

Expand Down Expand Up @@ -111,18 +117,9 @@ public function preFlush(PreFlushEventArgs $event)
}

/**
* Pre Persist event callback
*
* Checks if the entity contains an @Tracked (or derived)
* annotation. If so, it will dispatch 'Events::ENTITY_CHANGED'
* with the new entity states.
*
* @deprecated Will be removed. Here so the bundle does not break.
*
* @param LifecycleEventArgs $event
* @deprecated Will be removed when removing doctrine/annotations, will break entity-tracker-bundle otherwise.
*/
public function prePersist(LifecycleEventArgs $event)
public function prePersist(LifecycleEventArgs $event): void
{
// do nothing, will be removed later on.
}
}
56 changes: 4 additions & 52 deletions src/Provider/EntityAnnotationMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,9 @@

namespace Hostnet\Component\EntityTracker\Provider;

use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\EntityManagerInterface;
use Hostnet\Component\EntityTracker\Annotation\Tracked;

class EntityAnnotationMetadataProvider
/**
* @deprecated Will be removed on the next BC break, when removing the doctrine/annotations dependency.
*/
class EntityAnnotationMetadataProvider extends EntityMetadataProvider
{
/**
* @var Reader
*/
private $reader;

/**
* @param Reader $reader
*/
public function __construct(Reader $reader)
{
$this->reader = $reader;
}

/**
* Get the annotation from a class or null if it doesn't exists.
*
* @param EntityManagerInterface $em
* @param mixed $entity
* @return bool
*/
public function isTracked(EntityManagerInterface $em, $entity)
{
$class = get_class($entity);
$annotations = $this->reader->getClassAnnotations($em->getClassMetadata($class)->getReflectionClass());

foreach ($annotations as $annotation) {
if ($annotation instanceof Tracked) {
return true;
}
}

return false;
}

/**
* @param EntityManagerInterface $em
* @param mixed $entity
* @param string $annotation
* @return mixed
*/
public function getAnnotationFromEntity(EntityManagerInterface $em, $entity, $annotation)
{
return $this->reader->getClassAnnotation(
$em->getClassMetadata(get_class($entity))->getReflectionClass(),
$annotation
);
}
}
83 changes: 83 additions & 0 deletions src/Provider/EntityMetadataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* @copyright 2014-present Hostnet B.V.
*/
declare(strict_types=1);

namespace Hostnet\Component\EntityTracker\Provider;

use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Proxy;
use Hostnet\Component\EntityTracker\Annotation\Tracked as TrackedAnnotation;
use Hostnet\Component\EntityTracker\Attributes\Tracked as Tracked;

class EntityMetadataProvider
{
/**
* @var Reader
*/
private $reader;

/**
* @param Reader $reader
*/
public function __construct(Reader $reader)
{
$this->reader = $reader;
}

/**
* @param mixed $entity
*
* @deprecated Please use the Tracked attribute instead
*/
public function isTracked(EntityManagerInterface $em, $entity): bool
{
$class = get_class($entity);
$annotations = $this->reader->getClassAnnotations($em->getClassMetadata($class)->getReflectionClass());

foreach ($annotations as $annotation) {
if ($annotation instanceof TrackedAnnotation) {
return true;
}
}

return false;
}

/**
* @param EntityManagerInterface $em
* @param mixed $entity
* @param string $annotation
*
* @deprecated Please use the Tracked attribute instead
*/
public function getAnnotationFromEntity(EntityManagerInterface $em, $entity, $annotation): mixed
{
return $this->reader->getClassAnnotation(
$em->getClassMetadata(get_class($entity))->getReflectionClass(),
$annotation
);
}

public function getAttributeFromEntity(string $attribute_class, EntityManagerInterface $em, mixed $entity): ?Tracked
{
$class = get_class($entity);
if ($entity instanceof Proxy) {
$class = $em->getClassMetadata($class)->getName();
}

$reflection = new \ReflectionClass($class);
$attributes = $reflection->getAttributes($attribute_class, \ReflectionAttribute::IS_INSTANCEOF);

if (empty($attributes)) {
return null;
}

/** @var Tracked $attribute */
$attribute = $attributes[0]->newInstance();

return $attribute;
}
}
Loading
Loading