|
1
|
|
|
<?php |
|
2
|
|
|
/******************************************************************************* |
|
3
|
|
|
* This file is part of the GraphQL Bundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) YnloUltratech <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
******************************************************************************/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Test\FixtureLoader\SchemaUpdater; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\Bundle\DoctrineBundle\Registry; |
|
14
|
|
|
use Doctrine\DBAL\Connection; |
|
15
|
|
|
use Doctrine\DBAL\Driver; |
|
16
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
|
17
|
|
|
|
|
18
|
|
|
class ORMSQLite implements SchemaUpdaterInterface |
|
19
|
|
|
{ |
|
20
|
|
|
protected $cacheDir; |
|
21
|
|
|
|
|
22
|
22 |
|
public function __construct(string $cacheDir = null) |
|
23
|
|
|
{ |
|
24
|
22 |
|
$this->cacheDir = $cacheDir; |
|
25
|
22 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function supports(Registry $registry) |
|
31
|
|
|
{ |
|
32
|
1 |
|
return $registry->getName() === 'ORM' |
|
33
|
1 |
|
&& ($connection = $registry->getConnection()) |
|
34
|
1 |
|
&& $connection instanceof Connection |
|
35
|
1 |
|
&& $connection->getDriver() instanceof Driver\AbstractSQLiteDriver; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function updateSchema(Registry $registry) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$params = $registry->getConnection()->getParams(); |
|
44
|
1 |
|
if (isset($params['master'])) { |
|
45
|
|
|
$params = $params['master']; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$name = $params['path'] ?? ($params['dbname'] ?? false); |
|
49
|
1 |
|
if (!$name) { |
|
50
|
|
|
throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped."); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
$om = $registry->getManager(); |
|
54
|
1 |
|
static $metadata = null; |
|
55
|
1 |
|
static $metadataHash = null; |
|
56
|
1 |
|
if (!$metadata) { |
|
57
|
1 |
|
$metadata = $om->getMetadataFactory()->getAllMetadata(); |
|
58
|
1 |
|
$metadataHash = md5(serialize($metadata)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
if ($this->cacheDir && $this->cacheExist($metadataHash)) { |
|
62
|
|
|
copy($this->getCacheFile($metadataHash), $name); |
|
63
|
|
|
|
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
$schemaTool = new SchemaTool($om); |
|
68
|
1 |
|
$schemaTool->dropDatabase(); |
|
69
|
1 |
|
if (!empty($metadata)) { |
|
70
|
1 |
|
$schemaTool->createSchema($metadata); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
if ($this->cacheDir) { |
|
74
|
1 |
|
copy($name, $this->getCacheFile($metadataHash)); |
|
75
|
|
|
} |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
public function getCacheFile($hash): string |
|
79
|
|
|
{ |
|
80
|
1 |
|
return $this->cacheDir.DIRECTORY_SEPARATOR.$hash.'.schema'; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
public function cacheExist($hash): bool |
|
84
|
|
|
{ |
|
85
|
1 |
|
return file_exists($this->getCacheFile($hash)); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|