Skip to content

Commit 2be8ca6

Browse files
committed
feat: create console commands
1 parent 300e30d commit 2be8ca6

11 files changed

+184
-24
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
coverage/
33
vendor/
44
workbench/vendor
5+
workbench/storage/
56
.phpunit.result.cache
67
.pint.cache
78
composer.lock

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,24 @@ SqlEntity::withoutEntities(
303303

304304
After 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

308326
You may want to automatically drop all SQL entities before migrating, and then

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
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"

phpstan.neon.dist

+2-2
Original file line numberDiff line numberDiff 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
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}

src/Console/Commands/DropCommand.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}

src/ServiceProvider.php

+13-9
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,19 @@
44

55
namespace CalebDW\SqlEntities;
66

7+
use CalebDW\SqlEntities\Console\Commands\CreateCommand;
8+
use CalebDW\SqlEntities\Console\Commands\DropCommand;
79
use CalebDW\SqlEntities\Contracts\SqlEntity;
810
use CalebDW\SqlEntities\Support\Composer;
911
use Illuminate\Contracts\Foundation\Application;
10-
use Illuminate\Contracts\Support\DeferrableProvider;
1112
use Illuminate\Support\Collection;
1213
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
1314
use Override;
1415
use ReflectionClass;
1516
use 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
{

testbench.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
2+
laravel: ./workbench
23
providers:
34
- CalebDW\SqlEntities\ServiceProvider
4-
laravel: ./workbench
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
});
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
});

tests/TestCase.php

-12
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,10 @@
44

55
namespace CalebDW\SqlEntities\Tests;
66

7-
use Illuminate\Foundation\Testing\RefreshDatabase;
87
use Orchestra\Testbench\Concerns\WithWorkbench;
98
use Orchestra\Testbench\TestCase as OrchestraTestCase;
109

11-
use function Orchestra\Testbench\workbench_path;
12-
1310
abstract class TestCase extends OrchestraTestCase
1411
{
15-
use RefreshDatabase;
1612
use WithWorkbench;
17-
18-
/** @inheritDoc */
19-
protected function defineDatabaseMigrations(): void
20-
{
21-
$this->loadMigrationsFrom(
22-
workbench_path('database/migrations'),
23-
);
24-
}
2513
}

0 commit comments

Comments
 (0)