|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\Schema\Conveyor; |
|
6
|
|
|
|
|
7
|
|
|
use Cycle\Schema\Generator; |
|
8
|
|
|
use Cycle\Schema\GeneratorInterface; |
|
9
|
|
|
use Psr\Container\ContainerInterface; |
|
10
|
|
|
use Yiisoft\Yii\Cycle\Schema\SchemaConveyorInterface; |
|
11
|
|
|
use Yiisoft\Yii\Cycle\Exception\BadGeneratorDeclarationException; |
|
12
|
|
|
|
|
13
|
|
|
class SchemaConveyor implements SchemaConveyorInterface |
|
14
|
|
|
{ |
|
15
|
|
|
protected array $conveyor = [ |
|
16
|
|
|
self::STAGE_INDEX => [ |
|
17
|
|
|
Generator\ResetTables::class, // re-declared table schemas (remove columns) |
|
18
|
|
|
], |
|
19
|
|
|
self::STAGE_RENDER => [ |
|
20
|
|
|
Generator\GenerateRelations::class, // generate entity relations |
|
21
|
|
|
Generator\ValidateEntities::class, // make sure all entity schemas are correct |
|
22
|
|
|
Generator\RenderTables::class, // declare table schemas |
|
23
|
|
|
Generator\RenderRelations::class, // declare relation keys and indexes |
|
24
|
|
|
], |
|
25
|
|
|
self::STAGE_USERLAND => [], |
|
26
|
|
|
self::STAGE_POSTPROCESS => [ |
|
27
|
|
|
Generator\GenerateTypecast::class // typecast non string columns |
|
28
|
|
|
], |
|
29
|
|
|
]; |
|
30
|
|
|
|
|
31
|
|
|
protected ContainerInterface $container; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(ContainerInterface $container) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->container = $container; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function addGenerator(string $stage, $generator): void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->conveyor[$stage][] = $generator; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getGenerators(): array |
|
44
|
|
|
{ |
|
45
|
|
|
$result = []; |
|
46
|
|
|
foreach ($this->conveyor as $group) { |
|
47
|
|
|
foreach ($group as $generatorDefinition) { |
|
48
|
|
|
$generator = null; |
|
49
|
|
|
if (is_string($generatorDefinition)) { |
|
50
|
|
|
$generator = $this->container->get($generatorDefinition); |
|
51
|
|
|
} elseif (is_object($generatorDefinition) && $generatorDefinition instanceof GeneratorInterface) { |
|
52
|
|
|
$result[] = $generatorDefinition; |
|
53
|
|
|
continue; |
|
54
|
|
|
} elseif (is_object($generatorDefinition) && method_exists($generatorDefinition, '__invoke')) { |
|
55
|
|
|
$generator = $generatorDefinition($this->container); |
|
56
|
|
|
} |
|
57
|
|
|
if ($generator instanceof GeneratorInterface) { |
|
58
|
|
|
$result[] = $generator; |
|
59
|
|
|
continue; |
|
60
|
|
|
} |
|
61
|
|
|
throw new BadGeneratorDeclarationException(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
return $result; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|