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
|
|
|
|
16
|
|
|
public function __construct(Factory $filesystem, Config $config) |
17
|
|
|
{ |
18
|
|
|
$this->filesystem = $filesystem; |
19
|
|
|
$this->config = $config; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function setEndpoints(array $rawEndpoints) |
23
|
|
|
{ |
24
|
|
|
$endpoints = []; |
25
|
|
|
$delimiter = $this->config->get('yaro.apidocs.blueprint.reference_delimiter'); |
26
|
|
|
|
27
|
|
|
foreach ($rawEndpoints as $group => $groupEndpoints) { |
28
|
|
|
$group = preg_replace('~\.~', $delimiter, $group); |
29
|
|
|
$endpoints[$group] = $groupEndpoints; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$this->endpoints = $endpoints; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function render() |
36
|
|
|
{ |
37
|
|
|
$host = $this->config->get('yaro.apidocs.blueprint.host'); |
38
|
|
|
if (!$host) { |
39
|
|
|
$prefix = $this->config->get('yaro.apidocs.prefix', 'api'); |
40
|
|
|
$host = url($prefix); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return view('apidocs::blueprint', [ |
44
|
|
|
'endpoints' => $this->endpoints, |
45
|
|
|
'host' => $host, |
46
|
|
|
'title' => $this->config->get('yaro.apidocs.blueprint.title'), |
47
|
|
|
'introduction' => $this->config->get('yaro.apidocs.blueprint.introduction') |
48
|
|
|
])->render(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function create(string $snapshotName = '', string $diskName = '') |
52
|
|
|
{ |
53
|
|
|
$snapshotName = $snapshotName ?: 'blueprint_'. date('Y-m-d_H-i-s'); |
54
|
|
|
$diskName = $diskName ?: $this->config->get('yaro.apidocs.blueprint.disc'); |
55
|
|
|
|
56
|
|
|
$disk = $this->getDisk($diskName); |
57
|
|
|
|
58
|
|
|
$fileName = $snapshotName.'.apib'; |
59
|
|
|
$fileName = pathinfo($fileName, PATHINFO_BASENAME); |
60
|
|
|
|
61
|
|
|
$this->createFile($fileName, $disk); |
|
|
|
|
62
|
|
|
|
63
|
|
|
return $snapshotName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function getDisk(string $diskName) |
67
|
|
|
{ |
68
|
|
|
$path = sprintf('filesystems.disks.%s', $diskName); |
69
|
|
|
if (is_null($this->config->get($path))) { |
70
|
|
|
throw new InvalidFilesystemDisc($diskName); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->filesystem->disk($diskName); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function createFile(string $fileName, FilesystemAdapter $disk) |
77
|
|
|
{ |
78
|
|
|
$tempFileHandle = tmpfile(); |
79
|
|
|
fwrite($tempFileHandle, $this->render()); |
80
|
|
|
|
81
|
|
|
$disk->put($fileName, $tempFileHandle); |
82
|
|
|
|
83
|
|
|
fclose($tempFileHandle); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: