Skip to content

Commit

Permalink
Merge pull request #5 from andanteproject/feat-clock
Browse files Browse the repository at this point in the history
Release 3.0
  • Loading branch information
cristoforocervino authored Oct 12, 2023
2 parents c6aa449 + 8e89f19 commit 7746860
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
27 changes: 12 additions & 15 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,16 @@ jobs:
run: composer install
- name: CS Check
run: composer cs-check

tests:
name: PHP ${{ matrix.php }} / SF ^${{ matrix.symfony }}
runs-on: ubuntu-latest
strategy:
matrix:
name:
- 'PHP 8.0 tests (Symfony 5.4)'
- 'PHP 8.1 tests (Symfony 6.2)'
- 'PHP 8.1 tests (Symfony 6.3)'
include:
- php: '8.0'
symfony: 5.4.*
- php: '8.1'
symfony: 6.2.*
- php: '8.1'
symfony: 6.3.*
name: ${{ matrix.name }}
php: ['8.1','8.2']
symfony: ['5.4.*', '6.2.*', '6.3.*']
exclude:
- php: '8.2'
symfony: '5.4.*'
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -53,8 +46,12 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- run: |
- if: matrix.symfony == '5.4.*'
run: |
sed -ri 's/"symfony\/framework-bundle": "(.+)"/"symfony\/framework-bundle": "${{ matrix.symfony }}"/' composer.json;
sed -ri 's/"symfony\/yaml": "(.+)"/"symfony\/yaml": "${{ matrix.symfony }}"/' composer.json;
- if: matrix.symfony != '5.4.*'
run: |
sed -ri 's/"symfony\/(.+)": "(.+)"/"symfony\/\1": "${{ matrix.symfony }}"/' composer.json;
if: matrix.symfony
- run: composer update --no-interaction --no-progress --ansi
- run: composer phpunit
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"symfony/framework-bundle": "^5.0 | ^6.0",
"doctrine/common": "^2.13 || ^3.0",
"doctrine/event-manager": "^1.2 | ^2.0"
"doctrine/event-manager": "^1.2 | ^2.0",
"symfony/clock": "^6.2"
},
"require-dev": {
"ext-json": "*",
Expand Down
28 changes: 27 additions & 1 deletion src/DependencyInjection/Compiler/DoctrineEventSubscriberPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,48 @@
namespace Andante\TimestampableBundle\DependencyInjection\Compiler;

use Andante\TimestampableBundle\EventSubscriber\TimestampableEventSubscriber;
use Composer\InstalledVersions;
use Doctrine\ORM\Events;
use Psr\Clock\ClockInterface;
use Symfony\Component\Clock\Clock;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class DoctrineEventSubscriberPass implements CompilerPassInterface
{
public const TIMESTAMPABLE_SUBSCRIBER_SERVICE_ID = 'andante_timestampable.doctrine.timestampable_subscriber';
public const CLOCK_SERVICE_ID = 'andante_timestampable.clock';

public function process(ContainerBuilder $container): void
{
$container
$subscriberDefinition = $container
->register(
self::TIMESTAMPABLE_SUBSCRIBER_SERVICE_ID,
TimestampableEventSubscriber::class
)
->addArgument(new Reference('andante_timestampable.configuration'))
->addTag('doctrine.event_subscriber');
$symfonyDoctrineBridgeVersion = InstalledVersions::getVersion('symfony/doctrine-bridge');
if (null !== $symfonyDoctrineBridgeVersion && \version_compare($symfonyDoctrineBridgeVersion, '6.3', '>=')) {
$subscriberDefinition->addTag('doctrine.event_listener', [
'event' => Events::prePersist,
]);
$subscriberDefinition->addTag('doctrine.event_listener', [
'event' => Events::preUpdate,
]);
$subscriberDefinition->addTag('doctrine.event_listener', [
'event' => Events::loadClassMetadata,
]);
} else {
$subscriberDefinition->addTag('doctrine.event_subscriber');
}

if ($container->has(ClockInterface::class)) {
$subscriberDefinition->addArgument(new Reference(ClockInterface::class));
} else {
$container->register(self::CLOCK_SERVICE_ID, Clock::class);
$subscriberDefinition->addArgument(new Reference(self::CLOCK_SERVICE_ID));
}
}
}
13 changes: 9 additions & 4 deletions src/EventSubscriber/TimestampableEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Psr\Clock\ClockInterface;

class TimestampableEventSubscriber implements EventSubscriber
{
private Configuration $configuration;
private ClockInterface $clock;

public function __construct(Configuration $configuration)
{
public function __construct(
Configuration $configuration,
ClockInterface $clock
) {
$this->configuration = $configuration;
$this->clock = $clock;
}

public function getSubscribedEvents(): array
Expand All @@ -35,15 +40,15 @@ public function prePersist(LifecycleEventArgs $onFlushEventArgs): void
{
$entity = $onFlushEventArgs->getObject();
if ($entity instanceof CreatedAtTimestampableInterface && null === $entity->getCreatedAt()) {
$entity->setCreatedAt(new \DateTimeImmutable());
$entity->setCreatedAt($this->clock->now());
}
}

public function preUpdate(LifecycleEventArgs $onFlushEventArgs): void
{
$entity = $onFlushEventArgs->getObject();
if ($entity instanceof UpdatedAtTimestampableInterface) {
$entity->setUpdatedAt(new \DateTimeImmutable());
$entity->setUpdatedAt($this->clock->now());
}
}

Expand Down

0 comments on commit 7746860

Please sign in to comment.