Skip to content

Commit 2dfca51

Browse files
committed
feat: allow creating and dropping entities by class name
1 parent f264a20 commit 2dfca51

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/EntityManager.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,21 @@ public function get(string $name, ?string $connection = null): Entity
4545
return $entity;
4646
}
4747

48-
/** @throws InvalidArgumentException if the entity is not found. */
48+
/**
49+
* Create an entity.
50+
*
51+
* @param class-string<Entity>|string|Entity $entity The entity name, class, or instance.
52+
* @throws InvalidArgumentException if the entity is not found.
53+
*/
4954
public function create(Entity|string $entity): void
5055
{
5156
if (is_string($entity)) {
52-
$entity = $this->get($entity);
57+
$entity = class_exists($entity)
58+
? resolve($entity)
59+
: $this->get($entity);
5360
}
5461

62+
assert($entity instanceof Entity);
5563
$connection = $this->connection($entity);
5664

5765
if (! $entity->creating($connection)) {
@@ -64,13 +72,21 @@ public function create(Entity|string $entity): void
6472
$entity->created($connection);
6573
}
6674

67-
/** @throws InvalidArgumentException if the entity is not found. */
75+
/**
76+
* Drop an entity.
77+
*
78+
* @param class-string<Entity>|string|Entity $entity The entity name, class, or instance.
79+
* @throws InvalidArgumentException if the entity is not found.
80+
*/
6881
public function drop(Entity|string $entity): void
6982
{
7083
if (is_string($entity)) {
71-
$entity = $this->get($entity);
84+
$entity = class_exists($entity)
85+
? resolve($entity)
86+
: $this->get($entity);
7287
}
7388

89+
assert($entity instanceof Entity);
7490
$connection = $this->connection($entity);
7591

7692
if (! $entity->dropping($connection)) {

tests/Feature/EntityManagerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
test()->manager->create($entity);
6060
})->with([
6161
'name' => 'users_view',
62+
'class' => UserView::class,
6263
'entity' => new UserView(),
6364
]);
6465

@@ -85,6 +86,7 @@
8586
test()->manager->drop($entity);
8687
})->with([
8788
'name' => 'users_view',
89+
'class' => UserView::class,
8890
'entity' => new UserView(),
8991
]);
9092

0 commit comments

Comments
 (0)