1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\Schema\Provider; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Cycle\Database\DatabaseProviderInterface; |
9
|
|
|
use Cycle\Schema\Compiler; |
10
|
|
|
use Cycle\Schema\GeneratorInterface; |
11
|
|
|
use Cycle\Schema\Provider\SchemaProviderInterface; |
12
|
|
|
use Cycle\Schema\Registry; |
13
|
|
|
use Yiisoft\Yii\Cycle\Schema\SchemaConveyorInterface; |
14
|
|
|
|
15
|
|
|
final class FromConveyorSchemaProvider implements SchemaProviderInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Additional generators when reading Schema |
19
|
|
|
* |
20
|
|
|
* @var Closure[]|GeneratorInterface[]|string[] |
21
|
|
|
*/ |
22
|
|
|
private array $generators = []; |
23
|
|
|
|
24
|
8 |
|
public function __construct( |
25
|
|
|
private SchemaConveyorInterface $conveyor, |
26
|
|
|
private DatabaseProviderInterface $dbal, |
27
|
|
|
) { |
28
|
8 |
|
} |
29
|
|
|
|
30
|
4 |
|
public function withConfig(array $config): self |
31
|
|
|
{ |
32
|
4 |
|
$new = clone $this; |
33
|
4 |
|
$new->generators = $config['generators'] ?? []; |
34
|
4 |
|
return $new; |
35
|
|
|
} |
36
|
|
|
|
37
|
5 |
|
public function read(?SchemaProviderInterface $nextProvider = null): ?array |
38
|
|
|
{ |
39
|
5 |
|
$generators = $this->getGenerators(); |
40
|
5 |
|
$schema = (new Compiler())->compile(new Registry($this->dbal), $generators); |
41
|
|
|
|
42
|
5 |
|
return count($schema) !== 0 || $nextProvider === null ? $schema : $nextProvider->read(); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function clear(): bool |
46
|
|
|
{ |
47
|
1 |
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
5 |
|
private function getGenerators(): array |
51
|
|
|
{ |
52
|
5 |
|
$conveyor = clone $this->conveyor; |
53
|
|
|
// add generators to userland stage |
54
|
5 |
|
foreach ($this->generators as $generator) { |
55
|
1 |
|
$conveyor->addGenerator(SchemaConveyorInterface::STAGE_USERLAND, $generator); |
56
|
|
|
} |
57
|
5 |
|
return $conveyor->getGenerators(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|