Skip to content
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

Updated #751

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/code_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v1
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
extensions: pdo_sqlite, pdo_mysql, pdo_pgsql
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/linter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
steps:
- uses: actions/checkout@v2

- uses: shivammathur/setup-php@v1
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
extensions: pdo_sqlite, pdo_mysql, pdo_pgsql
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
steps:
- uses: actions/checkout@v2

- uses: shivammathur/setup-php@v1
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: pdo_sqlite, pdo_mysql, pdo_pgsql
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
"doctrine/persistence": "^2.5|^3.0",
"doctrine/dbal": "^3.3",
"doctrine/orm": "^2.12",
"doctrine/doctrine-bundle": "^2.6",
"doctrine/doctrine-bundle": "^2.7.2",
"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/security-bundle": "^5.4|^6.0",
"symfony/framework-bundle": "^5.4|^6.0",
"symfony/string": "^5.4|^6.0",
"symfony/translation-contracts": "^2.4|^3.0",
Expand All @@ -36,7 +37,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": "^0.15.1",
"symplify/easy-coding-standard": "^10.2.9",
"symplify/phpstan-extensions": "^10.2.9",
"phpstan/phpstan-doctrine": "^1.3",
Expand Down
2 changes: 0 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Rector\Config\RectorConfig;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Naming\Rector\Class_\RenamePropertyToMatchTypeRector;
use Rector\Nette\Set\NetteSetList;
use Rector\Php80\Rector\FunctionLike\UnionTypesRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
Expand All @@ -31,7 +30,6 @@
SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
NetteSetList::NETTE_CODE_QUALITY,
SetList::NAMING,
LevelSetList::UP_TO_PHP_80,
]);
Expand Down
30 changes: 13 additions & 17 deletions src/EventSubscriber/BlameableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@

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\Events;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Persistence\Event\LifecycleEventArgs as BaseLifecycleEventArgs;
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
Expand Down Expand Up @@ -59,9 +63,9 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $loadClassMetadataE
/**
* Stores the current user into createdBy and updatedBy properties
*/
public function prePersist(LifecycleEventArgs $lifecycleEventArgs): void
public function prePersist(BaseLifecycleEventArgs $lifecycleEventArgs): void
{
$entity = $lifecycleEventArgs->getEntity();
$entity = $lifecycleEventArgs->getObject();
if (! $entity instanceof BlameableInterface) {
return;
}
Expand Down Expand Up @@ -90,9 +94,9 @@ public function prePersist(LifecycleEventArgs $lifecycleEventArgs): void
/**
* Stores the current user into updatedBy property
*/
public function preUpdate(LifecycleEventArgs $lifecycleEventArgs): void
public function preUpdate(BaseLifecycleEventArgs $lifecycleEventArgs): void
{
$entity = $lifecycleEventArgs->getEntity();
$entity = $lifecycleEventArgs->getObject();
if (! $entity instanceof BlameableInterface) {
return;
}
Expand All @@ -112,9 +116,9 @@ public function preUpdate(LifecycleEventArgs $lifecycleEventArgs): void
/**
* Stores the current user into deletedBy property
*/
public function preRemove(LifecycleEventArgs $lifecycleEventArgs): void
public function preRemove(BaseLifecycleEventArgs $lifecycleEventArgs): void
{
$entity = $lifecycleEventArgs->getEntity();
$entity = $lifecycleEventArgs->getObject();
if (! $entity instanceof BlameableInterface) {
return;
}
Expand All @@ -131,14 +135,6 @@ public function preRemove(LifecycleEventArgs $lifecycleEventArgs): void
->propertyChanged($entity, self::DELETED_BY, $oldDeletedBy, $user);
}

/**
* @return string[]
*/
public function getSubscribedEvents(): array
{
return [Events::prePersist, Events::preUpdate, Events::preRemove, Events::loadClassMetadata];
}

private function mapEntity(ClassMetadataInfo $classMetadataInfo): void
{
if ($this->blameableUserEntity !== null && class_exists($this->blameableUserEntity)) {
Expand Down
40 changes: 18 additions & 22 deletions src/EventSubscriber/LoggableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@

namespace Knp\DoctrineBehaviors\EventSubscriber;

use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs as BaseLifecycleEventArgs;
use Knp\DoctrineBehaviors\Contract\Entity\LoggableInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

final class LoggableEventSubscriber implements EventSubscriberInterface
#[AsDoctrineListener(event: Events::postPersist)]
#[AsDoctrineListener(event: Events::postUpdate)]
#[AsDoctrineListener(event: Events::preRemove)]
final class LoggableEventSubscriber
{
public function __construct(
private EntityManagerInterface $entityManager,
private LoggerInterface $logger
) {
}

public function postPersist(LifecycleEventArgs $lifecycleEventArgs): void
public function postPersist(BaseLifecycleEventArgs $lifecycleEventArgs): void
{
$entity = $lifecycleEventArgs->getEntity();
$entity = $lifecycleEventArgs->getObject();
if (! $entity instanceof LoggableInterface) {
return;
}
Expand All @@ -31,44 +36,35 @@ public function postPersist(LifecycleEventArgs $lifecycleEventArgs): void
$this->logChangeSet($lifecycleEventArgs);
}

public function postUpdate(LifecycleEventArgs $lifecycleEventArgs): void
public function postUpdate(BaseLifecycleEventArgs $lifecycleEventArgs): void
{
$entity = $lifecycleEventArgs->getEntity();
$entity = $lifecycleEventArgs->getObject();
if (! $entity instanceof LoggableInterface) {
return;
}

$this->logChangeSet($lifecycleEventArgs);
}

public function preRemove(LifecycleEventArgs $lifecycleEventArgs): void
public function preRemove(BaseLifecycleEventArgs $lifecycleEventArgs): void
{
$entity = $lifecycleEventArgs->getEntity();
$entity = $lifecycleEventArgs->getObject();

if ($entity instanceof LoggableInterface) {
$this->logger->log(LogLevel::INFO, $entity->getRemoveLogMessage());
}
}

/**
* @return string[]
*/
public function getSubscribedEvents(): array
{
return [Events::postPersist, Events::postUpdate, Events::preRemove];
}

/**
* Logs entity changeset
*/
private function logChangeSet(LifecycleEventArgs $lifecycleEventArgs): void
private function logChangeSet(BaseLifecycleEventArgs $lifecycleEventArgs): void
{
$entityManager = $lifecycleEventArgs->getEntityManager();
$unitOfWork = $entityManager->getUnitOfWork();
$entity = $lifecycleEventArgs->getEntity();
$unitOfWork = $this->entityManager->getUnitOfWork();
$entity = $lifecycleEventArgs->getObject();

$entityClass = $entity::class;
$classMetadata = $entityManager->getClassMetadata($entityClass);
$classMetadata = $this->entityManager->getClassMetadata($entityClass);

/** @var LoggableInterface $entity */
$unitOfWork->computeChangeSet($classMetadata, $entity);
Expand Down
25 changes: 10 additions & 15 deletions src/EventSubscriber/SluggableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

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\Events;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\Persistence\Event\LifecycleEventArgs as BaseLifecycleEventArgs;
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
use Knp\DoctrineBehaviors\Repository\DefaultSluggableRepository;

final class SluggableEventSubscriber implements EventSubscriberInterface
#[AsDoctrineListener(event: Events::loadClassMetadata)]
#[AsDoctrineListener(event: Events::prePersist)]
#[AsDoctrineListener(event: Events::preUpdate)]
final class SluggableEventSubscriber
{
/**
* @var string
Expand All @@ -40,24 +43,16 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $loadClassMetadataE
]);
}

public function prePersist(LifecycleEventArgs $lifecycleEventArgs): void
public function prePersist(BaseLifecycleEventArgs $lifecycleEventArgs): void
{
$this->processLifecycleEventArgs($lifecycleEventArgs);
}

public function preUpdate(LifecycleEventArgs $lifecycleEventArgs): void
public function preUpdate(BaseLifecycleEventArgs $lifecycleEventArgs): void
{
$this->processLifecycleEventArgs($lifecycleEventArgs);
}

/**
* @return string[]
*/
public function getSubscribedEvents(): array
{
return [Events::loadClassMetadata, Events::prePersist, Events::preUpdate];
}

private function shouldSkip(ClassMetadataInfo $classMetadataInfo): bool
{
if (! is_a($classMetadataInfo->getName(), SluggableInterface::class, true)) {
Expand All @@ -67,9 +62,9 @@ private function shouldSkip(ClassMetadataInfo $classMetadataInfo): bool
return $classMetadataInfo->hasField(self::SLUG);
}

private function processLifecycleEventArgs(LifecycleEventArgs $lifecycleEventArgs): void
private function processLifecycleEventArgs(BaseLifecycleEventArgs $lifecycleEventArgs): void
{
$entity = $lifecycleEventArgs->getEntity();
$entity = $lifecycleEventArgs->getObject();
if (! $entity instanceof SluggableInterface) {
return;
}
Expand Down
25 changes: 12 additions & 13 deletions src/EventSubscriber/SoftDeletableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@

namespace Knp\DoctrineBehaviors\EventSubscriber;

use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;

final class SoftDeletableEventSubscriber implements EventSubscriberInterface
#[AsDoctrineListener(event: Events::loadClassMetadata)]
#[AsDoctrineListener(event: Events::onFlush)]
final class SoftDeletableEventSubscriber
{
public function __construct(
private EntityManagerInterface $entityManager,
) {
}

/**
* @var string
*/
private const DELETED_AT = 'deletedAt';

public function onFlush(OnFlushEventArgs $onFlushEventArgs): void
{
$entityManager = $onFlushEventArgs->getEntityManager();
$unitOfWork = $entityManager->getUnitOfWork();
$unitOfWork = $this->entityManager->getUnitOfWork();

foreach ($unitOfWork->getScheduledEntityDeletions() as $entity) {
if (! $entity instanceof SoftDeletableInterface) {
Expand All @@ -30,7 +37,7 @@ public function onFlush(OnFlushEventArgs $onFlushEventArgs): void
$oldValue = $entity->getDeletedAt();

$entity->delete();
$entityManager->persist($entity);
$this->entityManager->persist($entity);

$unitOfWork->propertyChanged($entity, self::DELETED_AT, $oldValue, $entity->getDeletedAt());
$unitOfWork->scheduleExtraUpdate($entity, [
Expand Down Expand Up @@ -61,12 +68,4 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $loadClassMetadataE
'nullable' => true,
]);
}

/**
* @return string[]
*/
public function getSubscribedEvents(): array
{
return [Events::onFlush, Events::loadClassMetadata];
}
}
13 changes: 3 additions & 10 deletions src/EventSubscriber/TimestampableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace Knp\DoctrineBehaviors\EventSubscriber;

use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;

final class TimestampableEventSubscriber implements EventSubscriberInterface
#[AsDoctrineListener(event: Events::loadClassMetadata)]
final class TimestampableEventSubscriber
{
public function __construct(
private string $timestampableDateFieldType
Expand Down Expand Up @@ -45,12 +46,4 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $loadClassMetadataE
}
}
}

/**
* @return string[]
*/
public function getSubscribedEvents(): array
{
return [Events::loadClassMetadata];
}
}
Loading
Loading