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