|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the composer-changelogs project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Loïck Piera <[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 Pyrech\ComposerChangelogs\OperationHandler; |
|
13
|
|
|
|
|
14
|
|
|
use Composer\DependencyResolver\Operation\OperationInterface; |
|
15
|
|
|
use Composer\DependencyResolver\Operation\UpdateOperation; |
|
16
|
|
|
use Composer\Package\Version\VersionParser; |
|
17
|
|
|
use Composer\Semver\Comparator; |
|
18
|
|
|
use Pyrech\ComposerChangelogs\UrlGenerator\UrlGenerator; |
|
19
|
|
|
use Pyrech\ComposerChangelogs\Version; |
|
20
|
|
|
|
|
21
|
|
|
class UpdateHandler implements OperationHandler |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
13 |
|
public function supports(OperationInterface $operation) |
|
27
|
|
|
{ |
|
28
|
13 |
|
return $operation instanceof UpdateOperation; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
19 |
|
public function extractSourceUrl(OperationInterface $operation) |
|
35
|
|
|
{ |
|
36
|
19 |
|
if (!($operation instanceof UpdateOperation)) { |
|
37
|
2 |
|
throw new \LogicException('Operation should be an instance of UpdateOperation'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
17 |
|
return $operation->getTargetPackage()->getSourceUrl(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
21 |
|
public function getOutput(OperationInterface $operation, UrlGenerator $urlGenerator = null) |
|
47
|
|
|
{ |
|
48
|
21 |
|
if (!($operation instanceof UpdateOperation)) { |
|
49
|
2 |
|
throw new \LogicException('Operation should be an instance of UpdateOperation'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
19 |
|
$output = []; |
|
53
|
|
|
|
|
54
|
19 |
|
$initialPackage = $operation->getInitialPackage(); |
|
55
|
19 |
|
$targetPackage = $operation->getTargetPackage(); |
|
56
|
|
|
|
|
57
|
19 |
|
$versionFrom = new Version( |
|
58
|
19 |
|
$initialPackage->getVersion(), |
|
59
|
19 |
|
$initialPackage->getPrettyVersion(), |
|
60
|
19 |
|
method_exists($initialPackage, 'getFullPrettyVersion') // This method was added after composer v1.0.0-alpha10 |
|
61
|
19 |
|
? $initialPackage->getFullPrettyVersion() |
|
62
|
19 |
|
: VersionParser::formatVersion($initialPackage) |
|
63
|
19 |
|
); |
|
64
|
19 |
|
$versionTo = new Version( |
|
65
|
19 |
|
$targetPackage->getVersion(), |
|
66
|
19 |
|
$targetPackage->getPrettyVersion(), |
|
67
|
19 |
|
method_exists($targetPackage, 'getFullPrettyVersion') // This method was added after composer v1.0.0-alpha10 |
|
68
|
19 |
|
? $targetPackage->getFullPrettyVersion() |
|
69
|
19 |
|
: VersionParser::formatVersion($targetPackage) |
|
70
|
19 |
|
); |
|
71
|
|
|
|
|
72
|
19 |
|
$action = 'updated'; |
|
73
|
|
|
|
|
74
|
19 |
|
if (Comparator::greaterThan($versionFrom->getName(), $versionTo->getName())) { |
|
75
|
2 |
|
$action = 'downgraded'; |
|
76
|
2 |
|
} |
|
77
|
|
|
|
|
78
|
19 |
|
$output[] = sprintf( |
|
79
|
19 |
|
' - <fg=green>%s</fg=green> %s from <fg=yellow>%s</fg=yellow> to <fg=yellow>%s</fg=yellow>', |
|
80
|
19 |
|
$initialPackage->getName(), |
|
81
|
19 |
|
$action, |
|
82
|
19 |
|
$versionFrom->getPretty(), |
|
83
|
19 |
|
$versionTo->getPretty() |
|
84
|
19 |
|
); |
|
85
|
|
|
|
|
86
|
19 |
|
if ($urlGenerator) { |
|
87
|
15 |
|
$compareUrl = $urlGenerator->generateCompareUrl( |
|
88
|
15 |
|
$initialPackage->getSourceUrl(), |
|
89
|
15 |
|
$versionFrom, |
|
90
|
15 |
|
$targetPackage->getSourceUrl(), |
|
91
|
|
|
$versionTo |
|
92
|
15 |
|
); |
|
93
|
|
|
|
|
94
|
15 |
|
if (!empty($compareUrl)) { |
|
95
|
13 |
|
$output[] = sprintf( |
|
96
|
13 |
|
' See changes: %s', |
|
97
|
|
|
$compareUrl |
|
98
|
13 |
|
); |
|
99
|
13 |
|
} |
|
100
|
|
|
|
|
101
|
15 |
|
$releaseUrl = $urlGenerator->generateReleaseUrl( |
|
102
|
15 |
|
$this->extractSourceUrl($operation), |
|
103
|
|
|
$versionTo |
|
104
|
15 |
|
); |
|
105
|
|
|
|
|
106
|
15 |
|
if (!empty($releaseUrl)) { |
|
107
|
13 |
|
$output[] = sprintf( |
|
108
|
13 |
|
' Release notes: %s', |
|
109
|
|
|
$releaseUrl |
|
110
|
13 |
|
); |
|
111
|
13 |
|
} |
|
112
|
15 |
|
} |
|
113
|
|
|
|
|
114
|
19 |
|
return $output; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|