File tree Expand file tree Collapse file tree 11 files changed +184
-24
lines changed Expand file tree Collapse file tree 11 files changed +184
-24
lines changed Original file line number Diff line number Diff line change 22coverage /
33vendor /
44workbench /vendor
5+ workbench /storage /
56.phpunit.result.cache
67.pint.cache
78composer.lock
Original file line number Diff line number Diff line change @@ -303,6 +303,24 @@ SqlEntity::withoutEntities(
303303
304304After the callback, all affected entities are automatically recreated in dependency order.
305305
306+ ### 💻 Console Commands
307+
308+ The package provides console commands to create and drop your SQL entities.
309+
310+ ``` bash
311+ php artisan sql-entities:create [entities] [--connection= CONNECTION ...]
312+
313+ # Create all entities
314+ php artisan sql-entities:create
315+ # Create a specific entity
316+ php artisan sql-entities:create ' Database\Entities\Views\RecentOrdersView'
317+ # Create all entities on a specific connection
318+ php artisan sql-entities:create -c reporting
319+
320+ # Similarly, drop all entities
321+ php artisan sql-entities:drop
322+ ```
323+
306324### 🚀 Automatic syncing when migrating (Optional)
307325
308326You may want to automatically drop all SQL entities before migrating, and then
Original file line number Diff line number Diff line change 3737 },
3838 "require" : {
3939 "php" : " ^8.4" ,
40+ "illuminate/console" : " ^11.0 || ^12.0" ,
4041 "illuminate/contracts" : " ^11.0 || ^12.0" ,
4142 "illuminate/database" : " ^11.0 || ^12.0" ,
4243 "illuminate/support" : " ^11.0 || ^12.0"
Original file line number Diff line number Diff line change @@ -4,5 +4,5 @@ parameters:
44 level: 8
55 paths:
66 - src
7- - workbench/app
8- - workbench/database
7+ # - workbench/app
8+ # - workbench/database
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace CalebDW \SqlEntities \Console \Commands ;
6+
7+ use CalebDW \SqlEntities \SqlEntityManager ;
8+ use Illuminate \Console \Command ;
9+ use Override ;
10+ use Symfony \Component \Console \Attribute \AsCommand ;
11+ use Symfony \Component \Console \Input \InputArgument ;
12+ use Symfony \Component \Console \Input \InputOption ;
13+
14+ #[AsCommand('sql-entities:create ' , 'Create SQL entities. ' )]
15+ class CreateCommand extends Command
16+ {
17+ public function __invoke (SqlEntityManager $ manager ): int
18+ {
19+ $ connections = $ this ->option ('connection ' );
20+ $ entities = $ this ->argument ('entities ' );
21+
22+ /** @phpstan-ignore argument.type */
23+ $ manager ->createAll ($ entities , $ connections );
24+
25+ return self ::SUCCESS ;
26+ }
27+
28+ /** @return array<mixed> */
29+ #[Override]
30+ protected function getArguments (): array
31+ {
32+ return [
33+ new InputArgument (
34+ 'entities ' ,
35+ InputArgument::OPTIONAL | InputArgument::IS_ARRAY ,
36+ 'The entities to create. ' ,
37+ null ,
38+ ),
39+ ];
40+ }
41+
42+ /** @return array<mixed> */
43+ #[Override]
44+ protected function getOptions (): array
45+ {
46+ return [
47+ new InputOption (
48+ 'connection ' ,
49+ 'c ' ,
50+ InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY ,
51+ 'The connection(s) to use. ' ,
52+ ),
53+ ];
54+ }
55+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace CalebDW \SqlEntities \Console \Commands ;
6+
7+ use CalebDW \SqlEntities \SqlEntityManager ;
8+ use Illuminate \Console \Command ;
9+ use Override ;
10+ use Symfony \Component \Console \Attribute \AsCommand ;
11+ use Symfony \Component \Console \Input \InputArgument ;
12+ use Symfony \Component \Console \Input \InputOption ;
13+
14+ #[AsCommand('sql-entities:drop ' , 'Drop SQL entities. ' )]
15+ class DropCommand extends Command
16+ {
17+ public function __invoke (SqlEntityManager $ manager ): int
18+ {
19+ $ connections = $ this ->option ('connection ' );
20+ $ entities = $ this ->argument ('entities ' );
21+
22+ /** @phpstan-ignore argument.type */
23+ $ manager ->dropAll ($ entities , $ connections );
24+
25+ return self ::SUCCESS ;
26+ }
27+
28+ /** @return array<mixed> */
29+ #[Override]
30+ protected function getArguments (): array
31+ {
32+ return [
33+ new InputArgument (
34+ 'entities ' ,
35+ InputArgument::OPTIONAL | InputArgument::IS_ARRAY ,
36+ 'The entities to create. ' ,
37+ null ,
38+ ),
39+ ];
40+ }
41+
42+ /** @return array<mixed> */
43+ #[Override]
44+ protected function getOptions (): array
45+ {
46+ return [
47+ new InputOption (
48+ 'connection ' ,
49+ 'c ' ,
50+ InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY ,
51+ 'The connection(s) to use. ' ,
52+ ),
53+ ];
54+ }
55+ }
Original file line number Diff line number Diff line change 44
55namespace CalebDW \SqlEntities ;
66
7+ use CalebDW \SqlEntities \Console \Commands \CreateCommand ;
8+ use CalebDW \SqlEntities \Console \Commands \DropCommand ;
79use CalebDW \SqlEntities \Contracts \SqlEntity ;
810use CalebDW \SqlEntities \Support \Composer ;
911use Illuminate \Contracts \Foundation \Application ;
10- use Illuminate \Contracts \Support \DeferrableProvider ;
1112use Illuminate \Support \Collection ;
1213use Illuminate \Support \ServiceProvider as IlluminateServiceProvider ;
1314use Override ;
1415use ReflectionClass ;
1516use Symfony \Component \Finder \Finder ;
1617
17- class ServiceProvider extends IlluminateServiceProvider implements DeferrableProvider
18+ class ServiceProvider extends IlluminateServiceProvider
1819{
19- /** @return list<string> */
20- #[Override]
21- public function provides (): array
22- {
23- return [SqlEntityManager::class, 'sql-entities ' ]; // @codeCoverageIgnore
24- }
25-
2620 #[Override]
2721 public function register (): void
2822 {
@@ -37,6 +31,16 @@ public function register(): void
3731 $ this ->app ->alias (SqlEntityManager::class, 'sql-entities ' );
3832 }
3933
34+ public function boot (): void
35+ {
36+ if ($ this ->app ->runningInConsole ()) {
37+ $ this ->commands ([
38+ CreateCommand::class,
39+ DropCommand::class,
40+ ]);
41+ }
42+ }
43+
4044 /** @return Collection<int, SqlEntity> */
4145 protected function getEntities (Application $ app ): Collection
4246 {
Original file line number Diff line number Diff line change 11---
2+ laravel : ./workbench
23providers :
34 - CalebDW\SqlEntities\ServiceProvider
4- laravel : ./workbench
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ use CalebDW \SqlEntities \SqlEntityManager ;
6+
7+ beforeEach (function () {
8+ test ()->manager = test ()->mock (SqlEntityManager::class);
9+ });
10+
11+ it ('can create entities ' , function () {
12+ test ()->manager
13+ ->shouldReceive ('createAll ' )
14+ ->once ()
15+ ->with (null , null );
16+
17+ test ()->artisan ('sql-entities:create ' )
18+ ->assertExitCode (0 );
19+ });
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ use CalebDW \SqlEntities \SqlEntityManager ;
6+
7+ beforeEach (function () {
8+ test ()->manager = test ()->mock (SqlEntityManager::class);
9+ });
10+
11+ it ('can drop entities ' , function () {
12+ test ()->manager
13+ ->shouldReceive ('dropAll ' )
14+ ->once ()
15+ ->with (null , null );
16+
17+ test ()->artisan ('sql-entities:drop ' )
18+ ->assertExitCode (0 );
19+ });
You can’t perform that action at this time.
0 commit comments