1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Cycle\Schema\Generator\Migrations\Strategy; |
||
6 | |||
7 | use Cycle\Database\Schema\AbstractTable; |
||
8 | use Cycle\Migrations\Atomizer\Atomizer; |
||
9 | use Cycle\Migrations\Atomizer\Renderer; |
||
10 | use Cycle\Migrations\Atomizer\TableSorter; |
||
11 | use Cycle\Migrations\Config\MigrationConfig; |
||
12 | use Cycle\Schema\Generator\Migrations\MigrationImage; |
||
13 | use Cycle\Schema\Generator\Migrations\NameGeneratorInterface; |
||
14 | |||
15 | final class MultipleFilesStrategy implements GeneratorStrategyInterface |
||
16 | { |
||
17 | private static int $sec = 0; |
||
18 | |||
19 | public function __construct( |
||
20 | private readonly MigrationConfig $migrationConfig, |
||
21 | private readonly NameGeneratorInterface $nameGenerator, |
||
22 | private readonly TableSorter $tableSorter = new TableSorter(), |
||
23 | ) {} |
||
24 | |||
25 | /** |
||
26 | * @param non-empty-string $database |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
27 | * @param array<AbstractTable> $tables |
||
28 | * |
||
29 | * @return array<MigrationImage> |
||
30 | */ |
||
31 | public function generate(string $database, array $tables): array |
||
32 | { |
||
33 | $atomizer = new Atomizer(new Renderer()); |
||
34 | |||
35 | $images = []; |
||
36 | foreach ($this->tableSorter->sort($tables) as $table) { |
||
37 | if ($table->getComparator()->hasChanges()) { |
||
38 | $atomizer->setTables([$table]); |
||
39 | |||
40 | $image = new MigrationImage($this->migrationConfig, $database); |
||
41 | $image->setName($this->nameGenerator->generate($atomizer)); |
||
42 | $image->fileNamePattern = self::$sec++ . '_{database}_{name}'; |
||
43 | |||
44 | $atomizer->declareChanges($image->getClass()->getMethod('up')); |
||
45 | $atomizer->revertChanges($image->getClass()->getMethod('down')); |
||
46 | |||
47 | $images[] = $image; |
||
48 | } |
||
49 | } |
||
50 | |||
51 | return $images; |
||
52 | } |
||
53 | } |
||
54 |