Skip to content

Commit 5b45263

Browse files
authored
Merge pull request #43: Add ::tableName() method and autowire ::transact()
Add `::tableName()` method and autowire `::transact()`
2 parents 50d434d + b6ef1d7 commit 5b45263

File tree

7 files changed

+129
-82
lines changed

7 files changed

+129
-82
lines changed

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.

psalm-baseline.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<MixedArgument>
55
<code><![CDATA[$primaryKey]]></code>
66
</MixedArgument>
7-
<PossiblyUnusedReturnValue>
8-
<code><![CDATA[TResult]]></code>
9-
</PossiblyUnusedReturnValue>
7+
<MixedReturnStatement>
8+
<code><![CDATA[static::getOrm()->getSchema()->define(static::class, SchemaInterface::TABLE)]]></code>
9+
</MixedReturnStatement>
1010
</file>
1111
<file src="src/Facade.php">
1212
<MixedReturnStatement>
@@ -19,8 +19,8 @@
1919
</InvalidThrow>
2020
</file>
2121
<file src="src/Repository/ActiveRepository.php">
22-
<MixedArgument>
22+
<ArgumentTypeCoercion>
2323
<code><![CDATA[$id]]></code>
24-
</MixedArgument>
24+
</ArgumentTypeCoercion>
2525
</file>
2626
</files>

src/ActiveRecord.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,24 @@
1212
use Cycle\ORM\Exception\RunnerException;
1313
use Cycle\ORM\ORMInterface;
1414
use Cycle\ORM\RepositoryInterface;
15+
use Cycle\ORM\SchemaInterface;
1516

1617
/**
1718
* A base class for entities that are managed by the ORM.
1819
* Adds a set of ActiveRecord methods to the extending entity class.
1920
*/
2021
abstract class ActiveRecord
2122
{
23+
/**
24+
* Get the table name associated with the entity.
25+
*
26+
* @return non-empty-string
27+
*/
28+
final public static function tableName(): string
29+
{
30+
return static::getOrm()->getSchema()->define(static::class, SchemaInterface::TABLE);
31+
}
32+
2233
/**
2334
* Create a new entity instance with the given data.
2435
* It is preferable to use this method instead of the constructor because
@@ -142,8 +153,14 @@ final public static function groupActions(
142153
* will be used.
143154
*
144155
* @template TResult
145-
* @param callable(DatabaseInterface, EntityManagerInterface): TResult $callback Note that the provided
146-
* 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+
*
147164
* @return TResult
148165
*
149166
* @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,8 +66,9 @@ public static function groupOrmActions(
6566

6667
/**
6768
* @template TResult
68-
* @param callable(DatabaseInterface, EntityManagerInterface): TResult $callback
69+
* @param callable(): TResult $callback
6970
* @param class-string|null $entity If null, the default database will be used.
71+
* @psalm-param callable(...): TResult $callback
7072
* @return TResult
7173
*
7274
* @throws TransactionException
@@ -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
}

src/Repository/ActiveRepository.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public function __construct(string $role)
5151
*
5252
* @note Limit of 1 will be added to the query.
5353
*
54+
* @param string|int|non-empty-list<string|int>|non-empty-array<non-empty-string, string|int>|object $id
55+
*
5456
* @return TEntity|null
5557
*/
5658
public function findByPK(mixed $id): ?object

tests/src/Functional/ActiveRecordTest.php

Lines changed: 24 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,9 +286,30 @@ 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
{
289306
self::assertInstanceOf(ActiveQuery::class, Identity::query());
290307
}
308+
309+
#[Test]
310+
public function get_table_name(): void
311+
{
312+
self::assertSame('user', User::tableName());
313+
self::assertSame('user_identity', Identity::tableName());
314+
}
291315
}

0 commit comments

Comments
 (0)