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