|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yaro\ApiDocs; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Filesystem\Factory; |
|
6
|
|
|
use Illuminate\Filesystem\FilesystemAdapter; |
|
7
|
|
|
use Illuminate\Contracts\Config\Repository as Config; |
|
8
|
|
|
use Yaro\ApiDocs\Exceptions\InvalidFilesystemDisc; |
|
9
|
|
|
|
|
10
|
|
|
class Blueprint |
|
11
|
|
|
{ |
|
12
|
|
|
private $filesystem; |
|
13
|
|
|
private $config; |
|
14
|
|
|
private $endpoints = []; |
|
15
|
|
|
private $routePrefix; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Factory $filesystem, Config $config) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->filesystem = $filesystem; |
|
20
|
|
|
$this->config = $config; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function setRoutePrefix($routePrefix) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->routePrefix = $routePrefix; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function setEndpoints(array $rawEndpoints) |
|
29
|
|
|
{ |
|
30
|
|
|
$endpoints = []; |
|
31
|
|
|
$delimiter = $this->config->get('yaro.apidocs.blueprint.reference_delimiter'); |
|
32
|
|
|
|
|
33
|
|
|
foreach ($rawEndpoints as $group => $groupEndpoints) { |
|
34
|
|
|
$group = preg_replace('~\.~', $delimiter, $group); |
|
35
|
|
|
$endpoints[$group] = $groupEndpoints; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$this->endpoints = $endpoints; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function render() |
|
42
|
|
|
{ |
|
43
|
|
|
$host = $this->config->get('yaro.apidocs.blueprint.host'); |
|
44
|
|
|
if (!$host) { |
|
45
|
|
|
$host = url($this->routePrefix); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return view('apidocs::blueprint', [ |
|
49
|
|
|
'endpoints' => $this->endpoints, |
|
50
|
|
|
'host' => $host, |
|
51
|
|
|
'title' => $this->config->get('yaro.apidocs.blueprint.title'), |
|
52
|
|
|
'introduction' => $this->config->get('yaro.apidocs.blueprint.introduction') |
|
53
|
|
|
])->render(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function create(string $snapshotName = '', string $diskName = '') |
|
57
|
|
|
{ |
|
58
|
|
|
$snapshotName = $snapshotName ?: 'blueprint_'. date('Y-m-d_H-i-s'); |
|
59
|
|
|
$diskName = $diskName ?: $this->config->get('yaro.apidocs.blueprint.disc'); |
|
60
|
|
|
|
|
61
|
|
|
$disk = $this->getDisk($diskName); |
|
62
|
|
|
|
|
63
|
|
|
$fileName = $snapshotName.'.apib'; |
|
64
|
|
|
$fileName = pathinfo($fileName, PATHINFO_BASENAME); |
|
65
|
|
|
|
|
66
|
|
|
$this->createFile($fileName, $disk); |
|
67
|
|
|
|
|
68
|
|
|
return $snapshotName; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private function getDisk(string $diskName) |
|
72
|
|
|
{ |
|
73
|
|
|
$path = sprintf('filesystems.disks.%s', $diskName); |
|
74
|
|
|
if (is_null($this->config->get($path))) { |
|
75
|
|
|
throw new InvalidFilesystemDisc($diskName); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->filesystem->disk($diskName); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function createFile(string $fileName, FilesystemAdapter $disk) |
|
82
|
|
|
{ |
|
83
|
|
|
$tempFileHandle = tmpfile(); |
|
84
|
|
|
fwrite($tempFileHandle, $this->render()); |
|
85
|
|
|
|
|
86
|
|
|
$disk->put($fileName, $tempFileHandle); |
|
87
|
|
|
|
|
88
|
|
|
fclose($tempFileHandle); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|