Skip to content

Commit 4297baf

Browse files
committed
added normalization classes.
1 parent 1b2da09 commit 4297baf

File tree

9 files changed

+157
-16
lines changed

9 files changed

+157
-16
lines changed

src/Config/migration-generator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
use N3XT0R\MigrationGenerator\Service\Generator\Definition;
43
use N3XT0R\MigrationGenerator\Service\Generator\Compiler\Mapper;
4+
use N3XT0R\MigrationGenerator\Service\Generator\Definition;
55

66
return [
77
'definitions' => [
@@ -32,4 +32,7 @@
3232
'requires' => ['table'],
3333
],
3434
],
35+
'normalizer' => [
36+
37+
],
3538
];

src/Providers/MigrationGeneratorServiceProvider.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ class MigrationGeneratorServiceProvider extends ServiceProvider
3030
*/
3131
public function boot(): void
3232
{
33-
$this->loadViewsFrom(__DIR__.'/../Stubs/', 'migration-generator');
33+
$this->loadViewsFrom(__DIR__ . '/../Stubs/', 'migration-generator');
3434
$this->publishes(
3535
[
36-
__DIR__.'/../Config/migration-generator.php' => config_path('migration-generator.php'),
36+
__DIR__ . '/../Config/migration-generator.php' => config_path('migration-generator.php'),
3737
],
3838
'migration-generator'
3939
);
@@ -46,7 +46,7 @@ public function boot(): void
4646
*/
4747
public function register(): void
4848
{
49-
$this->mergeConfigFrom(__DIR__.'/../Config/migration-generator.php', 'migration-generator');
49+
$this->mergeConfigFrom(__DIR__ . '/../Config/migration-generator.php', 'migration-generator');
5050
$this->registerParserFactory();
5151
$this->registerParser();
5252
$this->registerCompilerEngine();
@@ -97,12 +97,12 @@ function (Application $app, array $params = []) {
9797

9898
protected function getDefinitions(): array
9999
{
100-
return (array) app('config')->get('migration-generator.definitions');
100+
return (array)app('config')->get('migration-generator.definitions');
101101
}
102102

103103
protected function getMapper(): array
104104
{
105-
return (array) app('config')->get('migration-generator.mapper');
105+
return (array)app('config')->get('migration-generator.mapper');
106106
}
107107

108108
protected function registerDefinitionResolver(): void
@@ -118,7 +118,7 @@ protected function registerDefinitionResolver(): void
118118
static function (Application $app, array $params) use ($definitions) {
119119
$key = 'connection';
120120
if (!array_key_exists($key, $params)) {
121-
throw new \InvalidArgumentException('missing key '.$key.' in params.');
121+
throw new \InvalidArgumentException('missing key ' . $key . ' in params.');
122122
}
123123

124124
return new DefinitionResolver($params[$key], $definitions);
@@ -139,7 +139,7 @@ static function (Application $app, array $params) {
139139

140140
$key = 'connectionName';
141141
if (!array_key_exists($key, $params)) {
142-
throw new \InvalidArgumentException('missing key '.$key.' in params.');
142+
throw new \InvalidArgumentException('missing key ' . $key . ' in params.');
143143
}
144144

145145
/**
@@ -168,6 +168,10 @@ static function (Application $app, array $params) {
168168
);
169169
}
170170

171+
protected function registerNormalizer(): void
172+
{
173+
}
174+
171175
protected function registerCompilerEngine(): void
172176
{
173177
$this->app->bind(ReplaceEngine::class, ReplaceEngine::class);

src/Service/Generator/MigrationGenerator.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55

66
use N3XT0R\MigrationGenerator\Service\Generator\Compiler\MigrationCompilerInterface;
77
use N3XT0R\MigrationGenerator\Service\Generator\DTO\MigrationTimingDto;
8+
use N3XT0R\MigrationGenerator\Service\Generator\Normalization\SchemaNormalizationManagerInterface;
89
use N3XT0R\MigrationGenerator\Service\Generator\Resolver\DefinitionResolverInterface;
910

1011
class MigrationGenerator implements MigrationGeneratorInterface
1112
{
1213

1314
protected DefinitionResolverInterface $resolver;
1415
protected MigrationCompilerInterface $compiler;
16+
17+
protected ?SchemaNormalizationManagerInterface $normalizationManager = null;
1518
protected string $migrationDir = '';
1619
protected array $errorMessages = [];
1720
protected array $migrationFiles = [];
1821

1922
public function __construct(DefinitionResolverInterface $resolver, MigrationCompilerInterface $compiler)
2023
{
21-
$this->setMigrationDir(database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR);
24+
$this->setMigrationDir(database_path() . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR);
2225
$this->setResolver($resolver);
2326
$this->setCompiler($compiler);
2427
}
@@ -43,6 +46,16 @@ public function getCompiler(): MigrationCompilerInterface
4346
return $this->compiler;
4447
}
4548

49+
public function getNormalizationManager(): ?SchemaNormalizationManagerInterface
50+
{
51+
return $this->normalizationManager;
52+
}
53+
54+
public function setNormalizationManager(?SchemaNormalizationManagerInterface $normalizationManager): void
55+
{
56+
$this->normalizationManager = $normalizationManager;
57+
}
58+
4659
/**
4760
* @return string
4861
*/
@@ -52,7 +65,7 @@ public function getMigrationDir(): string
5265
}
5366

5467
/**
55-
* @param string $migrationDir
68+
* @param string $migrationDir
5669
*/
5770
public function setMigrationDir(string $migrationDir): void
5871
{
@@ -68,7 +81,7 @@ public function getErrorMessages(): array
6881
}
6982

7083
/**
71-
* @param array $errorMessages
84+
* @param array $errorMessages
7285
*/
7386
public function setErrorMessages(array $errorMessages): void
7487
{
@@ -89,7 +102,7 @@ public function getMigrationFiles(): array
89102
}
90103

91104
/**
92-
* @param array $migrationFiles
105+
* @param array $migrationFiles
93106
*/
94107
public function setMigrationFiles(array $migrationFiles): void
95108
{
@@ -99,7 +112,7 @@ public function setMigrationFiles(array $migrationFiles): void
99112
public function generateMigrationForTable(
100113
string $database,
101114
string $table,
102-
MigrationTimingDto $timingDto = new MigrationTimingDto()
115+
MigrationTimingDto $timingDto = new MigrationTimingDto(),
103116
): bool {
104117
$this->setErrorMessages([]);
105118
$result = false;
@@ -108,9 +121,15 @@ public function generateMigrationForTable(
108121

109122
try {
110123
$schemaResult = $resolver->resolveTableSchema($database, $table);
124+
125+
$normalizationManager = $this->getNormalizationManager();
126+
if ($normalizationManager) {
127+
$schemaResult = $normalizationManager->normalize($schemaResult);
128+
}
129+
111130
$compiler->generateByResult($schemaResult);
112131
$result = $compiler->writeToDisk(
113-
'Create'.ucfirst($table).'Table',
132+
'Create' . ucfirst($table) . 'Table',
114133
$this->getMigrationDir(),
115134
$timingDto,
116135
);

src/Service/Generator/MigrationGeneratorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public function __construct(DefinitionResolverInterface $resolver, MigrationComp
1515
public function setMigrationDir(string $migrationDir): void;
1616

1717
public function getMigrationDir(): string;
18-
18+
1919
public function generateMigrationForTable(
2020
string $database,
2121
string $table,
22-
MigrationTimingDto $timingDto
22+
MigrationTimingDto $timingDto,
2323
): bool;
2424

2525
public function getMigrationFiles(): array;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace N3XT0R\MigrationGenerator\Service\Generator\Normalization\Processors;
4+
5+
class ConstraintProcessor implements ProcessorInterface
6+
{
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace N3XT0R\MigrationGenerator\Service\Generator\Normalization\Processors;
4+
5+
class PivotProcessor implements ProcessorInterface
6+
{
7+
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace N3XT0R\MigrationGenerator\Service\Generator\Normalization\Processors;
4+
5+
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\ResultEntity;
6+
7+
interface ProcessorInterface
8+
{
9+
public function process(ResultEntity $result): ResultEntity;
10+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace N3XT0R\MigrationGenerator\Service\Generator\Normalization;
4+
5+
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\ResultEntity;
6+
use N3XT0R\MigrationGenerator\Service\Generator\Normalization\Processors\ProcessorInterface;
7+
8+
/**
9+
* Coordinates the execution of schema normalization processors.
10+
*
11+
* Each Processor implements a specific part of the normalization pipeline
12+
* and is executed in a fixed order.
13+
*
14+
* This manager ensures that the normalization process is consistent
15+
* and reusable, while remaining open for extension.
16+
*/
17+
class SchemaNormalizationManager implements SchemaNormalizationManagerInterface
18+
{
19+
/**
20+
* @var ProcessorInterface[]
21+
*/
22+
protected iterable $processors = [];
23+
24+
/**
25+
* SchemaNormalizationManager constructor.
26+
*
27+
* @param iterable|ProcessorInterface[] $processors
28+
*/
29+
public function __construct(iterable $processors = [])
30+
{
31+
foreach ($processors as $processor) {
32+
$this->addProcessor($processor);
33+
}
34+
}
35+
36+
/**
37+
* Adds a new processor to the normalization chain.
38+
*/
39+
public function addProcessor(ProcessorInterface|iterable $processor): void
40+
{
41+
$this->processors[] = $processor;
42+
}
43+
44+
public function getProcessors(): iterable
45+
{
46+
return $this->processors;
47+
}
48+
49+
public function setProcessors(iterable $processors = []): void
50+
{
51+
$this->processors = $processors;
52+
}
53+
54+
/**
55+
* Executes all processors on the given schema result.
56+
*
57+
* @param ResultEntity $result
58+
* @return ResultEntity
59+
*/
60+
public function normalize(ResultEntity $result): ResultEntity
61+
{
62+
$processors = $this->getProcessors();
63+
foreach ($processors as $processor) {
64+
$result = $processor->process($result);
65+
}
66+
67+
return $result;
68+
}
69+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace N3XT0R\MigrationGenerator\Service\Generator\Normalization;
4+
5+
use N3XT0R\MigrationGenerator\Service\Generator\Definition\Entity\ResultEntity;
6+
use N3XT0R\MigrationGenerator\Service\Generator\Normalization\Processors\ProcessorInterface;
7+
8+
interface SchemaNormalizationManagerInterface
9+
{
10+
public function __construct(iterable $processors = []);
11+
12+
13+
public function addProcessor(ProcessorInterface $processor): void;
14+
15+
public function normalize(ResultEntity $result): mixed;
16+
17+
public function getProcessors(): iterable;
18+
19+
public function setProcessors(iterable $processors = []): void;
20+
}

0 commit comments

Comments
 (0)