Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use absolute paths and make sure the public/docs/js and public/docs/css sub-folders exist #761

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 41 additions & 23 deletions src/Writing/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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'));
Expand Down Expand Up @@ -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" : '';

Expand All @@ -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) : '';

Expand All @@ -257,53 +257,71 @@ 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
{
$outputPath = $this->getOutputPath();
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", "{$outputPath}/index.html");
} else {
// Move output to resources/views
if (! is_dir($this->outputPath)) {
mkdir($this->outputPath);
if (! is_dir($outputPath)) {
mkdir($outputPath);
}
rename("{$this->sourceOutputPath}/index.html", "$this->outputPath/index.blade.php");
$contents = file_get_contents("$this->outputPath/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->outputPath/index.blade.php", $contents);
file_put_contents("$outputPath/index.blade.php", $contents);
}
}

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);
}
}