From 66eea855557a8900e7ce7ac3ec1453844d65d926 Mon Sep 17 00:00:00 2001 From: "Mike P. Sinn" Date: Wed, 10 Jun 2020 17:57:57 -0500 Subject: [PATCH 1/3] Use absolute paths and make sure js and css folders exist --- src/Writing/Writer.php | 61 ++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/src/Writing/Writer.php b/src/Writing/Writer.php index 4c03f2f7..88038471 100644 --- a/src/Writing/Writer.php +++ b/src/Writing/Writer.php @@ -91,8 +91,8 @@ public function writeDocs(Collection $routes) */ public function writeMarkdownAndSourceFiles(Collection $parsedRoutes) { - $targetFile = $this->sourceOutputPath . '/source/index.md'; - $compareFile = $this->sourceOutputPath . '/source/.compare.md'; + $targetFile = $this->getSourceOutputPath() . '/source/index.md'; + $compareFile = $this->getSourceOutputPath() . '/source/.compare.md'; $infoText = view('apidoc::partials.info') ->with('outputPath', 'docs') @@ -146,11 +146,11 @@ public function writeMarkdownAndSourceFiles(Collection $parsedRoutes) ->with('showPostmanCollectionButton', $this->shouldGeneratePostmanCollection) ->with('parsedRoutes', $parsedRouteOutput); - $this->output->info('Writing index.md and source files to: ' . $this->sourceOutputPath); + $this->output->info('Writing index.md and source files to: ' . $this->getSourceOutputPath()); - if (! is_dir($this->sourceOutputPath)) { + if (! is_dir($this->getSourceOutputPath())) { $documentarian = new Documentarian(); - $documentarian->create($this->sourceOutputPath); + $documentarian->create($this->getSourceOutputPath()); } // Write output file @@ -169,7 +169,7 @@ public function writeMarkdownAndSourceFiles(Collection $parsedRoutes) file_put_contents($compareFile, $compareMarkdown); - $this->output->info('Wrote index.md and source files to: ' . $this->sourceOutputPath); + $this->output->info('Wrote index.md and source files to: ' . $this->getSourceOutputPath()); } public function generateMarkdownOutputForEachRoute(Collection $parsedRoutes, array $settings): Collection @@ -203,7 +203,7 @@ protected function writePostmanCollection(Collection $parsedRoutes): void $collection = $this->generatePostmanCollection($parsedRoutes); if ($this->isStatic) { - $collectionPath = "{$this->outputPath}/collection.json"; + $collectionPath = "{$this->getOutputPath()}/collection.json"; file_put_contents($collectionPath, $collection); } else { $storageInstance = Storage::disk($this->config->get('storage')); @@ -239,7 +239,7 @@ public function generatePostmanCollection(Collection $routes) protected function getMarkdownToPrepend(): string { - $prependFile = $this->sourceOutputPath . '/source/prepend.md'; + $prependFile = $this->getSourceOutputPath() . '/source/prepend.md'; $prependFileContents = file_exists($prependFile) ? file_get_contents($prependFile) . "\n" : ''; @@ -248,7 +248,7 @@ protected function getMarkdownToPrepend(): string protected function getMarkdownToAppend(): string { - $appendFile = $this->sourceOutputPath . '/source/append.md'; + $appendFile = $this->getSourceOutputPath() . '/source/append.md'; $appendFileContents = file_exists($appendFile) ? "\n" . file_get_contents($appendFile) : ''; @@ -257,39 +257,50 @@ protected function getMarkdownToAppend(): string protected function copyAssetsFromSourceFolderToPublicFolder(): void { - $publicPath = $this->config->get('output_folder') ?? 'public/docs'; + $publicPath = base_path($this->config->get('output_folder') ?? base_path('public/docs')); if (! is_dir($publicPath)) { mkdir($publicPath, 0777, true); - mkdir("{$publicPath}/css"); - mkdir("{$publicPath}/js"); } - copy("{$this->sourceOutputPath}/js/all.js", "{$publicPath}/js/all.js"); - rcopy("{$this->sourceOutputPath}/images", "{$publicPath}/images"); - rcopy("{$this->sourceOutputPath}/css", "{$publicPath}/css"); + + if (! is_dir("{$publicPath}/css")) { + mkdir("{$publicPath}/css", 0777, true); + } + + if (! is_dir("{$publicPath}/js")) { + mkdir("{$publicPath}/js", 0777, true); + } + + copy("{$this->getSourceOutputPath()}/js/all.js", "{$publicPath}/js/all.js"); + rcopy("{$this->getSourceOutputPath()}/images", "{$publicPath}/images"); + rcopy("{$this->getSourceOutputPath()}/css", "{$publicPath}/css"); if ($logo = $this->config->get('logo')) { copy($logo, "{$publicPath}/images/logo.png"); } } + protected function getSourceOutputPath(): string { + return base_path($this->sourceOutputPath); + } + protected function moveOutputFromSourceFolderToTargetFolder(): void { if ($this->isStatic) { // Move output (index.html, css/style.css and js/all.js) to public/docs - rename("{$this->sourceOutputPath}/index.html", "{$this->outputPath}/index.html"); + rename("{$this->getSourceOutputPath()}/index.html", "{$this->getOutputPath()}/index.html"); } else { // Move output to resources/views - if (! is_dir($this->outputPath)) { - mkdir($this->outputPath); + if (! is_dir($this->getOutputPath())) { + mkdir($this->getOutputPath()); } - rename("{$this->sourceOutputPath}/index.html", "$this->outputPath/index.blade.php"); - $contents = file_get_contents("$this->outputPath/index.blade.php"); + rename("{$this->getSourceOutputPath()}/index.html", "$this->getOutputPath()/index.blade.php"); + $contents = file_get_contents("$this->getOutputPath()/index.blade.php"); // $contents = str_replace('href="css/style.css"', 'href="{{ asset(\'/docs/css/style.css\') }}"', $contents); $contents = str_replace('src="js/all.js"', 'src="{{ asset(\'/docs/js/all.js\') }}"', $contents); $contents = str_replace('src="images/', 'src="/docs/images/', $contents); $contents = preg_replace('#href="https?://.+?/docs/collection.json"#', 'href="{{ route("apidoc.json") }}"', $contents); - file_put_contents("$this->outputPath/index.blade.php", $contents); + file_put_contents("$this->getOutputPath()/index.blade.php", $contents); } } @@ -297,13 +308,17 @@ public function writeHtmlDocs(): void { $this->output->info('Generating API HTML code'); - $this->documentarian->generate($this->sourceOutputPath); + $this->documentarian->generate($this->getSourceOutputPath()); // Move assets to public folder $this->copyAssetsFromSourceFolderToPublicFolder(); $this->moveOutputFromSourceFolderToTargetFolder(); - $this->output->info("Wrote HTML documentation to: {$this->outputPath}"); + $this->output->info("Wrote HTML documentation to: {$this->getOutputPath()}"); + } + + protected function getOutputPath(): string { + return base_path($this->outputPath); } } From a0dd11844447addc607f997bcd66d965e6eb2abd Mon Sep 17 00:00:00 2001 From: "Mike P. Sinn" Date: Wed, 10 Jun 2020 18:04:44 -0500 Subject: [PATCH 2/3] Complying with oppressive StyleCI demands --- src/Writing/Writer.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Writing/Writer.php b/src/Writing/Writer.php index 88038471..91946e65 100644 --- a/src/Writing/Writer.php +++ b/src/Writing/Writer.php @@ -279,7 +279,8 @@ protected function copyAssetsFromSourceFolderToPublicFolder(): void } } - protected function getSourceOutputPath(): string { + protected function getSourceOutputPath(): string + { return base_path($this->sourceOutputPath); } @@ -318,7 +319,8 @@ public function writeHtmlDocs(): void $this->output->info("Wrote HTML documentation to: {$this->getOutputPath()}"); } - protected function getOutputPath(): string { + protected function getOutputPath(): string + { return base_path($this->outputPath); } } From 8b388db6c438c56eb5572f129ffb8b6aab901fd3 Mon Sep 17 00:00:00 2001 From: "Mike P. Sinn" Date: Thu, 11 Jun 2020 13:46:25 -0500 Subject: [PATCH 3/3] phpstan apparently doesn't like calling function using complex interpolation syntax --- src/Writing/Writer.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Writing/Writer.php b/src/Writing/Writer.php index 91946e65..9c741b7d 100644 --- a/src/Writing/Writer.php +++ b/src/Writing/Writer.php @@ -203,7 +203,7 @@ protected function writePostmanCollection(Collection $parsedRoutes): void $collection = $this->generatePostmanCollection($parsedRoutes); if ($this->isStatic) { - $collectionPath = "{$this->getOutputPath()}/collection.json"; + $collectionPath = $this->getOutputPath()."/collection.json"; file_put_contents($collectionPath, $collection); } else { $storageInstance = Storage::disk($this->config->get('storage')); @@ -286,22 +286,23 @@ protected function getSourceOutputPath(): string protected function moveOutputFromSourceFolderToTargetFolder(): void { + $outputPath = $this->getOutputPath(); if ($this->isStatic) { // Move output (index.html, css/style.css and js/all.js) to public/docs - rename("{$this->getSourceOutputPath()}/index.html", "{$this->getOutputPath()}/index.html"); + rename($this->getSourceOutputPath()."/index.html", "{$outputPath}/index.html"); } else { // Move output to resources/views - if (! is_dir($this->getOutputPath())) { - mkdir($this->getOutputPath()); + if (! is_dir($outputPath)) { + mkdir($outputPath); } - rename("{$this->getSourceOutputPath()}/index.html", "$this->getOutputPath()/index.blade.php"); - $contents = file_get_contents("$this->getOutputPath()/index.blade.php"); + rename($this->getSourceOutputPath()."/index.html", "$outputPath/index.blade.php"); + $contents = file_get_contents("$outputPath/index.blade.php"); // $contents = str_replace('href="css/style.css"', 'href="{{ asset(\'/docs/css/style.css\') }}"', $contents); $contents = str_replace('src="js/all.js"', 'src="{{ asset(\'/docs/js/all.js\') }}"', $contents); $contents = str_replace('src="images/', 'src="/docs/images/', $contents); $contents = preg_replace('#href="https?://.+?/docs/collection.json"#', 'href="{{ route("apidoc.json") }}"', $contents); - file_put_contents("$this->getOutputPath()/index.blade.php", $contents); + file_put_contents("$outputPath/index.blade.php", $contents); } } @@ -316,7 +317,7 @@ public function writeHtmlDocs(): void $this->moveOutputFromSourceFolderToTargetFolder(); - $this->output->info("Wrote HTML documentation to: {$this->getOutputPath()}"); + $this->output->info("Wrote HTML documentation to: ".$this->getOutputPath()); } protected function getOutputPath(): string