1414use Illuminate \Database \Connection ;
1515use Illuminate \Database \DatabaseManager ;
1616use Illuminate \Support \Collection ;
17+ use Illuminate \Support \ItemNotFoundException ;
1718use InvalidArgumentException ;
1819
1920class SqlEntityManager
2021{
22+ /** @var Collection<class-string<SqlEntity>, SqlEntity> */
23+ public readonly Collection $ entities ;
24+
2125 /**
2226 * The active grammar instances.
2327 *
2428 * @var array<string, Grammar>
2529 */
2630 protected array $ grammars = [];
2731
32+ /** @param Collection<int, SqlEntity> $entities */
2833 public function __construct (
29- /** @var Collection<int, SqlEntity> */
30- public readonly Collection $ entities ,
34+ Collection $ entities ,
3135 protected DatabaseManager $ db ,
3236 ) {
37+ $ this ->entities = $ entities
38+ ->keyBy (fn ($ entity ) => $ entity ::class);
3339 }
3440
35- /** @throws InvalidArgumentException if the entity is not found. */
36- public function get (string $ name , ?string $ connection = null ): SqlEntity
41+ /**
42+ * Get the entity by class.
43+ *
44+ * @param class-string<SqlEntity> $name
45+ * @throws ItemNotFoundException
46+ */
47+ public function get (string $ name ): SqlEntity
3748 {
38- $ entity = $ this ->entities ->firstWhere (
39- fn (SqlEntity $ e ) => $ e ->name () === $ name
40- && $ e ->connectionName () === $ connection ,
41- );
49+ $ entity = $ this ->entities ->get ($ name );
4250
43- throw_if (
44- $ entity === null ,
45- new InvalidArgumentException ("Entity [ {$ name }] not found. " ),
46- );
51+ if ($ entity === null ) {
52+ throw new ItemNotFoundException ("Entity [ {$ name }] not found. " );
53+ }
4754
4855 return $ entity ;
4956 }
5057
5158 /**
5259 * Create an entity.
5360 *
54- * @param class-string<SqlEntity>|string| SqlEntity $entity The entity name, class, or instance.
55- * @throws InvalidArgumentException if the entity is not found.
61+ * @param class-string<SqlEntity>|SqlEntity $entity
62+ * @throws ItemNotFoundException
5663 */
5764 public function create (SqlEntity |string $ entity ): void
5865 {
5966 if (is_string ($ entity )) {
60- $ entity = class_exists ($ entity )
61- ? resolve ($ entity )
62- : $ this ->get ($ entity );
67+ $ entity = $ this ->get ($ entity );
6368 }
6469
65- assert ($ entity instanceof SqlEntity);
6670 $ connection = $ this ->connection ($ entity );
6771
6872 if (! $ entity ->creating ($ connection )) {
@@ -78,18 +82,15 @@ public function create(SqlEntity|string $entity): void
7882 /**
7983 * Drop an entity.
8084 *
81- * @param class-string<SqlEntity>|string| SqlEntity $entity The entity name, class, or instance.
82- * @throws InvalidArgumentException if the entity is not found.
85+ * @param class-string<SqlEntity>|SqlEntity $entity
86+ * @throws ItemNotFoundException
8387 */
8488 public function drop (SqlEntity |string $ entity ): void
8589 {
8690 if (is_string ($ entity )) {
87- $ entity = class_exists ($ entity )
88- ? resolve ($ entity )
89- : $ this ->get ($ entity );
91+ $ entity = $ this ->get ($ entity );
9092 }
9193
92- assert ($ entity instanceof SqlEntity);
9394 $ connection = $ this ->connection ($ entity );
9495
9596 if (! $ entity ->dropping ($ connection )) {
@@ -144,9 +145,7 @@ protected function createGrammar(string $driver, Connection $connection): Gramma
144145 'pgsql ' => new PostgresGrammar ($ connection ),
145146 'sqlite ' => new SQLiteGrammar ($ connection ),
146147 'sqlsrv ' => new SqlServerGrammar ($ connection ),
147- default => throw new InvalidArgumentException (
148- "Unsupported driver [ {$ driver }]. " ,
149- ),
148+ default => throw new InvalidArgumentException ("Unsupported driver [ {$ driver }]. " ),
150149 };
151150 }
152151}
0 commit comments