-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update Subscribers with #[AsDoctrineListener] #742
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,11 @@ jobs: | |
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: shivammathur/setup-php@v1 | ||
- uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: 8.0 | ||
php-version: 8.1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be reverted as Symfony 6.0 still supports PHP 8.0. |
||
extensions: pdo_sqlite, pdo_mysql, pdo_pgsql | ||
coverage: none | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,16 @@ jobs: | |
tests: | ||
strategy: | ||
matrix: | ||
php: | ||
- 8.0 | ||
operating-system: [ ubuntu-latest ] | ||
php-versions: [ '8.1', '8.3' ] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should still test php 8.0 as well for the same reasons. |
||
|
||
name: Test ${{ matrix.php }} | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: shivammathur/setup-php@v1 | ||
- uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
extensions: pdo_sqlite, pdo_mysql, pdo_pgsql | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,18 +11,18 @@ | |
{ "name": "Knplabs", "homepage": "http://knplabs.com" } | ||
], | ||
"require": { | ||
"php": ">=8.0", | ||
"php": ">=8.1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be reverted as Symfony 6 still supports PHP 8.0 unless there is a reason for it. |
||
"doctrine/common": "^3.3", | ||
"doctrine/persistence": "^2.5|^3.0", | ||
"doctrine/dbal": "^3.3", | ||
"doctrine/orm": "^2.12", | ||
"doctrine/doctrine-bundle": "^2.6", | ||
"symfony/cache": "^5.4|^6.0", | ||
"symfony/dependency-injection": "^5.4|^6.0", | ||
"symfony/http-kernel": "^5.4|^6.0", | ||
"symfony/security-core": "^5.4|^6.0", | ||
"symfony/framework-bundle": "^5.4|^6.0", | ||
"symfony/string": "^5.4|^6.0", | ||
"doctrine/doctrine-bundle": "^2.7.2", | ||
alexpozzi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"symfony/cache": "^6.1|^7.0", | ||
"symfony/dependency-injection": "^6.1|^7.0", | ||
"symfony/http-kernel": "^6.1|^7.0", | ||
"symfony/security-bundle": "^6.1|^7.0", | ||
"symfony/framework-bundle": "^6.1|^7.0", | ||
"symfony/string": "^6.1|^7.0", | ||
"symfony/translation-contracts": "^2.4|^3.0", | ||
"nette/utils": "^3.2", | ||
"ramsey/uuid": "^4.2" | ||
|
@@ -36,7 +36,7 @@ | |
"php-parallel-lint/php-parallel-lint": "^1.3", | ||
"phpstan/phpstan": "^1.7.10", | ||
"phpunit/phpunit": "^9.5", | ||
"rector/rector": "^0.13.4", | ||
"rector/rector": "^1.0.0", | ||
"symplify/easy-coding-standard": "^10.2.9", | ||
"symplify/phpstan-extensions": "^10.2.9", | ||
"phpstan/phpstan-doctrine": "^1.3", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would keep |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,17 +4,23 @@ | |||||
|
||||||
namespace Knp\DoctrineBehaviors\EventSubscriber; | ||||||
|
||||||
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface; | ||||||
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; | ||||||
use Doctrine\ORM\EntityManagerInterface; | ||||||
use Doctrine\ORM\Event\LifecycleEventArgs; | ||||||
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; | ||||||
use Doctrine\ORM\Event\PrePersistEventArgs; | ||||||
use Doctrine\ORM\Event\PreRemoveEventArgs; | ||||||
use Doctrine\ORM\Event\PreUpdateEventArgs; | ||||||
use Doctrine\ORM\Events; | ||||||
use Doctrine\ORM\Mapping\ClassMetadataInfo; | ||||||
use Doctrine\ORM\UnitOfWork; | ||||||
use Knp\DoctrineBehaviors\Contract\Entity\BlameableInterface; | ||||||
use Knp\DoctrineBehaviors\Contract\Provider\UserProviderInterface; | ||||||
|
||||||
final class BlameableEventSubscriber implements EventSubscriberInterface | ||||||
#[AsDoctrineListener(event: Events::loadClassMetadata)] | ||||||
#[AsDoctrineListener(event: Events::prePersist)] | ||||||
#[AsDoctrineListener(event: Events::preUpdate)] | ||||||
#[AsDoctrineListener(event: Events::preRemove)] | ||||||
final class BlameableEventSubscriber | ||||||
{ | ||||||
/** | ||||||
* @var string | ||||||
|
@@ -59,10 +65,10 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $loadClassMetadataE | |||||
/** | ||||||
* Stores the current user into createdBy and updatedBy properties | ||||||
*/ | ||||||
public function prePersist(LifecycleEventArgs $lifecycleEventArgs): void | ||||||
public function prePersist(PrePersistEventArgs $prePersistEventArgs): void | ||||||
{ | ||||||
$entity = $lifecycleEventArgs->getEntity(); | ||||||
if (! $entity instanceof BlameableInterface) { | ||||||
$object = $prePersistEventArgs->getObject(); | ||||||
if (! $object instanceof BlameableInterface) { | ||||||
return; | ||||||
} | ||||||
|
||||||
|
@@ -72,28 +78,28 @@ public function prePersist(LifecycleEventArgs $lifecycleEventArgs): void | |||||
return; | ||||||
} | ||||||
|
||||||
if (! $entity->getCreatedBy()) { | ||||||
$entity->setCreatedBy($user); | ||||||
if (! $object->getCreatedBy()) { | ||||||
$object->setCreatedBy($user); | ||||||
|
||||||
$this->getUnitOfWork() | ||||||
->propertyChanged($entity, self::CREATED_BY, null, $user); | ||||||
->propertyChanged($object, self::CREATED_BY, null, $user); | ||||||
} | ||||||
|
||||||
if (! $entity->getUpdatedBy()) { | ||||||
$entity->setUpdatedBy($user); | ||||||
if (! $object->getUpdatedBy()) { | ||||||
$object->setUpdatedBy($user); | ||||||
|
||||||
$this->getUnitOfWork() | ||||||
->propertyChanged($entity, self::UPDATED_BY, null, $user); | ||||||
->propertyChanged($object, self::UPDATED_BY, null, $user); | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Stores the current user into updatedBy property | ||||||
*/ | ||||||
public function preUpdate(LifecycleEventArgs $lifecycleEventArgs): void | ||||||
public function preUpdate(PreUpdateEventArgs $preUpdateEventArgs): void | ||||||
{ | ||||||
$entity = $lifecycleEventArgs->getEntity(); | ||||||
if (! $entity instanceof BlameableInterface) { | ||||||
$object = $preUpdateEventArgs->getObject(); | ||||||
if (! $object instanceof BlameableInterface) { | ||||||
return; | ||||||
} | ||||||
|
||||||
|
@@ -102,20 +108,20 @@ public function preUpdate(LifecycleEventArgs $lifecycleEventArgs): void | |||||
return; | ||||||
} | ||||||
|
||||||
$oldValue = $entity->getUpdatedBy(); | ||||||
$entity->setUpdatedBy($user); | ||||||
$updatedBy = $object->getUpdatedBy(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
$object->setUpdatedBy($user); | ||||||
|
||||||
$this->getUnitOfWork() | ||||||
->propertyChanged($entity, self::UPDATED_BY, $oldValue, $user); | ||||||
->propertyChanged($object, self::UPDATED_BY, $updatedBy, $user); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Stores the current user into deletedBy property | ||||||
*/ | ||||||
public function preRemove(LifecycleEventArgs $lifecycleEventArgs): void | ||||||
public function preRemove(PreRemoveEventArgs $preRemoveEventArgs): void | ||||||
{ | ||||||
$entity = $lifecycleEventArgs->getEntity(); | ||||||
if (! $entity instanceof BlameableInterface) { | ||||||
$object = $preRemoveEventArgs->getObject(); | ||||||
if (! $object instanceof BlameableInterface) { | ||||||
return; | ||||||
} | ||||||
|
||||||
|
@@ -124,19 +130,11 @@ public function preRemove(LifecycleEventArgs $lifecycleEventArgs): void | |||||
return; | ||||||
} | ||||||
|
||||||
$oldDeletedBy = $entity->getDeletedBy(); | ||||||
$entity->setDeletedBy($user); | ||||||
$oldDeletedBy = $object->getDeletedBy(); | ||||||
$object->setDeletedBy($user); | ||||||
|
||||||
$this->getUnitOfWork() | ||||||
->propertyChanged($entity, self::DELETED_BY, $oldDeletedBy, $user); | ||||||
} | ||||||
|
||||||
/** | ||||||
* @return string[] | ||||||
*/ | ||||||
public function getSubscribedEvents(): array | ||||||
{ | ||||||
return [Events::prePersist, Events::preUpdate, Events::preRemove, Events::loadClassMetadata]; | ||||||
->propertyChanged($object, self::DELETED_BY, $oldDeletedBy, $user); | ||||||
} | ||||||
|
||||||
private function mapEntity(ClassMetadataInfo $classMetadataInfo): void | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be reverted as Symfony 6.0 still supports PHP 8.0.
If you want to add a test on PHP 8.1 you should add it instead of just change it to it IMHO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
symfony 6.0 is "Unmaintained" https://symfony.com/releases/6.0
symfony 6.4 is maintained ATM and: "Requires: PHP 8.1.0 or higher"