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