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; |
13
|
|
|
|
14
|
|
|
use Composer\DependencyResolver\Operation\OperationInterface; |
15
|
|
|
use Pyrech\ComposerChangelogs\OperationHandler\OperationHandler; |
16
|
|
|
use Pyrech\ComposerChangelogs\UrlGenerator\UrlGenerator; |
17
|
|
|
|
18
|
|
|
class Outputter |
19
|
|
|
{ |
20
|
|
|
/** @var OperationHandler[] */ |
21
|
|
|
private $operationHandlers; |
22
|
|
|
|
23
|
|
|
/** @var UrlGenerator[] */ |
24
|
|
|
private $urlGenerators; |
25
|
|
|
|
26
|
|
|
/** @var OperationInterface[] */ |
27
|
|
|
private $operations; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param OperationHandler[] $operationHandlers |
31
|
|
|
* @param UrlGenerator[] $urlGenerators |
32
|
|
|
*/ |
33
|
24 |
|
public function __construct(array $operationHandlers, array $urlGenerators) |
34
|
|
|
{ |
35
|
24 |
|
$this->urlGenerators = $urlGenerators; |
36
|
24 |
|
$this->operationHandlers = $operationHandlers; |
37
|
24 |
|
$this->operations = []; |
38
|
24 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param OperationInterface $operation |
42
|
|
|
*/ |
43
|
18 |
|
public function addOperation(OperationInterface $operation) |
44
|
|
|
{ |
45
|
18 |
|
$this->operations[] = $operation; |
46
|
18 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
18 |
|
public function isEmpty() |
52
|
|
|
{ |
53
|
18 |
|
return empty($this->operations); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
18 |
|
public function getOutput() |
60
|
|
|
{ |
61
|
18 |
|
$output = []; |
62
|
|
|
|
63
|
18 |
|
if ($this->isEmpty()) { |
64
|
2 |
|
$output[] = '<fg=green>No changelogs summary</fg=green>'; |
65
|
2 |
|
} else { |
66
|
16 |
|
$output[] = '<fg=green>Changelogs summary:</fg=green>'; |
67
|
|
|
|
68
|
16 |
|
foreach ($this->operations as $operation) { |
69
|
16 |
|
$this->createOperationOutput($output, $operation); |
70
|
16 |
|
} |
71
|
|
|
|
72
|
16 |
|
$output[] = ''; |
73
|
|
|
} |
74
|
|
|
|
75
|
18 |
|
return implode("\n", $output); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $output |
80
|
|
|
* @param OperationInterface $operation |
81
|
|
|
* |
82
|
|
|
* @return array|void |
83
|
|
|
*/ |
84
|
16 |
|
private function createOperationOutput(array &$output, OperationInterface $operation) |
85
|
|
|
{ |
86
|
16 |
|
$operationHandler = $this->getOperationHandler($operation); |
87
|
|
|
|
88
|
16 |
|
if (!$operationHandler) { |
89
|
2 |
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
14 |
|
$output[] = ''; |
93
|
|
|
|
94
|
14 |
|
$urlGenerator = $this->getUrlGenerator( |
95
|
14 |
|
$operationHandler->extractSourceUrl($operation) |
96
|
14 |
|
); |
97
|
|
|
|
98
|
14 |
|
$output = array_merge( |
99
|
14 |
|
$output, |
100
|
14 |
|
$operationHandler->getOutput($operation, $urlGenerator) |
101
|
14 |
|
); |
102
|
14 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param OperationInterface $operation |
106
|
|
|
* |
107
|
|
|
* @return OperationHandler|null |
108
|
|
|
*/ |
109
|
16 |
|
private function getOperationHandler(OperationInterface $operation) |
110
|
|
|
{ |
111
|
16 |
|
foreach ($this->operationHandlers as $operationHandler) { |
112
|
16 |
|
if ($operationHandler->supports($operation)) { |
113
|
14 |
|
return $operationHandler; |
114
|
|
|
} |
115
|
16 |
|
} |
116
|
|
|
|
117
|
2 |
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $sourceUrl |
122
|
|
|
* |
123
|
|
|
* @return UrlGenerator|null |
124
|
|
|
*/ |
125
|
14 |
|
private function getUrlGenerator($sourceUrl) |
126
|
|
|
{ |
127
|
14 |
|
foreach ($this->urlGenerators as $urlGenerator) { |
128
|
14 |
|
if ($urlGenerator->supports($sourceUrl)) { |
129
|
12 |
|
return $urlGenerator; |
130
|
|
|
} |
131
|
4 |
|
} |
132
|
|
|
|
133
|
2 |
|
return; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|