Skip to content

Commit 5d6ad1c

Browse files
committed
refactor(command): separate executor and normalization logic for improved SoC
1 parent 691b31c commit 5d6ad1c

File tree

4 files changed

+144
-58
lines changed

4 files changed

+144
-58
lines changed

src/Console/Commands/MigrationGeneratorCommand.php

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
use Illuminate\Console\ConfirmableTrait;
77
use Illuminate\Database\Migrations\Migrator;
88
use Illuminate\Support\Facades\Config;
9-
use N3XT0R\MigrationGenerator\Service\Generator\DTO\MigrationTimingDto;
10-
use N3XT0R\MigrationGenerator\Service\Generator\MigrationGenerator;
9+
use N3XT0R\MigrationGenerator\Service\Executor\SchemaMigrationExecutorInterface;
1110
use N3XT0R\MigrationGenerator\Service\Generator\MigrationGeneratorInterface;
12-
use N3XT0R\MigrationGenerator\Service\Generator\Normalization\SchemaNormalizationManager;
1311
use N3XT0R\MigrationGenerator\Service\Generator\Normalization\SchemaNormalizationManagerInterface;
1412
use N3XT0R\MigrationGenerator\Service\Parser\SchemaParserInterface;
1513

@@ -87,66 +85,32 @@ public function handle(): int
8785
]
8886
);
8987

90-
$this->createMigrationsForWholeSchema($schemaParser, $connectionName, $enabled);
91-
92-
return Command::SUCCESS;
93-
}
94-
95-
protected function createMigrationsForWholeSchema(
96-
SchemaParserInterface $schemaParser,
97-
string $connectionName,
98-
?array $enabledNormalizer
99-
): void {
10088
$laravel = $this->getLaravel();
101-
/**
102-
* @var MigrationGenerator $generator
103-
*/
10489
$generator = $laravel->make(
10590
MigrationGeneratorInterface::class,
10691
['connectionName' => $connectionName]
10792
);
10893

109-
/**
110-
* @var SchemaNormalizationManager $normalizer
111-
*/
112-
$normalizer = $laravel->make(
113-
SchemaNormalizationManagerInterface::class,
114-
['enabled' => $enabledNormalizer]
115-
);
116-
$generator->setNormalizationManager($normalizer);
117-
118-
$database = $this->getMigrator()->resolveConnection($connectionName)->getDatabaseName();
119-
$tables = $schemaParser->getSortedTablesFromSchema(
120-
$database
121-
);
122-
$tableAmount = count($tables);
123-
$bar = $this->output->createProgressBar($tableAmount);
124-
$bar->setFormat('verbose');
125-
$bar->start();
126-
$migrationTimingDto = new MigrationTimingDto();
127-
$migrationTimingDto->setMaxAmount($tableAmount);
128-
$migrationTimingDto->setTimestamp(time());
129-
130-
foreach ($tables as $num => $table) {
131-
$migrationTimingDto->setCurrentAmount($num);
132-
if (true === $generator->generateMigrationForTable($database, $table, $migrationTimingDto)) {
133-
$bar->advance();
134-
} else {
135-
$this->error('there occurred an error by creating migration for ' . $table);
136-
$this->error(implode(', ', $generator->getErrorMessages()));
137-
break;
138-
}
94+
$normalizer = null;
95+
if (count($enabled) > 0) {
96+
$normalizer = $laravel->make(
97+
SchemaNormalizationManagerInterface::class,
98+
['enabled' => $enabled]
99+
);
139100
}
140101

141-
$bar->finish();
142-
$this->line('');
143-
}
102+
$executor = $laravel->make(SchemaMigrationExecutorInterface::class, [
103+
'generator' => $generator,
104+
'normalizer' => $normalizer
105+
]);
144106

107+
return $executor->run($schemaParser, $connectionName, $this->output);
108+
}
145109

146110
/**
147111
* Prepare the migration database for running.
148112
*
149-
* @param string|null $database
113+
* @param string|null $database
150114
* @return void
151115
*/
152116
protected function prepareDatabase(string $database = null): void
@@ -168,7 +132,7 @@ protected function extendSignatureWithNormalizers(): void
168132

169133
if (!empty($normalizers)) {
170134
$choices = implode(',', $normalizers);
171-
$this->signature .= ' {--normalizer=* : Enabled normalizers (available: ' . $choices . ')}';
135+
$this->signature .= ' {--normalizer=* : Enabled normalizers (available: '.$choices.')}';
172136
}
173137
}
174138

@@ -187,7 +151,7 @@ protected function validateNormalizers(array $enabled): bool
187151
$invalid = array_diff($enabled, $available);
188152

189153
if (!empty($invalid)) {
190-
$this->error('Invalid normalizer(s): ' . implode(', ', $invalid));
154+
$this->error('Invalid normalizer(s): '.implode(', ', $invalid));
191155
$result = false;
192156
}
193157

src/Providers/MigrationGeneratorServiceProvider.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Illuminate\Support\ServiceProvider;
1010
use Illuminate\View\Engines\EngineResolver;
1111
use N3XT0R\MigrationGenerator\Console\Commands;
12+
use N3XT0R\MigrationGenerator\Service\Executor\SchemaMigrationExecutor;
13+
use N3XT0R\MigrationGenerator\Service\Executor\SchemaMigrationExecutorInterface;
1214
use N3XT0R\MigrationGenerator\Service\Generator\Compiler\Engine\ReplaceEngine;
1315
use N3XT0R\MigrationGenerator\Service\Generator\Compiler\MigrationCompiler;
1416
use N3XT0R\MigrationGenerator\Service\Generator\Compiler\MigrationCompilerInterface;
@@ -33,10 +35,10 @@ class MigrationGeneratorServiceProvider extends ServiceProvider
3335
*/
3436
public function boot(): void
3537
{
36-
$this->loadViewsFrom(__DIR__ . '/../Stubs/', 'migration-generator');
38+
$this->loadViewsFrom(__DIR__.'/../Stubs/', 'migration-generator');
3739
$this->publishes(
3840
[
39-
__DIR__ . '/../Config/migration-generator.php' => config_path('migration-generator.php'),
41+
__DIR__.'/../Config/migration-generator.php' => config_path('migration-generator.php'),
4042
],
4143
'migration-generator'
4244
);
@@ -49,14 +51,15 @@ public function boot(): void
4951
*/
5052
public function register(): void
5153
{
52-
$this->mergeConfigFrom(__DIR__ . '/../Config/migration-generator.php', 'migration-generator');
54+
$this->mergeConfigFrom(__DIR__.'/../Config/migration-generator.php', 'migration-generator');
5355
$this->registerParserFactory();
5456
$this->registerParser();
5557
$this->registerCompilerEngine();
5658
$this->registerCompiler();
5759
$this->registerDefinitionResolver();
5860
$this->registerNormalizer();
5961
$this->registerGenerator();
62+
$this->registerExecutor();
6063
$this->registerCommands();
6164
}
6265

@@ -98,7 +101,7 @@ function (Application $app, array $params = []) {
98101

99102
protected function getConfigSection(string $key): array
100103
{
101-
return (array)$this->app['config']->get('migration-generator.' . $key);
104+
return (array)$this->app['config']->get('migration-generator.'.$key);
102105
}
103106

104107
protected function registerDefinitionResolver(): void
@@ -111,7 +114,7 @@ protected function registerDefinitionResolver(): void
111114
static function (Application $app, array $params) use ($definitions) {
112115
$key = 'connection';
113116
if (!array_key_exists($key, $params)) {
114-
throw new \InvalidArgumentException('missing key ' . $key . ' in params.');
117+
throw new \InvalidArgumentException('missing key '.$key.' in params.');
115118
}
116119

117120
return new DefinitionResolver($params[$key], $definitions);
@@ -133,7 +136,7 @@ static function (Application $app, array $params) use ($config) {
133136

134137
$key = 'connectionName';
135138
if (!array_key_exists($key, $params)) {
136-
throw new \InvalidArgumentException('missing key ' . $key . ' in params.');
139+
throw new \InvalidArgumentException('missing key '.$key.' in params.');
137140
}
138141

139142
/**
@@ -233,4 +236,17 @@ static function (Application $app, array $params) use ($normalizer) {
233236
}
234237
);
235238
}
239+
240+
protected function registerExecutor(): void
241+
{
242+
$this->app->bind(SchemaMigrationExecutorInterface::class, function ($app, $params) {
243+
if (!array_key_exists('generator', $params)) {
244+
throw new \InvalidArgumentException('missing key generator in params.');
245+
}
246+
return new SchemaMigrationExecutor(
247+
$params['generator'],
248+
$params['normalizer'] ?? null
249+
);
250+
});
251+
}
236252
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace N3XT0R\MigrationGenerator\Service\Executor;
6+
7+
use Illuminate\Support\Facades\DB;
8+
use N3XT0R\MigrationGenerator\Service\Generator\DTO\MigrationTimingDto;
9+
use N3XT0R\MigrationGenerator\Service\Generator\MigrationGeneratorInterface;
10+
use N3XT0R\MigrationGenerator\Service\Generator\Normalization\SchemaNormalizationManagerInterface;
11+
use N3XT0R\MigrationGenerator\Service\Parser\SchemaParserInterface;
12+
use Symfony\Component\Console\Command\Command as CommandAlias;
13+
use Symfony\Component\Console\Style\OutputStyle;
14+
15+
class SchemaMigrationExecutor implements SchemaMigrationExecutorInterface
16+
{
17+
protected MigrationGeneratorInterface $generator;
18+
19+
protected ?SchemaNormalizationManagerInterface $normalizer = null;
20+
21+
public function __construct(
22+
MigrationGeneratorInterface $generator,
23+
?SchemaNormalizationManagerInterface $normalizer = null
24+
) {
25+
$this->setGenerator($generator);
26+
$this->setNormalizer($normalizer);
27+
}
28+
29+
public function getGenerator(): MigrationGeneratorInterface
30+
{
31+
return $this->generator;
32+
}
33+
34+
public function setGenerator(MigrationGeneratorInterface $generator): void
35+
{
36+
$this->generator = $generator;
37+
}
38+
39+
public function getNormalizer(): ?SchemaNormalizationManagerInterface
40+
{
41+
return $this->normalizer;
42+
}
43+
44+
public function setNormalizer(?SchemaNormalizationManagerInterface $normalizer): void
45+
{
46+
$this->normalizer = $normalizer;
47+
}
48+
49+
public function run(
50+
SchemaParserInterface $schemaParser,
51+
string $connectionName,
52+
OutputStyle $output
53+
): int {
54+
$result = CommandAlias::SUCCESS;
55+
$generator = $this->getGenerator();
56+
$normalizer = $this->getNormalizer();
57+
if ($normalizer) {
58+
$generator->setNormalizationManager($normalizer);
59+
}
60+
61+
$database = DB::connection($connectionName)->getDatabaseName();
62+
$tables = $schemaParser->getSortedTablesFromSchema($database);
63+
64+
$bar = $output->createProgressBar(count($tables));
65+
$bar->setFormat('verbose');
66+
$bar->start();
67+
68+
$dto = new MigrationTimingDto();
69+
$dto->setMaxAmount(count($tables));
70+
$dto->setTimestamp(time());
71+
72+
foreach ($tables as $i => $table) {
73+
$dto->setCurrentAmount($i);
74+
if ($generator->generateMigrationForTable($database, $table, $dto)) {
75+
$bar->advance();
76+
} else {
77+
$output->error("Error creating migration for $table");
78+
$output->error(implode(', ', $this->generator->getErrorMessages()));
79+
$result = CommandAlias::FAILURE;
80+
break;
81+
}
82+
}
83+
84+
$bar->finish();
85+
$output->writeln('');
86+
return $result;
87+
}
88+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace N3XT0R\MigrationGenerator\Service\Executor;
6+
7+
use N3XT0R\MigrationGenerator\Service\Parser\SchemaParserInterface;
8+
use Symfony\Component\Console\Style\OutputStyle;
9+
10+
interface SchemaMigrationExecutorInterface
11+
{
12+
13+
public function run(
14+
SchemaParserInterface $schemaParser,
15+
string $connectionName,
16+
OutputStyle $output
17+
): int;
18+
}

0 commit comments

Comments
 (0)