1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Changelog keeper |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/chkipper |
7
|
|
|
* @package chkipper |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\chkipper\history; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Abstract history renderer. |
16
|
|
|
* |
17
|
|
|
* @author Andrii Vasyliev <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractRenderer |
20
|
|
|
{ |
21
|
2 |
|
public function setHistory($value) |
22
|
|
|
{ |
23
|
2 |
|
$this->_history = $value; |
|
|
|
|
24
|
2 |
|
$this->normalizeHistory(); |
25
|
2 |
|
} |
26
|
|
|
|
27
|
2 |
|
public function getHistory() |
28
|
|
|
{ |
29
|
2 |
|
return $this->_history; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Renders history to string. |
34
|
|
|
* @param History $history |
35
|
|
|
* @return text |
36
|
|
|
*/ |
37
|
|
|
abstract public function render(History $history); |
38
|
|
|
|
39
|
2 |
|
public function normalizeHistory() |
40
|
|
|
{ |
41
|
2 |
|
$this->getHistory()->normalize(); |
42
|
2 |
|
$this->addCommitLinks(); |
43
|
2 |
|
$this->removeCommitLinks(); |
44
|
2 |
|
} |
45
|
|
|
|
46
|
2 |
|
public function addCommitLinks() |
47
|
|
|
{ |
48
|
2 |
|
$history = $this->getHistory(); |
49
|
2 |
|
foreach ($history->getHashes() as $hash) { |
50
|
2 |
|
if (!$history->hasLink($hash)) { |
51
|
1 |
|
$history->addLink($hash, $this->generateHashHref($hash)); |
52
|
1 |
|
} |
53
|
2 |
|
} |
54
|
2 |
|
} |
55
|
|
|
|
56
|
1 |
|
public function generateHashHref($hash) |
57
|
|
|
{ |
58
|
1 |
|
$project = $this->getHistory()->getProject(); |
59
|
|
|
|
60
|
1 |
|
return "https://github.com/$project/commit/$hash"; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Removes commit links that are not in the history. |
65
|
|
|
*/ |
66
|
2 |
|
public function removeCommitLinks() |
67
|
|
|
{ |
68
|
2 |
|
$history = $this->getHistory(); |
69
|
2 |
|
foreach ($history->getLinks() as $link => $href) { |
70
|
2 |
|
if (preg_match('/^[0-9a-f]{7}$/', $link) && !$history->hasHash($link)) { |
71
|
|
|
$history->removeLink($link); |
72
|
|
|
} |
73
|
2 |
|
} |
74
|
2 |
|
} |
75
|
|
|
} |
76
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: