Skip to content

Commit 182d387

Browse files
committed
feat: autowire callable in transact() method
1 parent 5431a10 commit 182d387

5 files changed

Lines changed: 104 additions & 77 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"php": ">=8.1",
3939
"cycle/database": "^2.11",
4040
"cycle/orm": "^2.7",
41-
"psr/container": "^2.0"
41+
"psr/container": "^2.0",
42+
"yiisoft/injector": "^1.2"
4243
},
4344
"require-dev": {
4445
"buggregator/trap": "^1.5",

composer.lock

Lines changed: 71 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ActiveRecord.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,14 @@ final public static function groupActions(
153153
* will be used.
154154
*
155155
* @template TResult
156-
* @param callable(DatabaseInterface, EntityManagerInterface): TResult $callback Note that the provided
157-
* Entity Manager doesn't collect operations and executes them right away in the opened transaction.
156+
* @param callable(): TResult $callback A function that may accept parameters of the following types in any order:
157+
* - {@see DatabaseInterface}
158+
* - {@see EntityManagerInterface}
159+
* - {@see ORMInterface}
160+
* - {@see SchemaInterface}
161+
* @psalm-param callable(...): TResult $callback
162+
* @note that the provided Entity Manager doesn't collect operations and executes them right away in the opened transaction.
163+
*
158164
* @return TResult
159165
*
160166
* @throws TransactionException

src/Internal/TransactionFacade.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Cycle\ORM\Service\SourceProviderInterface;
1313
use Cycle\ORM\Transaction\Runner;
1414
use Cycle\ORM\Transaction\UnitOfWork;
15+
use Yiisoft\Injector\Injector;
1516

1617
/**
1718
* @internal
@@ -65,7 +66,8 @@ public static function groupOrmActions(
6566

6667
/**
6768
* @template TResult
68-
* @param callable(DatabaseInterface, EntityManagerInterface): TResult $callback
69+
* @param callable(): TResult $callback
70+
* @psalm-param callable(...): TResult $callback
6971
* @param class-string|null $entity If null, the default database will be used.
7072
* @return TResult
7173
*
@@ -86,12 +88,13 @@ public static function transact(
8688
return $dbal->transaction(static function (DatabaseInterface $db) use ($callback): mixed {
8789
$previous = self::$em;
8890
try {
91+
$orm = Facade::getOrm();
8992
self::$em = $em = new EntityManager(
90-
static fn(): UnitOfWork => new UnitOfWork(Facade::getOrm(), Runner::outerTransaction(strict: true)),
93+
static fn(): UnitOfWork => new UnitOfWork($orm, Runner::outerTransaction(strict: true)),
9194
autoExecute: true,
9295
);
9396

94-
return $callback($db, $em);
97+
return (new Injector())->invoke($callback, [$db, $em, $orm, $orm->getHeap(), $orm->getSchema()]);
9598
} finally {
9699
self::$em = $previous;
97100
}

tests/src/Functional/ActiveRecordTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use Cycle\Database\DatabaseInterface;
1313
use Cycle\ORM\EntityManagerInterface;
1414
use Cycle\ORM\Exception\RunnerException;
15+
use Cycle\ORM\Heap\HeapInterface;
16+
use Cycle\ORM\ORMInterface;
17+
use Cycle\ORM\SchemaInterface;
1518
use Cycle\ORM\Select\Repository;
1619
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
1720
use PHPUnit\Framework\Attributes\Test;
@@ -283,6 +286,20 @@ public function it_runs_transaction_with_orm_actions(): void
283286
self::assertSame($savedUserFour->name, $user4->name);
284287
}
285288

289+
#[Test]
290+
public function transact_method_resolves_parameters(): void
291+
{
292+
$ars = User::transact(static fn(
293+
SchemaInterface $schema,
294+
EntityManagerInterface $em,
295+
ORMInterface $orm,
296+
HeapInterface $heap,
297+
DatabaseInterface $dbal,
298+
): array => \func_get_args());
299+
300+
self::assertIsArray($ars);
301+
}
302+
286303
#[Test]
287304
public function query_method_returns_ActiveQuery(): void
288305
{

0 commit comments

Comments
 (0)