|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\ComposerConfigManager\Application\Updater; |
|
3
|
|
|
|
|
4
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Configuration; |
|
5
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationFile; |
|
6
|
|
|
|
|
7
|
|
|
class ConfigurationFileUpdater |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var PlainValueUpdater */ |
|
10
|
|
|
private $plainValueUpdater; |
|
11
|
|
|
/** @var KeywordListUpdater */ |
|
12
|
|
|
private $keywordListUpdater; |
|
13
|
|
|
/** @var ListUpdater */ |
|
14
|
|
|
private $listUpdater; |
|
15
|
|
|
/** @var AuthorListUpdater */ |
|
16
|
|
|
private $authorListUpdater; |
|
17
|
|
|
|
|
18
|
1 |
|
public function __construct( |
|
19
|
|
|
PlainValueUpdater $plainValueUpdater, |
|
20
|
|
|
KeywordListUpdater $keywordListUpdater, |
|
21
|
|
|
ListUpdater $listUpdater, |
|
22
|
|
|
AuthorListUpdater $authorListUpdater |
|
23
|
|
|
) { |
|
24
|
1 |
|
$this->plainValueUpdater = $plainValueUpdater; |
|
25
|
1 |
|
$this->keywordListUpdater = $keywordListUpdater; |
|
26
|
1 |
|
$this->listUpdater = $listUpdater; |
|
27
|
1 |
|
$this->authorListUpdater = $authorListUpdater; |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ConfigurationFile[] $configurationFileList |
|
32
|
|
|
* |
|
33
|
|
|
* @return ConfigurationFile |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function update(array $configurationFileList) |
|
36
|
|
|
{ |
|
37
|
1 |
|
$newConfigurationFile = array_pop($configurationFileList); |
|
38
|
|
|
|
|
39
|
1 |
|
while (count($configurationFileList) > 0) { |
|
40
|
1 |
|
$baseConfigurationFile = array_pop($configurationFileList); |
|
41
|
1 |
|
$newConfigurationFile = $this->merge($baseConfigurationFile, $newConfigurationFile); |
|
|
|
|
|
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
return $newConfigurationFile; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param ConfigurationFile $baseConfigurationFile |
|
50
|
|
|
* @param ConfigurationFile $newConfigurationFile |
|
51
|
|
|
* |
|
52
|
|
|
* @return ConfigurationFile |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function merge(ConfigurationFile $baseConfigurationFile, ConfigurationFile $newConfigurationFile) |
|
55
|
|
|
{ |
|
56
|
1 |
|
return new ConfigurationFile( |
|
57
|
1 |
|
$this->mergeConfiguration( |
|
58
|
1 |
|
$baseConfigurationFile->getConfiguration(), |
|
59
|
1 |
|
$newConfigurationFile->getConfiguration() |
|
60
|
1 |
|
), |
|
61
|
1 |
|
$this->mergeKeyList( |
|
62
|
1 |
|
$baseConfigurationFile->getKeyList(), |
|
63
|
1 |
|
$newConfigurationFile->getKeyList() |
|
64
|
1 |
|
) |
|
65
|
1 |
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
protected function mergeConfiguration(Configuration $baseConfiguration, Configuration $newConfiguration) |
|
69
|
|
|
{ |
|
70
|
1 |
|
return new Configuration( |
|
71
|
1 |
|
$this->plainValueUpdater->update( |
|
72
|
1 |
|
$newConfiguration->getPackageName(), |
|
73
|
1 |
|
$baseConfiguration->getPackageName() |
|
74
|
1 |
|
), |
|
75
|
1 |
|
$this->plainValueUpdater->update( |
|
76
|
1 |
|
$newConfiguration->getType(), |
|
77
|
1 |
|
$baseConfiguration->getType() |
|
78
|
1 |
|
), |
|
79
|
1 |
|
$this->plainValueUpdater->update( |
|
80
|
1 |
|
$newConfiguration->getLicense(), |
|
81
|
1 |
|
$baseConfiguration->getLicense() |
|
82
|
1 |
|
), |
|
83
|
1 |
|
$this->plainValueUpdater->update( |
|
84
|
1 |
|
$newConfiguration->getPackageVersion(), |
|
85
|
1 |
|
$baseConfiguration->getPackageVersion() |
|
86
|
1 |
|
), |
|
87
|
1 |
|
$this->plainValueUpdater->update( |
|
88
|
1 |
|
$newConfiguration->getDescription(), |
|
89
|
1 |
|
$baseConfiguration->getDescription() |
|
90
|
1 |
|
), |
|
91
|
1 |
|
$this->keywordListUpdater->update( |
|
92
|
1 |
|
$newConfiguration->getKeywordList(), |
|
93
|
1 |
|
$baseConfiguration->getKeywordList() |
|
94
|
1 |
|
), |
|
95
|
1 |
|
$this->authorListUpdater->update($newConfiguration->getAuthorList(), $baseConfiguration->getAuthorList()), |
|
96
|
1 |
|
$this->listUpdater->update( |
|
97
|
1 |
|
$newConfiguration->getProvidedPackageList(), |
|
98
|
1 |
|
$baseConfiguration->getProvidedPackageList() |
|
99
|
1 |
|
), |
|
100
|
1 |
|
$this->listUpdater->update( |
|
101
|
1 |
|
$newConfiguration->getSuggestedPackageList(), |
|
102
|
1 |
|
$baseConfiguration->getSuggestedPackageList() |
|
103
|
1 |
|
), |
|
104
|
1 |
|
$this->listUpdater->update($newConfiguration->getSupportList(), $baseConfiguration->getSupportList()), |
|
105
|
1 |
|
$this->listUpdater->update($newConfiguration->getAutoloadList(), $baseConfiguration->getAutoloadList()), |
|
106
|
1 |
|
$this->listUpdater->update( |
|
107
|
1 |
|
$newConfiguration->getAutoloadDevList(), |
|
108
|
1 |
|
$baseConfiguration->getAutoloadDevList() |
|
109
|
1 |
|
), |
|
110
|
1 |
|
$this->listUpdater->update( |
|
111
|
1 |
|
$newConfiguration->getRequiredPackageList(), |
|
112
|
1 |
|
$baseConfiguration->getRequiredPackageList() |
|
113
|
1 |
|
), |
|
114
|
1 |
|
$this->listUpdater->update( |
|
115
|
1 |
|
$newConfiguration->getRequiredDevPackageList(), |
|
116
|
1 |
|
$baseConfiguration->getRequiredDevPackageList() |
|
117
|
1 |
|
), |
|
118
|
1 |
|
$this->listUpdater->update($newConfiguration->getScriptList(), $baseConfiguration->getScriptList()) |
|
119
|
1 |
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
protected function mergeKeyList(array $baseKeyList, array $newKeyList) |
|
123
|
|
|
{ |
|
124
|
1 |
|
return array_replace($baseKeyList, $newKeyList); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: