|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of laravel.su package. |
|
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
5
|
|
|
* file that was distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Services\GitHub; |
|
10
|
|
|
|
|
11
|
|
|
use Illuminate\Support\Collection; |
|
12
|
|
|
use GrahamCampbell\GitHub\GitHubManager; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class GitHubDocsManager. |
|
16
|
|
|
*/ |
|
17
|
|
|
class GitHubDocsManager implements DocsManagerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var GitHubManager |
|
21
|
|
|
*/ |
|
22
|
|
|
private $manager; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* GitHubDocsImport constructor. |
|
26
|
|
|
* @param GitHubManager $manager |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(GitHubManager $manager) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->manager = $manager; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $org |
|
35
|
|
|
* @param string $repo |
|
36
|
|
|
* @param string $branch |
|
37
|
|
|
* @return Collection|DocsCollection|DocsStatus[] |
|
38
|
|
|
* @throws \Github\Exception\ErrorException |
|
39
|
|
|
* @throws \Github\Exception\InvalidArgumentException |
|
40
|
|
|
* @throws \RuntimeException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function findFiles(string $org, string $repo, string $branch): DocsCollection |
|
43
|
|
|
{ |
|
44
|
|
|
$result = new DocsCollection(); |
|
45
|
|
|
|
|
46
|
|
|
$info = $this->getRepoFilesInfo($org, $repo, $branch); |
|
47
|
|
|
|
|
48
|
|
|
foreach ($info as $page) { |
|
49
|
|
|
if (! isset($page['path'], $page['download_url'])) { |
|
50
|
|
|
throw new \RuntimeException('Import failed: "path" or "download_url" arguments required.'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$result[] = new DocsStatus($page['path'], $page['sha']); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $result; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $org |
|
61
|
|
|
* @param string $repo |
|
62
|
|
|
* @param string $branch |
|
63
|
|
|
* @param string $file |
|
64
|
|
|
* @return string |
|
65
|
|
|
* @throws \Github\Exception\ErrorException |
|
66
|
|
|
* @throws \Github\Exception\InvalidArgumentException |
|
67
|
|
|
*/ |
|
68
|
|
|
private function getBody(string $org, string $repo, string $branch, string $file): string |
|
69
|
|
|
{ |
|
70
|
|
|
return (string) $this->manager |
|
71
|
|
|
->repo() |
|
72
|
|
|
->contents() |
|
73
|
|
|
->download($org, $repo, $file, $branch); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $org |
|
78
|
|
|
* @param string $repo |
|
79
|
|
|
* @param string $branch |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
private function getRepoFilesInfo(string $org, string $repo, string $branch): array |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->manager |
|
85
|
|
|
->repo() |
|
86
|
|
|
->contents() |
|
87
|
|
|
->show($org, $repo, null, $branch); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $org |
|
92
|
|
|
* @param string $repo |
|
93
|
|
|
* @param string $branch |
|
94
|
|
|
* @param string $file |
|
95
|
|
|
* @return ExternalDocsPage |
|
96
|
|
|
* @throws \Github\Exception\ErrorException |
|
97
|
|
|
* @throws \Github\Exception\InvalidArgumentException |
|
98
|
|
|
*/ |
|
99
|
|
|
public function import(string $org, string $repo, string $branch, string $file): ExternalDocsPage |
|
100
|
|
|
{ |
|
101
|
|
|
$body = $this->getBody($org, $repo, $branch, $file); |
|
102
|
|
|
|
|
103
|
|
|
return new ExternalDocsPage(basename($file, '.md'), $body); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|