Skip to content

Commit

Permalink
Merge pull request #462 from WordsofDefiance/prefix-for-public-larave…
Browse files Browse the repository at this point in the history
…l-resources

add assets directory functionality
  • Loading branch information
shalvah authored Apr 29, 2022
2 parents c82acf5 + 9fdbed6 commit 89456b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
7 changes: 7 additions & 0 deletions config/scribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@
*/
'docs_url' => '/docs',

/*
* Specify directory within `public` in which to store Laravel Scribe assets.
* By default, assets are stored in `public/vendor/scribe`.
* If asset_prefix != null, assets will be stored in `public/{{asset_prefix}}/vendor/scribe`
*/
'assets_directory' => null,

/*
* Middleware to attach to the docs endpoint (if `add_routes` is true).
*/
Expand Down
23 changes: 15 additions & 8 deletions src/Writing/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ class Writer
]
];

private string $laravelAssetsPath;

public function __construct(DocumentationConfig $config = null)
{
// If no config is injected, pull from global. Makes testing easier.
$this->config = $config ?: new DocumentationConfig(config('scribe'));

$this->isStatic = $this->config->get('type') === 'static';
$this->staticTypeOutputPath = rtrim($this->config->get('static.output_path', 'public/docs'), '/');

$this->laravelAssetsPath = $this->config->get('laravel.assets_directory')
? '/' . $this->config->get('laravel.assets_directory')
: '/vendor/scribe';
}

/**
Expand Down Expand Up @@ -146,24 +152,24 @@ protected function performFinalTasksForLaravelType(): void
mkdir($this->laravelTypeOutputPath, 0777, true);
}
$publicDirectory = app()->get('path.public');
if (!is_dir("$publicDirectory/vendor/scribe")) {
mkdir("$publicDirectory/vendor/scribe", 0777, true);
if (!is_dir($publicDirectory . $this->laravelAssetsPath)) {
mkdir($publicDirectory . $this->laravelAssetsPath, 0777, true);
}


// Transform output HTML to a Blade view
rename("{$this->staticTypeOutputPath}/index.html", "$this->laravelTypeOutputPath/index.blade.php");

// Move assets from public/docs to public/vendor/scribe
// Move assets from public/docs to public/vendor/scribe or config('laravel.assets_directory')
// We need to do this delete first, otherwise move won't work if folder exists
Utils::deleteDirectoryAndContents("$publicDirectory/vendor/scribe/");
rename("{$this->staticTypeOutputPath}/", "$publicDirectory/vendor/scribe/");
Utils::deleteDirectoryAndContents($publicDirectory . $this->laravelAssetsPath);
rename("{$this->staticTypeOutputPath}/", $publicDirectory . $this->laravelAssetsPath);

$contents = file_get_contents("$this->laravelTypeOutputPath/index.blade.php");

// Rewrite asset links to go through Laravel
$contents = preg_replace('#href="\.\./docs/css/(.+?)"#', 'href="{{ asset("vendor/scribe/css/$1") }}"', $contents);
$contents = preg_replace('#src="\.\./docs/(js|images)/(.+?)"#', 'src="{{ asset("vendor/scribe/$1/$2") }}"', $contents);
$contents = preg_replace('#href="\.\./docs/css/(.+?)"#', 'href="{{ asset("' . $this->laravelAssetsPath . '/css/$1") }}"', $contents);
$contents = preg_replace('#src="\.\./docs/(js|images)/(.+?)"#', 'src="{{ asset("' . $this->laravelAssetsPath . '/$1/$2") }}"', $contents);
$contents = str_replace('href="../docs/collection.json"', 'href="{{ route("scribe.postman") }}"', $contents);
$contents = str_replace('href="../docs/openapi.yaml"', 'href="{{ route("scribe.openapi") }}"', $contents);

Expand Down Expand Up @@ -192,7 +198,8 @@ public function writeHtmlDocs(array $groupedEndpoints): void
$outputPath = rtrim($this->laravelTypeOutputPath, '/') . '/';
c::success("Wrote Blade docs to: $outputPath");
$this->generatedFiles['blade'] = realpath("{$outputPath}index.blade.php");
$assetsOutputPath = app()->get('path.public')."/vendor/scribe/";
$assetsOutputPath = app()->get('path.public').$this->laravelAssetsPath;
c::success("Wrote Laravel assets to: " . realpath($assetsOutputPath));
}
$this->generatedFiles['assets']['js'] = realpath("{$assetsOutputPath}js");
$this->generatedFiles['assets']['css'] = realpath("{$assetsOutputPath}css");
Expand Down

0 comments on commit 89456b8

Please sign in to comment.