|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\ComposerConfigManager\Application\Updater; |
|
3
|
|
|
|
|
4
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Author; |
|
5
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Autoload; |
|
6
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Configuration; |
|
7
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Package; |
|
8
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Script; |
|
9
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\SuggestedPackage; |
|
10
|
|
|
use Yoanm\ComposerConfigManager\Domain\Model\Support; |
|
11
|
|
|
|
|
12
|
|
|
class ConfigurationUpdater |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param Configuration $baseConfiguration |
|
16
|
|
|
* @param Configuration $newConfiguration |
|
17
|
|
|
* |
|
18
|
|
|
* @return Configuration |
|
19
|
|
|
*/ |
|
20
|
|
|
public function update(Configuration $baseConfiguration, Configuration $newConfiguration) |
|
21
|
|
|
{ |
|
22
|
|
|
return new Configuration( |
|
23
|
|
|
$this->updateIfDefined($baseConfiguration->getPackageName(), $newConfiguration->getPackageName()), |
|
24
|
|
|
$this->updateIfDefined($baseConfiguration->getType(), $newConfiguration->getType()), |
|
25
|
|
|
$this->updateIfDefined($baseConfiguration->getLicense(), $newConfiguration->getLicense()), |
|
26
|
|
|
$this->updateIfDefined($baseConfiguration->getPackageVersion(), $newConfiguration->getPackageVersion()), |
|
27
|
|
|
$this->updateIfDefined($baseConfiguration->getDescription(), $newConfiguration->getDescription()), |
|
28
|
|
|
$this->mergeValueList($baseConfiguration->getKeywordList(), $newConfiguration->getKeywordList()), |
|
29
|
|
|
$this->updateList($baseConfiguration->getAuthorList(), $newConfiguration->getAuthorList()), |
|
30
|
|
|
$this->updateList( |
|
31
|
|
|
$baseConfiguration->getProvidedPackageList(), |
|
32
|
|
|
$newConfiguration->getProvidedPackageList() |
|
33
|
|
|
), |
|
34
|
|
|
$this->updateList( |
|
35
|
|
|
$baseConfiguration->getSuggestedPackageList(), |
|
36
|
|
|
$newConfiguration->getSuggestedPackageList() |
|
37
|
|
|
), |
|
38
|
|
|
$this->updateList($baseConfiguration->getSupportList(), $newConfiguration->getSupportList()), |
|
39
|
|
|
$this->updateList( |
|
40
|
|
|
$baseConfiguration->getRequiredDevPackageList(), |
|
41
|
|
|
$newConfiguration->getRequiredDevPackageList() |
|
42
|
|
|
), |
|
43
|
|
|
$this->updateList( |
|
44
|
|
|
$baseConfiguration->getRequiredDevPackageList(), |
|
45
|
|
|
$newConfiguration->getRequiredDevPackageList() |
|
46
|
|
|
), |
|
47
|
|
|
$this->updateList($baseConfiguration->getAutoloadList(), $newConfiguration->getAutoloadList()), |
|
48
|
|
|
$this->updateList($baseConfiguration->getAutoloadDevList(), $newConfiguration->getAutoloadDevList()), |
|
49
|
|
|
$this->updateList($baseConfiguration->getScriptList(), $newConfiguration->getScriptList()) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param object $entity |
|
55
|
|
|
* |
|
56
|
|
|
* @return null|string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getEntityId($entity) { |
|
59
|
|
|
switch (true) { |
|
60
|
|
|
case $entity instanceof Author: $id = $entity->getName(); |
|
|
|
|
|
|
61
|
|
|
break; |
|
62
|
|
|
case $entity instanceof SuggestedPackage: $id = $entity->getName(); |
|
|
|
|
|
|
63
|
|
|
break; |
|
64
|
|
|
case $entity instanceof Support: $id = $entity->getType(); |
|
|
|
|
|
|
65
|
|
|
break; |
|
66
|
|
|
case $entity instanceof Autoload: $id = $entity->getType().'#'.$entity->getNamespace(); |
|
|
|
|
|
|
67
|
|
|
break; |
|
68
|
|
|
case $entity instanceof Package: $id = $entity->getName(); |
|
|
|
|
|
|
69
|
|
|
break; |
|
70
|
|
|
case $entity instanceof Script: $id = $entity->getName(); |
|
|
|
|
|
|
71
|
|
|
break; |
|
72
|
|
|
default: |
|
73
|
|
|
$id = null; |
|
74
|
|
|
}; |
|
75
|
|
|
|
|
76
|
|
|
return $id; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param mixed $baseValue |
|
81
|
|
|
* @param mixed $newValue |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function updateIfDefined($baseValue, $newValue) |
|
86
|
|
|
{ |
|
87
|
|
|
return $newValue ? $newValue : $baseValue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array $newList |
|
92
|
|
|
* @param array $oldList |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function mergeValueList(array $newList, array $oldList) |
|
97
|
|
|
{ |
|
98
|
|
|
return array_values( |
|
99
|
|
|
array_unique( |
|
100
|
|
|
array_merge($oldList, $newList) |
|
101
|
|
|
) |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param array $newEntityList |
|
107
|
|
|
* @param array $oldEntityList |
|
108
|
|
|
* |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function updateList(array $newEntityList, array $oldEntityList) |
|
112
|
|
|
{ |
|
113
|
|
|
$list = []; |
|
114
|
|
|
$mergedEntityIdList = []; |
|
115
|
|
|
$self = $this; |
|
116
|
|
|
foreach ($newEntityList as $newEntity) { |
|
117
|
|
|
// Search for an old entry |
|
118
|
|
|
$newEntityId = $this->getEntityId($newEntity); |
|
119
|
|
|
$oldEntityMatches = array_filter( |
|
120
|
|
|
$oldEntityList, |
|
121
|
|
|
function(Package $oldEntity) use ($newEntityId, $self) { |
|
122
|
|
|
return $self->getEntityId($oldEntity) == $newEntityId; |
|
123
|
|
|
} |
|
124
|
|
|
); |
|
125
|
|
|
if (count($oldEntityMatches)) { |
|
126
|
|
|
$mergedEntityIdList[$newEntityId] = true; |
|
127
|
|
|
} |
|
128
|
|
|
$list[] = $newEntity; |
|
129
|
|
|
} |
|
130
|
|
|
// Merge remaining entities that have not been already merged |
|
131
|
|
|
foreach ($oldEntityList as $entity) { |
|
132
|
|
|
if (!array_key_exists($this->getEntityId($entity), $mergedEntityIdList)) { |
|
133
|
|
|
$list[] = $entity; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $list; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.