forked from nextras/migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructureDiffGenerator.php
More file actions
82 lines (61 loc) · 1.81 KB
/
StructureDiffGenerator.php
File metadata and controls
82 lines (61 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php declare(strict_types = 1);
/**
* This file is part of the Nextras community extensions of Nette Framework
*
* @license New BSD License
* @link https://github.com/nextras/migrations
*/
namespace Nextras\Migrations\Bridges\DoctrineOrm;
use Doctrine\Common\Cache\ClearableCache;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Nextras;
use Nextras\Migrations\IDiffGenerator;
class StructureDiffGenerator implements IDiffGenerator
{
/** @var EntityManagerInterface */
private $entityManager;
/** @var string|null absolute path to a file */
private $ignoredQueriesFile;
public function __construct(EntityManagerInterface $entityManager, ?string $ignoredQueriesFile = null)
{
$this->entityManager = $entityManager;
$this->ignoredQueriesFile = $ignoredQueriesFile;
}
public function getExtension(): string
{
return 'sql';
}
public function generateContent(): string
{
$queries = array_diff($this->getUpdateQueries(), $this->getIgnoredQueries());
$content = $queries ? (implode(";\n", $queries) . ";\n") : '';
return $content;
}
/**
* @return list<string>
*/
protected function getUpdateQueries(): array
{
$cache = $this->entityManager->getConfiguration()->getMetadataCache();
if ($cache !== null) {
$cache->clear();
}
$schemaTool = new SchemaTool($this->entityManager);
$metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
$queries = $schemaTool->getUpdateSchemaSql($metadata, true);
return $queries;
}
/**
* @return list<string>
*/
protected function getIgnoredQueries(): array
{
if ($this->ignoredQueriesFile === null) {
return [];
}
$content = file_get_contents($this->ignoredQueriesFile);
$queries = preg_split('~(\s*;\s*\r?\n|\z)~', $content, -1, PREG_SPLIT_NO_EMPTY);
return $queries;
}
}