|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheCodingMachine\TDBM; |
|
5
|
|
|
|
|
6
|
|
|
use BrainDiminished\SchemaVersionControl\SchemaVersionControlService; |
|
7
|
|
|
use Doctrine\Common\Cache\Cache; |
|
8
|
|
|
use Doctrine\DBAL\Connection; |
|
9
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
10
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
11
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
12
|
|
|
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; |
|
13
|
|
|
use TheCodingMachine\TDBM\Utils\ImmutableCaster; |
|
14
|
|
|
use function array_map; |
|
15
|
|
|
use function array_values; |
|
16
|
|
|
use function file_exists; |
|
17
|
|
|
use function hash; |
|
18
|
|
|
use function implode; |
|
19
|
|
|
use function in_array; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Load / save schema in the tdbm.lock file. |
|
23
|
|
|
*/ |
|
24
|
|
|
class SchemaLockFileDumper |
|
25
|
|
|
{ |
|
26
|
|
|
private $lockFilePath; |
|
27
|
|
|
private $connection; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var Schema |
|
31
|
|
|
*/ |
|
32
|
|
|
private $schema; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $cachePrefix; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Cache |
|
41
|
|
|
*/ |
|
42
|
|
|
private $cache; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var SchemaVersionControlService |
|
46
|
|
|
*/ |
|
47
|
|
|
private $schemaVersionControlService; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param Connection $connection The DBAL DB connection to use |
|
51
|
|
|
* @param Cache $cache A cache service to be used |
|
52
|
|
|
* @param string $lockFilePath The path for the lock file which will store the database schema |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(Connection $connection, Cache $cache, string $lockFilePath) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->connection = $connection; |
|
57
|
|
|
$this->cache = $cache; |
|
58
|
|
|
$this->lockFilePath = $lockFilePath; |
|
59
|
|
|
$this->schemaVersionControlService = new SchemaVersionControlService($this->connection, $this->lockFilePath); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns a unique ID for the current connection. Useful for namespacing cache entries in the current connection. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getCachePrefix(): string |
|
68
|
|
|
{ |
|
69
|
|
|
if ($this->cachePrefix === null) { |
|
70
|
|
|
$this->cachePrefix = hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName()); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->cachePrefix; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getLockFilePath(): string |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->lockFilePath; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns the (cached) schema. |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getSchema(bool $ignoreCache = false): Schema |
|
85
|
|
|
{ |
|
86
|
|
|
if ($this->schema === null) { |
|
87
|
|
|
$cacheKey = $this->getCachePrefix().'_immutable_schema'; |
|
88
|
|
|
if (!$ignoreCache && $this->cache->contains($cacheKey)) { |
|
89
|
|
|
$this->schema = $this->cache->fetch($cacheKey); |
|
90
|
|
|
} elseif (!file_exists($this->getLockFilePath())) { |
|
91
|
|
|
throw new TDBMException('No tdbm lock file found. Please regenerate DAOs and Beans.'); |
|
92
|
|
|
} else { |
|
93
|
|
|
$this->schema = $this->schemaVersionControlService->loadSchemaFile(); |
|
94
|
|
|
ImmutableCaster::castSchemaToImmutable($this->schema); |
|
95
|
|
|
$this->cache->save($cacheKey, $this->schema); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->schema; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function generateLockFile(): void |
|
103
|
|
|
{ |
|
104
|
|
|
$this->schemaVersionControlService->dumpSchema(); |
|
105
|
|
|
\chmod($this->getLockFilePath(), 0664); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|