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\Docs; |
13
|
|
|
use App\Models\DocsPage; |
14
|
|
|
use Illuminate\Console\Command; |
15
|
|
|
use Service\DocsImporter\DocsFileInterface; |
16
|
|
|
use Service\DocsImporter\DocsImporterManager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class GitHubDocsSync. |
20
|
|
|
*/ |
21
|
|
|
class GitHubDocsSync extends Command |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The name and signature of the console command. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $signature = 'docs:sync {?--force}'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The console command description. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $description = 'Sync documentation from GitHub.'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Execute the console command. |
39
|
|
|
* @param DocsImporterManager $manager |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function handle(DocsImporterManager $manager): void |
43
|
|
|
{ |
44
|
|
|
$docs = Docs::with('pages')->get(); |
|
|
|
|
45
|
|
|
|
46
|
|
|
if ($docs->isEmpty()) { |
47
|
|
|
$this->info('Skipping: No docs provided.'); |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @var Docs $repo */ |
52
|
|
|
foreach ($docs as $repo) { |
53
|
|
|
$importer = $manager->get($repo->importer); |
54
|
|
|
$config = $importer->config($repo->organisation, $repo->repository, $repo->branch); |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
foreach ($importer->files($config) as $file) { |
58
|
|
|
$prefix = '[' . $repo->title . ']::' . $file->getRoute(); |
59
|
|
|
|
60
|
|
|
/* |
61
|
|
|
|-------------------------------------------------------------------------- |
62
|
|
|
| Is file with required hash exists? |
63
|
|
|
|-------------------------------------------------------------------------- |
64
|
|
|
*/ |
65
|
|
|
$updateRequired = $repo->pages->where('hash', $file->getHash())->isEmpty(); |
66
|
|
|
|
67
|
|
|
if (!$updateRequired) { |
68
|
|
|
$this->comment($prefix . ' has no available updates.'); |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* |
73
|
|
|
|-------------------------------------------------------------------------- |
74
|
|
|
| Is file with required identifier exists? |
75
|
|
|
|-------------------------------------------------------------------------- |
76
|
|
|
*/ |
77
|
|
|
$page = $repo->pages->where('identify', $file->getId())->first(); |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
$message = $prefix . ' found. Updating sources...'; |
81
|
|
|
|
82
|
|
|
if ($page === null) { |
83
|
|
|
$message = $prefix . ' not exists. Uploading new...'; |
84
|
|
|
$page = new DocsPage(['identify' => $file->getId()]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->comment($message); |
88
|
|
|
$this->updatePage($repo, $page, $file); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param Docs $repo |
95
|
|
|
* @param DocsPage $page |
96
|
|
|
* @param DocsFileInterface $file |
97
|
|
|
*/ |
98
|
|
|
private function updatePage(Docs $repo, DocsPage $page, DocsFileInterface $file) |
99
|
|
|
{ |
100
|
|
|
$page->docs()->associate($repo); |
101
|
|
|
|
102
|
|
|
if ($page->category_id === null) { |
|
|
|
|
103
|
|
|
$page->category_id = 0; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$page->hash = $file->getHash(); |
|
|
|
|
107
|
|
|
$page->title = $file->getTitle(); |
|
|
|
|
108
|
|
|
$page->content_source = $file->getContent(); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$page->save(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: