|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Yii\Cycle\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Cycle\Migrations\GenerateMigrations; |
|
7
|
|
|
use Cycle\Schema\Compiler; |
|
8
|
|
|
use Cycle\Schema\GeneratorInterface; |
|
9
|
|
|
use Cycle\Schema\Registry; |
|
10
|
|
|
use Spiral\Database\DatabaseManager; |
|
11
|
|
|
use Spiral\Migrations\Config\MigrationConfig; |
|
12
|
|
|
use Spiral\Migrations\Migrator; |
|
13
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
14
|
|
|
use Yiisoft\Yii\Cycle\SchemaConveyorInterface; |
|
15
|
|
|
|
|
16
|
|
|
class CycleOrmHelper |
|
17
|
|
|
{ |
|
18
|
|
|
private DatabaseManager $dbal; |
|
19
|
|
|
|
|
20
|
|
|
private CacheInterface $cache; |
|
21
|
|
|
|
|
22
|
|
|
private string $cacheKey = 'Cycle-ORM-Schema'; |
|
23
|
|
|
|
|
24
|
|
|
private SchemaConveyorInterface $schemaConveyor; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
DatabaseManager $dbal, |
|
28
|
|
|
CacheInterface $cache, |
|
29
|
|
|
SchemaConveyorInterface $schemaConveyor |
|
30
|
|
|
) { |
|
31
|
|
|
$this->dbal = $dbal; |
|
32
|
|
|
$this->cache = $cache; |
|
33
|
|
|
$this->schemaConveyor = $schemaConveyor; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function dropCurrentSchemaCache(): void |
|
37
|
|
|
{ |
|
38
|
|
|
$this->cache->delete($this->cacheKey); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Migrator $migrator |
|
43
|
|
|
* @param MigrationConfig $config |
|
44
|
|
|
* @param GeneratorInterface[]|string[]|Closure[] $generators Additional generators |
|
45
|
|
|
* @throws \Yiisoft\Yii\Cycle\Exception\BadGeneratorDeclarationException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function generateMigrations(Migrator $migrator, MigrationConfig $config, array $generators = []): void |
|
48
|
|
|
{ |
|
49
|
|
|
// add migrations generator |
|
50
|
|
|
$migrate = new GenerateMigrations($migrator->getRepository(), $config); |
|
51
|
|
|
$this->schemaConveyor->addGenerator($this->schemaConveyor::STAGE_USERLAND, $migrate); |
|
52
|
|
|
// add custom generators |
|
53
|
|
|
foreach ($generators as $generator) { |
|
54
|
|
|
$this->schemaConveyor->addGenerator($this->schemaConveyor::STAGE_USERLAND, $generator); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$conveyor = $this->schemaConveyor->getGenerators(); |
|
58
|
|
|
|
|
59
|
|
|
(new Compiler())->compile(new Registry($this->dbal), $conveyor); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param bool $fromCache |
|
64
|
|
|
* @param GeneratorInterface[]|string[]|Closure[] $generators Additional generators |
|
65
|
|
|
* @return array |
|
66
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
67
|
|
|
* @throws \Yiisoft\Yii\Cycle\Exception\BadGeneratorDeclarationException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getCurrentSchemaArray($fromCache = true, array $generators = []): array |
|
70
|
|
|
{ |
|
71
|
|
|
if ($fromCache) { |
|
72
|
|
|
$schema = $this->cache->get($this->cacheKey); |
|
73
|
|
|
if (is_array($schema)) { |
|
74
|
|
|
return $schema; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
// add generators to userland |
|
78
|
|
|
foreach ($generators as $generator) { |
|
79
|
|
|
$this->schemaConveyor->addGenerator($this->schemaConveyor::STAGE_USERLAND, $generator); |
|
80
|
|
|
} |
|
81
|
|
|
// compile schema array |
|
82
|
|
|
$conveyor = $this->schemaConveyor->getGenerators(); |
|
83
|
|
|
|
|
84
|
|
|
$schema = (new Compiler())->compile(new Registry($this->dbal), $conveyor); |
|
85
|
|
|
|
|
86
|
|
|
$this->cache->set($this->cacheKey, $schema); |
|
87
|
|
|
return $schema; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function setCacheKey(string $cacheKey): void |
|
91
|
|
|
{ |
|
92
|
|
|
$this->cacheKey = $cacheKey; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|