1010
1111class Writer
1212{
13- /**
14- * @var Collection
15- */
16- protected $ routes ;
17-
1813 /**
1914 * @var Command
2015 */
@@ -45,46 +40,59 @@ class Writer
4540 */
4641 private $ documentarian ;
4742
48- public function __construct (Collection $ routes , bool $ forceIt , Command $ output , DocumentationConfig $ config = null )
43+ /**
44+ * @var bool
45+ */
46+ private $ isStatic ;
47+
48+ /**
49+ * @var string
50+ */
51+ private $ sourceOutputPath ;
52+
53+ /**
54+ * @var string
55+ */
56+ private $ outputPath ;
57+
58+ public function __construct (Command $ output , DocumentationConfig $ config = null , bool $ forceIt = false )
4959 {
50- $ this ->routes = $ routes ;
5160 // If no config is injected, pull from global
5261 $ this ->config = $ config ?: new DocumentationConfig (config ('apidoc ' ));
5362 $ this ->baseUrl = $ this ->config ->get ('base_url ' ) ?? config ('app.url ' );
5463 $ this ->forceIt = $ forceIt ;
5564 $ this ->output = $ output ;
5665 $ this ->shouldGeneratePostmanCollection = $ this ->config ->get ('postman.enabled ' , false );
5766 $ this ->documentarian = new Documentarian ();
67+ $ this ->isStatic = $ this ->config ->get ('type ' ) === 'static ' ;
68+ $ this ->sourceOutputPath = 'resources/docs ' ;
69+ $ this ->outputPath = $ this ->isStatic ? 'public/docs ' : 'resources/views/apidoc ' ;
5870 }
5971
60- public function writeDocs ()
72+ public function writeDocs (Collection $ routes )
6173 {
6274 // The source files (index.md, js/, css/, and images/) always go in resources/docs/source.
6375 // The static assets (js/, css/, and images/) always go in public/docs/.
6476 // For 'static' docs, the output files (index.html, collection.json) go in public/docs/.
6577 // For 'laravel' docs, the output files (index.blade.php, collection.json)
6678 // go in resources/views/apidoc/ and storage/app/apidoc/ respectively.
67- $ isStatic = $ this ->config ->get ('type ' ) === 'static ' ;
6879
69- $ sourceOutputPath = 'resources/docs ' ;
70- $ outputPath = $ isStatic ? 'public/docs ' : 'resources/views/apidoc ' ;
80+ $ this ->writeMarkdownAndSourceFiles ($ routes );
7181
72- $ this ->writeMarkdownAndSourceFiles ( $ this -> routes , $ sourceOutputPath );
82+ $ this ->writeHtmlDocs ( );
7383
74- $ this ->writeHtmlDocs ($ sourceOutputPath , $ outputPath , $ isStatic );
75-
76- $ this ->writePostmanCollection ($ this ->routes , $ outputPath , $ isStatic );
84+ $ this ->writePostmanCollection ($ routes );
7785 }
7886
7987 /**
8088 * @param Collection $parsedRoutes
8189 *
8290 * @return void
8391 */
84- public function writeMarkdownAndSourceFiles (Collection $ parsedRoutes, string $ sourceOutputPath )
92+ public function writeMarkdownAndSourceFiles (Collection $ parsedRoutes )
8593 {
86- $ targetFile = $ sourceOutputPath .'/source/index.md ' ;
87- $ compareFile = $ sourceOutputPath .'/source/.compare.md ' ;
94+ $ targetFile = $ this -> sourceOutputPath .'/source/index.md ' ;
95+ $ compareFile = $ this -> sourceOutputPath .'/source/.compare.md ' ;
8896
8997 $ infoText = view ('apidoc::partials.info ' )
9098 ->with ('outputPath ' , 'docs ' )
@@ -125,8 +133,8 @@ public function writeMarkdownAndSourceFiles(Collection $parsedRoutes, string $so
125133 });
126134 }
127135
128- $ prependFileContents = $ this ->getMarkdownToPrepend ($ sourceOutputPath );
129- $ appendFileContents = $ this ->getMarkdownToAppend ($ sourceOutputPath );
136+ $ prependFileContents = $ this ->getMarkdownToPrepend ();
137+ $ appendFileContents = $ this ->getMarkdownToAppend ();
130138
131139 $ markdown = view ('apidoc::documentarian ' )
132140 ->with ('writeCompareFile ' , false )
@@ -138,11 +146,11 @@ public function writeMarkdownAndSourceFiles(Collection $parsedRoutes, string $so
138146 ->with ('showPostmanCollectionButton ' , $ this ->shouldGeneratePostmanCollection )
139147 ->with ('parsedRoutes ' , $ parsedRouteOutput );
140148
141- $ this ->output ->info ('Writing index.md and source files to: ' .$ sourceOutputPath );
149+ $ this ->output ->info ('Writing index.md and source files to: ' .$ this -> sourceOutputPath );
142150
143- if (! is_dir ($ sourceOutputPath )) {
151+ if (! is_dir ($ this -> sourceOutputPath )) {
144152 $ documentarian = new Documentarian ();
145- $ documentarian ->create ($ sourceOutputPath );
153+ $ documentarian ->create ($ this -> sourceOutputPath );
146154 }
147155
148156 // Write output file
@@ -161,7 +169,7 @@ public function writeMarkdownAndSourceFiles(Collection $parsedRoutes, string $so
161169
162170 file_put_contents ($ compareFile , $ compareMarkdown );
163171
164- $ this ->output ->info ('Wrote index.md and source files to: ' .$ sourceOutputPath );
172+ $ this ->output ->info ('Wrote index.md and source files to: ' .$ this -> sourceOutputPath );
165173 }
166174
167175 public function generateMarkdownOutputForEachRoute (Collection $ parsedRoutes , array $ settings ): Collection
@@ -188,14 +196,14 @@ public function generateMarkdownOutputForEachRoute(Collection $parsedRoutes, arr
188196 return $ parsedRouteOutput ;
189197 }
190198
191- protected function writePostmanCollection (Collection $ parsedRoutes, string $ outputPath , bool $ isStatic ): void
199+ protected function writePostmanCollection (Collection $ parsedRoutes ): void
192200 {
193201 if ($ this ->shouldGeneratePostmanCollection ) {
194202 $ this ->output ->info ('Generating Postman collection ' );
195203
196204 $ collection = $ this ->generatePostmanCollection ($ parsedRoutes );
197- if ($ isStatic ) {
198- $ collectionPath = "{$ outputPath }/collection.json " ;
205+ if ($ this -> isStatic ) {
206+ $ collectionPath = "{$ this -> outputPath }/collection.json " ;
199207 file_put_contents ($ collectionPath , $ collection );
200208 } else {
201209 Storage::disk ('local ' )->put ('apidoc/collection.json ' , $ collection );
@@ -220,90 +228,75 @@ public function generatePostmanCollection(Collection $routes)
220228 return $ writer ->getCollection ();
221229 }
222230
223- /**
224- * @param string $sourceOutputPath
225- *
226- * @return string
227- */
228- protected function getMarkdownToPrepend (string $ sourceOutputPath ): string
231+ protected function getMarkdownToPrepend (): string
229232 {
230- $ prependFile = $ sourceOutputPath .'/source/prepend.md ' ;
233+ $ prependFile = $ this -> sourceOutputPath .'/source/prepend.md ' ;
231234 $ prependFileContents = file_exists ($ prependFile )
232235 ? file_get_contents ($ prependFile )."\n" : '' ;
233236
234237 return $ prependFileContents ;
235238 }
236239
237- /**
238- * @param string $sourceOutputPath
239- *
240- * @return string
241- */
242- protected function getMarkdownToAppend (string $ sourceOutputPath ): string
240+ protected function getMarkdownToAppend (): string
243241 {
244- $ appendFile = $ sourceOutputPath .'/source/append.md ' ;
242+ $ appendFile = $ this -> sourceOutputPath .'/source/append.md ' ;
245243 $ appendFileContents = file_exists ($ appendFile )
246244 ? "\n" .file_get_contents ($ appendFile ) : '' ;
247245
248246 return $ appendFileContents ;
249247 }
250248
251- protected function copyAssetsFromSourceFolderToPublicFolder (string $ sourceOutputPath , bool $ isStatic = true ): void
249+ protected function copyAssetsFromSourceFolderToPublicFolder (): void
252250 {
253251 $ publicPath = 'public/docs ' ;
254252 if (! is_dir ($ publicPath )) {
255253 mkdir ($ publicPath , 0777 , true );
256254 mkdir ("{$ publicPath }/css " );
257255 mkdir ("{$ publicPath }/js " );
258256 }
259- copy ("{$ sourceOutputPath }/js/all.js " , "{$ publicPath }/js/all.js " );
260- rcopy ("{$ sourceOutputPath }/images " , "{$ publicPath }/images " );
261- rcopy ("{$ sourceOutputPath }/css " , "{$ publicPath }/css " );
257+ copy ("{$ this -> sourceOutputPath }/js/all.js " , "{$ publicPath }/js/all.js " );
258+ rcopy ("{$ this -> sourceOutputPath }/images " , "{$ publicPath }/images " );
259+ rcopy ("{$ this -> sourceOutputPath }/css " , "{$ publicPath }/css " );
262260
263261 if ($ logo = $ this ->config ->get ('logo ' )) {
264- if ($ isStatic ) {
262+ if ($ this -> isStatic ) {
265263 copy ($ logo , "{$ publicPath }/images/logo.png " );
266264 }
267265 }
268266 }
269267
270- protected function moveOutputFromSourceFolderToTargetFolder (string $ sourceOutputPath , string $ outputPath , bool $ isStatic ): void
268+ protected function moveOutputFromSourceFolderToTargetFolder (): void
271269 {
272- if ($ isStatic ) {
270+ if ($ this -> isStatic ) {
273271 // Move output (index.html, css/style.css and js/all.js) to public/docs
274- rename ("{$ sourceOutputPath }/index.html " , "{$ outputPath }/index.html " );
272+ rename ("{$ this -> sourceOutputPath }/index.html " , "{$ this -> outputPath }/index.html " );
275273 } else {
276274 // Move output to resources/views
277- if (! is_dir ($ outputPath )) {
278- mkdir ($ outputPath );
275+ if (! is_dir ($ this -> outputPath )) {
276+ mkdir ($ this -> outputPath );
279277 }
280- rename ("{$ sourceOutputPath }/index.html " , "$ outputPath/index.blade.php " );
281- $ contents = file_get_contents ("$ outputPath/index.blade.php " );
278+ rename ("{$ this -> sourceOutputPath }/index.html " , "$ this -> outputPath /index.blade.php " );
279+ $ contents = file_get_contents ("$ this -> outputPath /index.blade.php " );
282280 //
283281 $ contents = str_replace ('href="css/style.css" ' , 'href="/docs/css/style.css" ' , $ contents );
284282 $ contents = str_replace ('src="js/all.js" ' , 'src="/docs/js/all.js" ' , $ contents );
285283 $ contents = str_replace ('src="images/ ' , 'src="/docs/images/ ' , $ contents );
286284 $ contents = preg_replace ('#href="http://.+?/docs/collection.json"# ' , 'href="{{ route("apidoc", ["format" => ".json"]) }}" ' , $ contents );
287- file_put_contents ("$ outputPath/index.blade.php " , $ contents );
285+ file_put_contents ("$ this -> outputPath /index.blade.php " , $ contents );
288286 }
289287 }
290288
291- /**
292- * @param string $sourceOutputPath
293- * @param string $outputPath
294- * @param bool $isStatic
295- */
296- protected function writeHtmlDocs (string $ sourceOutputPath , string $ outputPath , bool $ isStatic ): void
289+ public function writeHtmlDocs (): void
297290 {
298291 $ this ->output ->info ('Generating API HTML code ' );
299292
300- $ this ->documentarian ->generate ($ sourceOutputPath );
293+ $ this ->documentarian ->generate ($ this -> sourceOutputPath );
301294
302295 // Move assets to public folder
303- $ this ->copyAssetsFromSourceFolderToPublicFolder ($ sourceOutputPath , $ isStatic );
296+ $ this ->copyAssetsFromSourceFolderToPublicFolder ();
304297
305- $ this ->moveOutputFromSourceFolderToTargetFolder ($ sourceOutputPath , $ outputPath , $ isStatic );
298+ $ this ->moveOutputFromSourceFolderToTargetFolder ();
306299
307- $ this ->output ->info ("Wrote HTML documentation to: {$ outputPath }" );
300+ $ this ->output ->info ("Wrote HTML documentation to: {$ this -> outputPath }" );
308301 }
309302}
0 commit comments