1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the webmozart/json package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webmozart\Json\Migration; |
13
|
|
|
|
14
|
|
|
use stdClass; |
15
|
|
|
use Webmozart\Assert\Assert; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Migrates a JSON object between different versions. |
19
|
|
|
* |
20
|
|
|
* The JSON object is expected to have the property "version" set. |
21
|
|
|
* |
22
|
|
|
* @since 1.3 |
23
|
|
|
* |
24
|
|
|
* @author Bernhard Schussek <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class MigrationManager |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var JsonMigration[] |
30
|
|
|
*/ |
31
|
|
|
private $migrationsBySourceVersion = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var JsonMigration[] |
35
|
|
|
*/ |
36
|
|
|
private $migrationsByTargetVersion = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string[] |
40
|
|
|
*/ |
41
|
|
|
private $knownVersions; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates a new migration manager. |
45
|
|
|
* |
46
|
|
|
* @param JsonMigration[] $migrations The migrations migrating a JSON object |
47
|
|
|
* between individual versions. |
48
|
|
|
*/ |
49
|
10 |
|
public function __construct(array $migrations) |
50
|
|
|
{ |
51
|
10 |
|
Assert::allIsInstanceOf($migrations, __NAMESPACE__.'\JsonMigration'); |
52
|
|
|
|
53
|
10 |
|
foreach ($migrations as $migration) { |
54
|
10 |
|
$this->migrationsBySourceVersion[$migration->getSourceVersion()] = $migration; |
55
|
10 |
|
$this->migrationsByTargetVersion[$migration->getTargetVersion()] = $migration; |
56
|
10 |
|
$this->knownVersions[] = $migration->getSourceVersion(); |
57
|
10 |
|
$this->knownVersions[] = $migration->getTargetVersion(); |
58
|
|
|
} |
59
|
|
|
|
60
|
10 |
|
$this->knownVersions = array_unique($this->knownVersions); |
61
|
|
|
|
62
|
10 |
|
uksort($this->migrationsBySourceVersion, 'version_compare'); |
63
|
10 |
|
uksort($this->migrationsByTargetVersion, 'version_compare'); |
64
|
10 |
|
usort($this->knownVersions, 'version_compare'); |
65
|
10 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Migrates a JSON object to the given version. |
69
|
|
|
* |
70
|
|
|
* @param stdClass $data The JSON object. |
71
|
|
|
* @param string $targetVersion The version string. |
72
|
|
|
* |
73
|
|
|
*/ |
74
|
9 |
|
public function migrate(stdClass $data, $targetVersion) |
75
|
|
|
{ |
76
|
9 |
|
if (version_compare($targetVersion, $data->version, '>')) { |
77
|
4 |
|
$this->up($data, $targetVersion); |
78
|
5 |
|
} elseif (version_compare($targetVersion, $data->version, '<')) { |
79
|
4 |
|
$this->down($data, $targetVersion); |
80
|
|
|
} |
81
|
5 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns all versions known to the manager. |
85
|
|
|
* |
86
|
|
|
* @return string[] The known version strings. |
87
|
|
|
*/ |
88
|
1 |
|
public function getKnownVersions() |
89
|
|
|
{ |
90
|
1 |
|
return $this->knownVersions; |
91
|
|
|
} |
92
|
|
|
|
93
|
4 |
View Code Duplication |
private function up($data, $targetVersion) |
|
|
|
|
94
|
|
|
{ |
95
|
4 |
|
while (version_compare($data->version, $targetVersion, '<')) { |
96
|
4 |
|
if (!isset($this->migrationsBySourceVersion[$data->version])) { |
97
|
1 |
|
throw new MigrationException(sprintf( |
98
|
1 |
|
'No migration found to upgrade from version %s to %s.', |
99
|
1 |
|
$data->version, |
100
|
|
|
$targetVersion |
101
|
|
|
)); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
$migration = $this->migrationsBySourceVersion[$data->version]; |
105
|
|
|
|
106
|
|
|
// Final version too high |
107
|
3 |
|
if (version_compare($migration->getTargetVersion(), $targetVersion, '>')) { |
108
|
1 |
|
throw new MigrationException(sprintf( |
109
|
1 |
|
'No migration found to upgrade from version %s to %s.', |
110
|
1 |
|
$data->version, |
111
|
|
|
$targetVersion |
112
|
|
|
)); |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
$migration->up($data); |
116
|
|
|
|
117
|
3 |
|
$data->version = $migration->getTargetVersion(); |
118
|
|
|
} |
119
|
2 |
|
} |
120
|
|
|
|
121
|
4 |
View Code Duplication |
private function down($data, $targetVersion) |
|
|
|
|
122
|
|
|
{ |
123
|
4 |
|
while (version_compare($data->version, $targetVersion, '>')) { |
124
|
4 |
|
if (!isset($this->migrationsByTargetVersion[$data->version])) { |
125
|
1 |
|
throw new MigrationException(sprintf( |
126
|
1 |
|
'No migration found to downgrade from version %s to %s.', |
127
|
1 |
|
$data->version, |
128
|
|
|
$targetVersion |
129
|
|
|
)); |
130
|
|
|
} |
131
|
|
|
|
132
|
3 |
|
$migration = $this->migrationsByTargetVersion[$data->version]; |
133
|
|
|
|
134
|
|
|
// Final version too low |
135
|
3 |
|
if (version_compare($migration->getSourceVersion(), $targetVersion, '<')) { |
136
|
1 |
|
throw new MigrationException(sprintf( |
137
|
1 |
|
'No migration found to downgrade from version %s to %s.', |
138
|
1 |
|
$data->version, |
139
|
|
|
$targetVersion |
140
|
|
|
)); |
141
|
|
|
} |
142
|
|
|
|
143
|
3 |
|
$migration->down($data); |
144
|
|
|
|
145
|
3 |
|
$data->version = $migration->getSourceVersion(); |
146
|
|
|
} |
147
|
2 |
|
} |
148
|
|
|
} |
149
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.