1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @category Library
|
4
|
|
|
* @license MIT http://opensource.org/licenses/MIT
|
5
|
|
|
* @link https://github.com/emlynwest/changelog
|
6
|
|
|
*/
|
7
|
|
|
|
8
|
|
|
namespace ChangeLog\Renderer;
|
9
|
|
|
|
10
|
|
|
use ChangeLog\Log;
|
11
|
|
|
use ChangeLog\Release;
|
12
|
|
|
use ChangeLog\RenderInterface;
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* Allows change logs to be rendered into the http://keepachangelog.com format.
|
16
|
|
|
*/
|
17
|
|
|
class KeepAChangeLog implements RenderInterface
|
18
|
|
|
{
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* {@inheritdoc}
|
22
|
|
|
*/
|
23
|
2 |
|
public function render(Log $log)
|
24
|
|
|
{
|
25
|
2 |
|
$content = "# {$log->getTitle()}\n" .
|
26
|
2 |
|
"{$log->getDescription()}\n";
|
27
|
|
|
|
28
|
2 |
|
$links = '';
|
29
|
|
|
|
30
|
|
|
/** @var Release $release */
|
31
|
2 |
|
foreach ($log as $release)
|
32
|
|
|
{
|
33
|
2 |
|
$content .= $this->renderRelease($release);
|
34
|
2 |
|
$links .= $this->createLink($release);
|
35
|
|
|
}
|
36
|
|
|
|
37
|
2 |
|
if ($links !== '')
|
38
|
|
|
{
|
39
|
1 |
|
$content .= "\n\n" . $links;
|
40
|
|
|
}
|
41
|
|
|
|
42
|
2 |
|
return $content;
|
43
|
|
|
}
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* Creates the needed link text for a Release.
|
47
|
|
|
*
|
48
|
|
|
* @param Release $release
|
49
|
|
|
*
|
50
|
|
|
* @return string
|
51
|
|
|
*/
|
52
|
2 |
|
protected function createLink(Release $release)
|
53
|
|
|
{
|
54
|
2 |
|
$line = '';
|
55
|
|
|
|
56
|
2 |
|
$link = $release->getLink();
|
57
|
|
|
|
58
|
2 |
|
if ($link !== null)
|
|
|
|
|
59
|
|
|
{
|
60
|
1 |
|
$linkName = $release->getLinkName();
|
61
|
1 |
|
$name = $release->getName();
|
62
|
|
|
|
63
|
1 |
|
$reference = ($linkName === null) ? $name : $linkName;
|
|
|
|
|
64
|
|
|
|
65
|
1 |
|
$line = "[$reference] $link\n";
|
66
|
|
|
}
|
67
|
|
|
|
68
|
2 |
|
return $line;
|
69
|
|
|
}
|
70
|
|
|
|
71
|
|
|
/**
|
72
|
|
|
* Converts a Release into its text representation.
|
73
|
|
|
*
|
74
|
|
|
* @param Release $release
|
75
|
|
|
*
|
76
|
|
|
* @return string
|
77
|
|
|
*/
|
78
|
4 |
|
public function renderRelease(Release $release)
|
79
|
|
|
{
|
80
|
4 |
|
$name = $release->getName();
|
81
|
|
|
|
82
|
4 |
|
if ($release->getLink() !== null)
|
|
|
|
|
83
|
|
|
{
|
84
|
1 |
|
$name = "[$name]";
|
85
|
|
|
}
|
86
|
|
|
|
87
|
4 |
|
if ($release->getLinkName() !== null)
|
|
|
|
|
88
|
|
|
{
|
89
|
1 |
|
$name .= "[{$release->getLinkName()}]";
|
90
|
|
|
}
|
91
|
|
|
|
92
|
4 |
|
$content = "\n## $name";
|
93
|
|
|
|
94
|
4 |
|
$content .= $this->addDate($release);
|
95
|
4 |
|
$content .= $this->addYanked($release);
|
96
|
|
|
|
97
|
4 |
|
$content .= "\n";
|
98
|
|
|
|
99
|
4 |
|
foreach ($release->getAllChanges() as $type => $changes)
|
100
|
|
|
{
|
101
|
1 |
|
$content .= $this->renderType($type, $changes);
|
102
|
|
|
}
|
103
|
|
|
|
104
|
4 |
|
return substr($content, 0, strlen($content)-1);
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
/**
|
108
|
|
|
* Converts a list of changes with a given type back into text.
|
109
|
|
|
*
|
110
|
|
|
* @param string $type
|
111
|
|
|
* @param array $changes
|
112
|
|
|
*
|
113
|
|
|
* @return string
|
114
|
|
|
*/
|
115
|
1 |
|
public function renderType($type, $changes)
|
116
|
|
|
{
|
117
|
1 |
|
$content = '';
|
118
|
|
|
|
119
|
1 |
|
if (count($changes) > 0)
|
120
|
|
|
{
|
121
|
1 |
|
$content = "### $type\n" .
|
122
|
1 |
|
'- ' . implode("\n- ", $changes) . "\n";
|
123
|
|
|
}
|
124
|
|
|
|
125
|
1 |
|
return $content . "\n";
|
126
|
|
|
}
|
127
|
|
|
|
128
|
|
|
/**
|
129
|
|
|
* Adds the date to a Release title for rendering if the Release has a date.
|
130
|
|
|
*
|
131
|
|
|
* @param Release $release
|
132
|
|
|
*
|
133
|
|
|
* @return string
|
134
|
|
|
*/
|
135
|
4 |
|
protected function addDate(Release $release)
|
136
|
|
|
{
|
137
|
4 |
|
$content = '';
|
138
|
4 |
|
$date = $release->getDate();
|
139
|
4 |
|
if ($date !== null)
|
140
|
|
|
{
|
141
|
1 |
|
$content = ' - ' . $date->format('Y-m-d');
|
142
|
|
|
}
|
143
|
4 |
|
return $content;
|
144
|
|
|
}
|
145
|
|
|
/**
|
146
|
|
|
* Returns the YANKED tag if needed
|
147
|
|
|
*
|
148
|
|
|
* @param Release $release
|
149
|
|
|
*
|
150
|
|
|
* @return string
|
151
|
|
|
*/
|
152
|
4 |
|
protected function addYanked(Release $release)
|
153
|
|
|
{
|
154
|
4 |
|
$content = '';
|
155
|
4 |
|
if ($release->isYanked())
|
156
|
|
|
{
|
157
|
1 |
|
$content = ' [YANKED]';
|
158
|
|
|
}
|
159
|
4 |
|
return $content;
|
160
|
|
|
}
|
161
|
|
|
|
162
|
|
|
}
|
163
|
|
|
|