1 | <?php |
||||
2 | |||||
3 | namespace TheCodingMachine\TDBM\SchemaVersionControl; |
||||
4 | |||||
5 | use Doctrine\DBAL\Connection; |
||||
6 | use Doctrine\DBAL\Schema\Schema; |
||||
7 | use Symfony\Component\Yaml\Yaml; |
||||
8 | |||||
9 | /** |
||||
10 | * SchemaVersionControlService is the main class of this library. It provides useful methods for managing a database |
||||
11 | * schema, whether to save the current and share current configuration in YAML format, or update the schema to match |
||||
12 | * a new configuration. |
||||
13 | */ |
||||
14 | class SchemaVersionControlService |
||||
15 | { |
||||
16 | /** @var Connection */ |
||||
17 | private $connection; |
||||
18 | |||||
19 | /** @var string */ |
||||
20 | private $schemaFile; |
||||
21 | |||||
22 | /** |
||||
23 | * SchemaVersionControlService constructor. |
||||
24 | * @param Connection $connection |
||||
25 | * @param string $schemaFile Path to config file containing top object `schema`. |
||||
26 | */ |
||||
27 | public function __construct(Connection $connection, string $schemaFile) |
||||
28 | { |
||||
29 | $this->connection = $connection; |
||||
30 | $this->schemaFile = $schemaFile; |
||||
31 | } |
||||
32 | |||||
33 | /** |
||||
34 | * Get the current schema used in database. |
||||
35 | * @return Schema |
||||
36 | */ |
||||
37 | public function getCurrentSchema(): Schema |
||||
38 | { |
||||
39 | return $this->connection->getSchemaManager()->createSchema(); |
||||
0 ignored issues
–
show
The function
Doctrine\DBAL\Schema\Abs...Manager::createSchema() has been deprecated: Use {@link introspectSchema()} instead.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
40 | } |
||||
41 | |||||
42 | /** |
||||
43 | * Load schema from config file. |
||||
44 | * @return Schema |
||||
45 | */ |
||||
46 | public function loadSchemaFile(): Schema |
||||
47 | { |
||||
48 | if (!file_exists($this->schemaFile)) { |
||||
49 | return new Schema(); |
||||
50 | } |
||||
51 | |||||
52 | $content = file_get_contents($this->schemaFile); |
||||
53 | $desc = Yaml::parse($content); |
||||
54 | if (empty($desc)) { |
||||
55 | return new Schema(); |
||||
56 | } |
||||
57 | |||||
58 | $builder = new SchemaBuilder(); |
||||
59 | return $builder->build($desc['schema']); |
||||
60 | } |
||||
61 | |||||
62 | /** |
||||
63 | * Write current database schema in config file |
||||
64 | */ |
||||
65 | public function dumpSchema(): void |
||||
66 | { |
||||
67 | $schema = $this->getCurrentSchema(); |
||||
68 | $normalizer = new SchemaNormalizer(); |
||||
69 | $desc = $normalizer->normalize($schema); |
||||
70 | $yamlSchema = Yaml::dump(['schema' => $desc], 10, 2); |
||||
71 | $directory = dirname($this->schemaFile); |
||||
72 | if (!file_exists($directory)) { |
||||
73 | if (mkdir($directory, 0666, true) === false) { |
||||
74 | throw new \RuntimeException('Could not create directory '.$directory); |
||||
75 | } |
||||
76 | } |
||||
77 | if (file_put_contents($this->schemaFile, $yamlSchema) === false) { |
||||
78 | throw new \RuntimeException('Could not edit dump file '.$this->schemaFile); |
||||
79 | } |
||||
80 | } |
||||
81 | } |
||||
82 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.