1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Containers\Documentation\Traits; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DocsGeneratorTrait |
10
|
|
|
* |
11
|
|
|
* @author Mahmoud Zalt <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
trait DocsGeneratorTrait |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param $type |
18
|
|
|
* |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
private function getFullApiUrl($type) |
22
|
|
|
{ |
23
|
|
|
return '> ' . $this->getAppUrl() . '/' . $this->getUrl($type); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
|
|
private function getAppUrl() |
30
|
|
|
{ |
31
|
|
|
return Config::get('app.url'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
private function getHtmlPath() |
38
|
|
|
{ |
39
|
|
|
return Config::get("{$this->getConfigFile()}.html_files"); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Where to generate the new documentation. |
44
|
|
|
* |
45
|
|
|
* @param $type |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
private function getDocumentationPath($type) |
50
|
|
|
{ |
51
|
|
|
return $this->getHtmlPath() . $this->getUrl($type); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $type |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
private function getJsonFilePath($type) |
60
|
|
|
{ |
61
|
|
|
return "app/Containers/Documentation/ApiDocJs/{$type}"; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
private function getConfigFile() |
68
|
|
|
{ |
69
|
|
|
return 'documentation-container'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
|
|
private function getTypeConfig() |
76
|
|
|
{ |
77
|
|
|
return Config::get($this->getConfigFile() . '.types'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
private function getExecutable() |
84
|
|
|
{ |
85
|
|
|
return Config::get($this->getConfigFile() . '.executable'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
|
private function getSwaggerConverter() |
92
|
|
|
{ |
93
|
|
|
return Config::get($this->getConfigFile() . '.swagger-converter'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $type |
98
|
|
|
* |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
private function getUrl($type) |
102
|
|
|
{ |
103
|
|
|
$configs = $this->getTypeConfig(); |
104
|
|
|
|
105
|
|
|
return $configs[$type]['url']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $type |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
private function getEndpointFiles($type) |
114
|
|
|
{ |
115
|
|
|
$configs = $this->getTypeConfig(); |
116
|
|
|
|
117
|
|
|
// what files types needs to be included |
118
|
|
|
$routeFilesCommand = []; |
119
|
|
|
$routes = $configs[$type]['routes']; |
120
|
|
|
|
121
|
|
|
foreach ($routes as $route) { |
122
|
|
|
$routeFilesCommand[] = '-f'; |
123
|
|
|
$routeFilesCommand[] = $route . '.php'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $routeFilesCommand; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param $templateKey |
132
|
|
|
* @param $value |
133
|
|
|
*/ |
134
|
|
|
private function replace($templateKey, $value) |
135
|
|
|
{ |
136
|
|
|
$this->headerMarkdownContent = str_replace($templateKey, $value, $this->headerMarkdownContent); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $minutes |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
private function minutesToTimeDisplay($minutes) |
145
|
|
|
{ |
146
|
|
|
$seconds = $minutes * 60; |
147
|
|
|
|
148
|
|
|
$dtF = new DateTime('@0'); |
149
|
|
|
$dtT = new DateTime("@$seconds"); |
150
|
|
|
|
151
|
|
|
return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: