Skip to content

Commit 8b69fe9

Browse files
committed
feat: bind entity behaviour to interface
1 parent 8aee302 commit 8b69fe9

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/Bridge/Laravel/Providers/Registrators/RegisterORM.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Cycle\ORM\ORM;
1515
use Cycle\ORM\ORMInterface;
1616
use Cycle\ORM\SchemaInterface;
17+
use Cycle\ORM\Transaction\CommandGeneratorInterface;
1718
use Illuminate\Contracts\Foundation\Application;
1819
use WayOfDev\Cycle\Bridge\Laravel\Factories\SpiralFactory;
1920
use WayOfDev\Cycle\Schema\Config\SchemaConfig;
@@ -39,13 +40,16 @@ public function __invoke(Application $app): void
3940
);
4041
});
4142

42-
$app->singleton(ORMInterface::class, function (Application $app): ORMInterface {
43-
$commandGenerator = null;
44-
$loadEntityBehavior = config('cycle.entityBehavior.register', true);
43+
if ($this->shouldLoadEntityBehavior() === true) {
44+
$app->bind(CommandGeneratorInterface::class, function (Application $app): CommandGeneratorInterface {
45+
return new EventDrivenCommandGenerator($app->get(SchemaInterface::class), $app);
46+
});
47+
}
4548

46-
if (true === $loadEntityBehavior) {
47-
$commandGenerator = new EventDrivenCommandGenerator($app->get(SchemaInterface::class), $app);
48-
}
49+
$app->singleton(ORMInterface::class, function (Application $app): ORMInterface {
50+
$commandGenerator = $this->shouldLoadEntityBehavior()
51+
? $app->get(CommandGeneratorInterface::class)
52+
: null;
4953

5054
return new ORM(
5155
factory: $app->get(FactoryInterface::class),
@@ -60,4 +64,9 @@ public function __invoke(Application $app): void
6064
);
6165
});
6266
}
67+
68+
private function shouldLoadEntityBehavior(): bool
69+
{
70+
return config('cycle.entityBehavior.register', true);
71+
}
6372
}

tests/app/Entities/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class User
3030
public ?User $friend = null;
3131

3232
#[HasMany(target: User::class, outerKey: 'userId', nullable: true)]
33-
public iterable|null $friends = [];
33+
public ?iterable $friends = [];
3434

3535
#[HasMany(target: User::class, outerKey: 'userId', nullable: true, collection: 'array')]
3636
public ?array $friendsAsArray = [];

tests/src/Bridge/Laravel/Providers/Registrators/RegisterORMTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Cycle\ORM\ORM;
1111
use Cycle\ORM\ORMInterface;
1212
use Cycle\ORM\Transaction\CommandGenerator;
13+
use Cycle\ORM\Transaction\CommandGeneratorInterface;
1314
use Psr\Container\ContainerExceptionInterface;
1415
use Psr\Container\NotFoundExceptionInterface;
1516
use WayOfDev\Tests\TestCase;
@@ -51,6 +52,9 @@ public function it_registers_orm_as_singleton(): void
5152

5253
/**
5354
* @test
55+
*
56+
* @throws NotFoundExceptionInterface
57+
* @throws ContainerExceptionInterface
5458
*/
5559
public function it_registers_entity_behavior_by_default(): void
5660
{
@@ -61,6 +65,11 @@ public function it_registers_entity_behavior_by_default(): void
6165
}
6266

6367
$this::assertInstanceOf(EventDrivenCommandGenerator::class, $class->getCommandGenerator());
68+
69+
$this::assertInstanceOf(
70+
EventDrivenCommandGenerator::class,
71+
$this->app->get(CommandGeneratorInterface::class)
72+
);
6473
}
6574

6675
/**

0 commit comments

Comments
 (0)