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 @@ -15,7 +15,7 @@ jobs:
php-versions: ['8.3', '8.4']
name: PHP ${{ matrix.php-versions }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
Expand Down
92 changes: 42 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This component lets you automatically update entities by implementing `setUpdate

Requirements
------------
The blamable component requires a minimum of php 5.4 and runs on Doctrine2. For specific requirements, please check [composer.json](../master/composer.json).
The blamable component requires a minimum of php 8.3 and runs on Doctrine2. For specific requirements, please check [composer.json](../master/composer.json).

Installation
------------
Expand All @@ -32,7 +32,7 @@ Installing is pretty easy, this package is available on [packagist](https://pack

```javascript
"require" : {
"hostnet/entity-blamable-component" : "1.*"
"hostnet/entity-blamable-component" : "2.*"
}

```
Expand All @@ -45,14 +45,14 @@ Documentation
How does it work?
-----------------

It works by putting the `@Blamable` annotation and BlamableInterface on your Entity and registering the listener on the entityChanged event, assuming you have already configured the [Entity Tracker Component](https://github.com/hostnet/entity-tracker-component/#setup).
It works by putting the `#[Blamable]` attribute and BlamableInterface on your Entity and registering the listener on the entityChanged event, assuming you have already configured the [Entity Tracker Component](https://github.com/hostnet/entity-tracker-component/#setup).

For a usage example, follow the setup below.

Setup
-----

- You have to add `@Blamable` to your entity
- You have to add `#[Blamable]` to your entity
- You have to add the BlamableInterface to your entity
- You have to implement the BlamableProviderInterface on an object and pass it to the listener

Expand All @@ -72,27 +72,24 @@ use Acme\Bundle\AcmeBundle\Service\AcmeBlamableProvider;
use Hostnet\Component\EntityBlamable\Listener\BlamableListener;
use Hostnet\Component\EntityBlamable\Resolver\BlamableResolver;
use Hostnet\Component\EntityTracker\Listener\EntityChangedListener;
use Hostnet\Component\EntityTracker\Provider\EntityAnnotationMetadataProvider;
use Hostnet\Component\EntityTracker\Provider\EntityMetadataProvider;
use Hostnet\Component\EntityTracker\Provider\EntityMutationMetadataProvider;

/* @var $em \Doctrine\ORM\EntityManager */
$event_manager = $em->getEventManager();

// default doctrine annotation reader
$annotation_reader = new AnnotationReader();

// setup required providers
$mutation_metadata_provider = new EntityMutationMetadataProvider($annotation_reader);
$annotation_metadata_provider = new EntityAnnotationMetadataProvider($annotation_reader);
$mutation_metadata_provider = new EntityMutationMetadataProvider();
$metadata_provider = new EntityMetadataProvider();

// pre flush event listener that uses the @Tracked/@Blamable annotation
// pre flush event listener that uses the #[Tracked]/#[Blamable] attribute
$entity_changed_listener = new EntityChangedListener(
$mutation_metadata_provider,
$annotation_metadata_provider
$metadata_provider,
$mutation_metadata_provider
);

// the resolver is used to find the correct annotation
$blamable_resolver = new BlamableResolver($annotation_metadata_provider);
// the resolver is used to find the correct attribute
$blamable_resolver = new BlamableResolver($metadata_provider);

// the object that will provide the username and date time. This is an
// application specific implementation of the BlamableProviderInterface
Expand Down Expand Up @@ -120,70 +117,65 @@ use Hostnet\Component\EntityBlamable\Provider\BlamableProviderInterface;

class AcmeBlamableProvider implements BlamableProviderInterface
{
private $username;

public function __construct($username)
public function __construct(private string $username)
{
$this->username = $username;
}

public function getUpdatedBy()
public function getUpdatedBy(): string
{
return $this->username;
}
public function getChangedAt()

public function getChangedAt(): \DateTimeInterface
{
return new \DateTime();
return new \DateTimeImmutable();
}
}


```

#### Configuring the Entity
All we have to do now is put the `@Blamable` annotation and BlamableInterface on our Entity.
All we have to do now is put the `#[Blamable]` attribute and BlamableInterface on our Entity.

```php

use Doctrine\ORM\Mapping as ORM;
use Hostnet\Component\EntityBlamable\Blamable;
use Hostnet\Component\EntityBlamable\Attributes\Blamable;
use Hostnet\Component\EntityBlamable\BlamableInterface;

/**
* @ORM\Entity
* @Blamable
*/
#[ORM\Entity]
#[Blamable]
class MyEntity implements BlamableInterface
{
/**
* @ORM\...
*/
private $updated_by;

/**
* @ORM\...
*/
private $updated_at;

/**
* @ORM\...
*/
private $created_at;

public function setUpdatedBy($by)
#[ORM\Column(nullable: true)]
private ?string $updated_by = null;

#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?\DateTimeInterface $updated_at = null;

#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private ?\DateTimeInterface $created_at = null;

public function setUpdatedBy(string $by): static
{
$this->updated_by = $by;

return $this;
}

public function setUpdatedAt(\DateTime $at)
public function setUpdatedAt(\DateTimeInterface $at): static
{
$this->changed_at = $at;
$this->updated_at = $at;

return $this;
}
public function setCreatedAt(\DateTime $at)

public function setCreatedAt(\DateTimeInterface $at): static
{
$this->created_at = $at
$this->created_at = $at;

return $this;
}
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"require": {
"php": "^8.3",
"doctrine/orm": "^2.4.0",
"hostnet/entity-tracker-component": "^2.3.0",
"hostnet/entity-tracker-component": "^3.0.0",
"symfony/cache": "^6.4|^7.4"
},
"require-dev": {
Expand Down
19 changes: 0 additions & 19 deletions src/Blamable.php

This file was deleted.

17 changes: 3 additions & 14 deletions src/BlamableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,12 @@

/**
* Implement on Entities to trigger the BlamableEntityListener
*
* @TODO: add (return)typehints on next BC break, when removing doctrine/annotations
*/
interface BlamableInterface
{
/**
* @param string $by
*/
public function setUpdatedBy($by);
public function setUpdatedBy(string $by): static;

/**
* @param \DateTime $at
*/
public function setUpdatedAt(\DateTime $at);
public function setUpdatedAt(\DateTimeInterface $at): static;

/**
* @param \DateTime $at
*/
public function setCreatedAt(\DateTime $at);
public function setCreatedAt(\DateTimeInterface $at): static;
}
15 changes: 4 additions & 11 deletions src/Listener/BlamableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Hostnet\Component\EntityBlamable\Listener;

use Doctrine\Persistence\ObjectManager;
use Doctrine\ORM\EntityManagerInterface;
use Hostnet\Component\EntityBlamable\BlamableInterface;
use Hostnet\Component\EntityBlamable\Provider\BlamableProviderInterface;
use Hostnet\Component\EntityBlamable\Resolver\BlamableResolverInterface;
Expand All @@ -19,7 +19,7 @@
* Listens to "Events::entityChanged"
*
* Attempts to set updated at, created at, updated by and updated at fields
* in an entity using @Blamable and implementing the BlamableInterface
* in an entity using #[Blamable] and implementing the BlamableInterface
*/
class BlamableListener
{
Expand All @@ -30,9 +30,6 @@ public function __construct(
) {
}

/**
* @param EntityChangedEvent $event
*/
public function entityChanged(EntityChangedEvent $event): void
{
$entity = $event->getCurrentEntity();
Expand All @@ -53,9 +50,9 @@ public function entityChanged(EntityChangedEvent $event): void
}
}

private function isBlamable(ObjectManager $em, mixed $entity): bool
private function isBlamable(EntityManagerInterface $em, object $entity): bool
{
$cache_key = base64_encode('BLAMABLE-' . get_class($entity));
$cache_key = base64_encode('BLAMABLE-' . $entity::class);
$cached_item = $this->is_blamable_cache->getItem($cache_key);

if ($cached_item->isHit()) {
Expand All @@ -66,10 +63,6 @@ private function isBlamable(ObjectManager $em, mixed $entity): bool
return $this->save($cached_item, false);
}

if (null !== $this->resolver->getBlamableAnnotation($em, $entity)) {
return $this->save($cached_item, true);
}

if (null !== $this->resolver->getBlamableAttribute($em, $entity)) {
return $this->save($cached_item, true);
}
Expand Down
13 changes: 2 additions & 11 deletions src/Provider/BlamableProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@

namespace Hostnet\Component\EntityBlamable\Provider;

/**
* @TODO: add (return)typehints on next BC break, when removing doctrine/annotations
*/
interface BlamableProviderInterface
{
/**
* @return string
*/
public function getUpdatedBy();
public function getUpdatedBy(): string;

/**
* @return \DateTime
*/
public function getChangedAt();
public function getChangedAt(): \DateTimeInterface;
}
13 changes: 1 addition & 12 deletions src/Resolver/BlamableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use Doctrine\ORM\EntityManagerInterface;
use Hostnet\Component\EntityBlamable\Attributes\Blamable;
use Hostnet\Component\EntityBlamable\Blamable as BlamabeAnnotation;
use Hostnet\Component\EntityTracker\Provider\EntityMetadataProvider;

class BlamableResolver implements BlamableResolverInterface
Expand All @@ -17,17 +16,7 @@ public function __construct(private EntityMetadataProvider $provider)
{
}

/**
* @see \Hostnet\Component\EntityBlamable\Resolver\BlamableResolverInterface::getBlamableAnnotation()
*
* @deprecated Please use the attribute instead.
*/
public function getBlamableAnnotation(EntityManagerInterface $em, $entity)
{
return $this->provider->getAnnotationFromEntity($em, $entity, BlamabeAnnotation::class);
}

public function getBlamableAttribute(EntityManagerInterface $em, $entity): ?Blamable
public function getBlamableAttribute(EntityManagerInterface $em, object $entity): ?Blamable
{
return $this->provider->getAttributeFromEntity(Blamable::class, $em, $entity);
}
Expand Down
12 changes: 1 addition & 11 deletions src/Resolver/BlamableResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,5 @@

interface BlamableResolverInterface
{
/**
* Return the blamable annotation
*
* @param EntityManagerInterface $em
* @param mixed $entity
*
* @deprecated Please use the attribute instead.
*/
public function getBlamableAnnotation(EntityManagerInterface $em, $entity);

public function getBlamableAttribute(EntityManagerInterface $em, $entity): ?Blamable;
public function getBlamableAttribute(EntityManagerInterface $em, object $entity): ?Blamable;
}
Loading
Loading