1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of laravel.su package. |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace App\Console\Commands; |
11
|
|
|
|
12
|
|
|
use App\Models\Article; |
13
|
|
|
use App\Models\Docs; |
14
|
|
|
use App\Services\GitHub\DocsStatus; |
15
|
|
|
use App\Services\GitHub\ExternalDocsPage; |
16
|
|
|
use App\Services\GitHub\GitHubConfigRepository; |
17
|
|
|
use App\Services\GitHub\GitHubDocsManager; |
18
|
|
|
use Illuminate\Console\Command; |
19
|
|
|
use Illuminate\Support\Collection; |
20
|
|
|
use Illuminate\Support\Str; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class GitHubDocsImport. |
24
|
|
|
*/ |
25
|
|
|
class GitHubDocsImport extends Command |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The name and signature of the console command. |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $signature = 'docs:import'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The console command description. |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $description = 'Import documentation from GitHub.'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Execute the console command. |
41
|
|
|
* @param GitHubDocsManager $manager |
42
|
|
|
* @param GitHubConfigRepository $config |
43
|
|
|
* @return void |
44
|
|
|
* @throws \Github\Exception\ErrorException |
45
|
|
|
* @throws \Github\Exception\InvalidArgumentException |
46
|
|
|
* @throws \RuntimeException |
47
|
|
|
*/ |
48
|
|
|
public function handle(GitHubDocsManager $manager, GitHubConfigRepository $config): void |
49
|
|
|
{ |
50
|
|
|
$exists = $this->getDocsCurrentState(...$config->values()); |
|
|
|
|
51
|
|
|
|
52
|
|
|
$files = $manager->findFiles(...$config->values()) |
|
|
|
|
53
|
|
|
->reject(function (DocsStatus $status) use ($exists) { |
54
|
|
|
return $exists->where('github_hash', $status->getHash())->first(); |
55
|
|
|
}) |
56
|
|
|
->each(function (DocsStatus $status) use ($config, $manager) { |
57
|
|
|
$args = $config->values(); |
58
|
|
|
$args[] = $status->getPath(); |
59
|
|
|
|
60
|
|
|
/** @var ExternalDocsPage $page */ |
61
|
|
|
$page = $manager->import(...$args); |
|
|
|
|
62
|
|
|
|
63
|
|
|
$docs = $this->getDocsModel(...$args); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$docs->content_source = $page->getContent(); |
66
|
|
|
$docs->github_hash = $status->getHash(); |
67
|
|
|
|
68
|
|
|
if (!$docs->category_id) { |
69
|
|
|
$docs->category_id = 0; // TODO |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (!$docs->title) { |
73
|
|
|
$docs->title = Str::ucfirst(str_replace( |
74
|
|
|
['-', '_'], |
75
|
|
|
' ', |
76
|
|
|
$page->getUrl() |
77
|
|
|
)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$docs->save(); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $org |
86
|
|
|
* @param string $repo |
87
|
|
|
* @param string $branch |
88
|
|
|
* @return Collection |
89
|
|
|
*/ |
90
|
|
|
private function getDocsCurrentState(string $org, string $repo, string $branch) |
91
|
|
|
{ |
92
|
|
|
return Docs::query() |
93
|
|
|
->where('github_org', $org) |
94
|
|
|
->where('github_repo', $repo) |
95
|
|
|
->where('github_branch', $branch) |
96
|
|
|
->get(['github_hash']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $org |
101
|
|
|
* @param string $repo |
102
|
|
|
* @param string $branch |
103
|
|
|
* @param string $file |
104
|
|
|
* @return \Illuminate\Database\Eloquent\Model|Docs |
105
|
|
|
*/ |
106
|
|
|
private function getDocsModel(string $org, string $repo, string $branch, string $file) |
107
|
|
|
{ |
108
|
|
|
return Docs::query() |
109
|
|
|
->firstOrNew([ |
110
|
|
|
'github_org' => $org, |
111
|
|
|
'github_repo' => $repo, |
112
|
|
|
'github_branch' => $branch, |
113
|
|
|
'github_file' => $file, |
114
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|