|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace CalebDW\SqlEntities; |
| 6 | + |
| 7 | +use CalebDW\SqlEntities\Entities\Entity; |
| 8 | +use CalebDW\SqlEntities\Grammars\Grammar; |
| 9 | +use CalebDW\SqlEntities\Grammars\PostgresGrammar; |
| 10 | +use CalebDW\SqlEntities\Grammars\SQLiteGrammar; |
| 11 | +use Illuminate\Database\Connection; |
| 12 | +use Illuminate\Database\DatabaseManager; |
| 13 | +use Illuminate\Support\Collection; |
| 14 | +use InvalidArgumentException; |
| 15 | + |
| 16 | +class EntityManager |
| 17 | +{ |
| 18 | + /** |
| 19 | + * The active grammar instances. |
| 20 | + * |
| 21 | + * @var array<string, Grammar> |
| 22 | + */ |
| 23 | + protected array $grammars = []; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + /** @var Collection<int, Entity> */ |
| 27 | + public readonly Collection $entities, |
| 28 | + protected DatabaseManager $db, |
| 29 | + ) { |
| 30 | + } |
| 31 | + |
| 32 | + /** @throws InvalidArgumentException if the entity is not found. */ |
| 33 | + public function get(string $name, ?string $connection = null): Entity |
| 34 | + { |
| 35 | + $entity = $this->entities->firstWhere( |
| 36 | + fn (Entity $e) => $e->name() === $name |
| 37 | + && $e->connectionName() === $connection, |
| 38 | + ); |
| 39 | + |
| 40 | + throw_if( |
| 41 | + $entity === null, |
| 42 | + new InvalidArgumentException("Entity [{$name}] not found."), |
| 43 | + ); |
| 44 | + |
| 45 | + return $entity; |
| 46 | + } |
| 47 | + |
| 48 | + /** @throws InvalidArgumentException if the entity is not found. */ |
| 49 | + public function create(Entity|string $entity): void |
| 50 | + { |
| 51 | + if (is_string($entity)) { |
| 52 | + $entity = $this->get($entity); |
| 53 | + } |
| 54 | + |
| 55 | + $connection = $this->connection($entity); |
| 56 | + $grammar = $this->grammar($connection); |
| 57 | + |
| 58 | + $entity->creating($connection); |
| 59 | + $connection->statement($grammar->compileCreate($entity)); |
| 60 | + $entity->created($connection); |
| 61 | + } |
| 62 | + |
| 63 | + /** @throws InvalidArgumentException if the entity is not found. */ |
| 64 | + public function drop(Entity|string $entity): void |
| 65 | + { |
| 66 | + if (is_string($entity)) { |
| 67 | + $entity = $this->get($entity); |
| 68 | + } |
| 69 | + |
| 70 | + $connection = $this->connection($entity); |
| 71 | + $grammar = $this->grammar($connection); |
| 72 | + |
| 73 | + $entity->dropping($connection); |
| 74 | + $connection->statement($grammar->compileDrop($entity)); |
| 75 | + $entity->dropped($connection); |
| 76 | + } |
| 77 | + |
| 78 | + /** @param class-string<Entity>|null $type */ |
| 79 | + public function createAll(?string $type = null, ?string $connection = null): void |
| 80 | + { |
| 81 | + $this->entities |
| 82 | + ->when($connection, fn ($c) => $c->filter( |
| 83 | + fn ($e) => $e->connectionName() === $connection, |
| 84 | + )) |
| 85 | + ->when($type, fn ($c, $t) => $c->filter(fn ($e) => is_a($e, $t))) |
| 86 | + ->each(fn ($e) => $this->create($e)); |
| 87 | + } |
| 88 | + |
| 89 | + /** @param class-string<Entity>|null $type */ |
| 90 | + public function dropAll(?string $type = null, ?string $connection = null): void |
| 91 | + { |
| 92 | + $this->entities |
| 93 | + ->when($connection, fn ($c) => $c->filter( |
| 94 | + fn ($e) => $e->connectionName() === $connection, |
| 95 | + )) |
| 96 | + ->when($type, fn ($c, $t) => $c->filter(fn ($e) => is_a($e, $t))) |
| 97 | + ->each(fn ($e) => $this->drop($e)); |
| 98 | + } |
| 99 | + |
| 100 | + protected function connection(Entity $entity): Connection |
| 101 | + { |
| 102 | + return $this->db->connection($entity->connectionName()); |
| 103 | + } |
| 104 | + |
| 105 | + protected function grammar(Connection $connection): Grammar |
| 106 | + { |
| 107 | + $driver = $connection->getDriverName(); |
| 108 | + |
| 109 | + return $this->grammars[$driver] ??= $this->createGrammar($driver, $connection); |
| 110 | + } |
| 111 | + |
| 112 | + protected function createGrammar(string $driver, Connection $connection): Grammar |
| 113 | + { |
| 114 | + return match ($driver) { |
| 115 | + 'sqlite' => new SQLiteGrammar($connection), |
| 116 | + 'pgsql' => new PostgresGrammar($connection), |
| 117 | + default => throw new InvalidArgumentException( |
| 118 | + "Unsupported driver [{$driver}].", |
| 119 | + ), |
| 120 | + }; |
| 121 | + } |
| 122 | +} |
0 commit comments