friends-of-ddd / sequence-number-generator-symfony-bundle
Sequence Number Generator Symfony Bundle
Requires
- php: >=8.1
- friends-of-ddd/sequence-number-generator-doctrine: ^1.0
- symfony/config: ^5.4||^6.0||^7.0
- symfony/dependency-injection: ^5.4||^6.0||^7.0
- symfony/http-kernel: ^5.4||^6.0||^7.0
Requires (Dev)
- ext-pdo_pgsql: *
- doctrine/doctrine-bundle: ^2.13
- friendsofphp/php-cs-fixer: ^3.64
- phpunit/phpunit: ^10.5
- symfony/cache: ^6.4
- symfony/framework-bundle: ^6.4
README
A symfony bundle implementation of friends-of-ddd/sequence-number-generator
using Doctrine to generate sequence numbers.
Installation
composer require friends-of-ddd/sequence-number-generator-symfony-bundle
Enable the bundle manually in the application:
// config/bundles.php
return [
FriendsOfDdd\SequenceNumberGenerator\Infrastructure\Symfony\SequenceNumberGeneratorBundle::class => ['all' => true],
];
You must create a table in DB which saves sequence values.
You can use either bin/console doctrine:migrations:diff
(prefered) or bin/console doctrine:schema:update
.
Table with name sequence
will be created by default. If you need another name see "Configuration" section.
Configuration
Custom table name can be configured the following way:
# config/packages/sequence_number_generator.yaml
sequence_number_generator:
table_name: custom_sequences
For more advanced configuration use directly friends-of-ddd/sequence-number-generator-doctrine
package.
Usage
Generate an incremented sequence number:
use FriendsOfDdd\SequenceNumberGenerator\Domain\SequenceNumberGeneratorInterface;
class OrderFactory
{
public function __construct(
private SequenceNumberGeneratorInterface $sequenceNumberGenerator // inject the service into your class
) {
}
public function create(): void
{
$newOrderId = $this->sequenceNumberGenerator->generateNext('order_id');
return new Order(
id: $newOrderId,
...
);
}
}
Testing
In unit tests you can use FriendsOfDdd\SequenceNumberGenerator\Infrastructure\InMemorySequenceNumberGenerator
to fake real database usage.
use FriendsOfDdd\SequenceNumberGenerator\Infrastructure\InMemorySequenceNumberGenerator;
class OrderFactoryTest extends \PHPUnit\Framework\TestCase
{
private OrderFactory $orderFactory;
protected function setUp(): void
{
$this->orderFactory = new OrderFactory(
sequenceNumberGenerator: new InMemorySequenceNumberGenerator()
);
}
public function testCreateNewOrder(): void
{
// arrange
$previousOrder = $this->orderFactory->create();
// act
$newOrder = $this->orderFactory->create();
// assert
self::assertSame($previousOrder->id + 1, $newOrder->id);
}
}
Supported PHP versions:
- 8.1.*
- 8.2.*
- 8.3.*
- 8.4.*
Supported Symfony versions
- 5.4.48+
- 6.4.19+
- 7.2.4+