1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2017 Julius Härtl <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Julius Härtl <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace JuliusHaertl\PHPDocToRst\Extension; |
25
|
|
|
|
26
|
|
|
use JuliusHaertl\PHPDocToRst\Builder\FileBuilder; |
27
|
|
|
use JuliusHaertl\PHPDocToRst\Builder\PhpDomainBuilder; |
28
|
|
|
use phpDocumentor\Reflection\Element; |
29
|
|
|
use phpDocumentor\Reflection\Php\File; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* This extension adds a link to the source at github to all elements |
33
|
|
|
* |
34
|
|
|
* Arguments |
35
|
|
|
* 0 => Url to the github repo (required) |
36
|
|
|
* 1 => Path to the git repository (required) |
37
|
|
|
* 2 => Branch to link to (default=master) |
38
|
|
|
*/ |
39
|
|
|
|
40
|
|
|
class GithubLocationExtension extends Extension { |
41
|
|
|
|
42
|
|
|
protected $basePath; |
43
|
|
|
protected $githubRepo; |
44
|
|
|
protected $branch = 'master'; |
45
|
|
|
|
46
|
|
|
public function prepare() { |
47
|
|
|
if (count($this->arguments) < 2) { |
48
|
|
|
throw new \Exception('GithubLocationExtension requires the following arguments githubUrl, basePath.'); |
49
|
|
|
} |
50
|
|
|
$this->basePath = $this->arguments[0]; |
51
|
|
|
$this->githubRepo = $this->arguments[1]; |
52
|
|
|
if (count($this->arguments) > 2) { |
53
|
|
|
$this->branch = $this->arguments[2]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $type |
59
|
|
|
* @param FileBuilder $builder |
60
|
|
|
* @param Element $element |
61
|
|
|
*/ |
62
|
|
|
public function render($type, &$builder, $element) { |
63
|
|
|
if (!$builder instanceof FileBuilder) { |
|
|
|
|
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
if ($type === PhpDomainBuilder::SECTION_AFTER_DESCRIPTION) { |
67
|
|
|
if (!$builder->getFile() instanceof File) { |
|
|
|
|
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
$filePath = $builder->getFile()->getPath(); |
71
|
|
|
$filePath = preg_replace('/^' . preg_quote($this->basePath, '/') . '/', '', $filePath); |
72
|
|
|
$lineNumber = $element->getLocation()->getLineNumber(); |
|
|
|
|
73
|
|
|
$url = $this->getGithubLink($filePath, $lineNumber, $this->branch); |
74
|
|
|
$builder->addFieldList('Source', '`' . $filePath. '#' . $lineNumber . ' <'.$url.'>`_'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
private function getGithubLink($file, $line=1, $branch='master') { |
79
|
|
|
return $this->githubRepo . '/blob/'.$branch.'/'.$file.'#L' . $line; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
} |