Skip to content

Commit

Permalink
#94010 Blade has used local assets now
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeny Bereza committed Nov 3, 2022
1 parent 8db45e7 commit 0a442ee
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 210 deletions.
33 changes: 33 additions & 0 deletions src/Controllers/StoplightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ensi\LaravelServeStoplight\Controllers;

use DateTime;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand All @@ -21,4 +22,36 @@ public function documentation(?string $version): View
throw new NotFoundHttpException();
}
}

public function asset($asset, $ext): Response
{
$assetFile = $asset . '.' . $ext;
$path = $this->distPath($assetFile);

return (new Response(file_get_contents($path), 200, [
'Content-Type' => (pathinfo($path))['extension'] == 'css' ? 'text/css' : 'application/javascript',
]))->setSharedMaxAge(31536000)
->setMaxAge(31536000)
->setExpires(new DateTime('+1 year'));
}

protected function distPath($asset = null): string
{
$allowedFiles = [
'web-components.min.js',
'styles.min.css',
];

$path = base_path('vendor/ensi/laravel-serve-stoplight/packages/elements/dist/');

if (!$asset) {
return realpath($path);
}

if (!in_array($asset, $allowedFiles)) {
throw new NotFoundHttpException();
}

return realpath($path . $asset);
}
}
1 change: 1 addition & 0 deletions src/ServeStoplightServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function boot()
->name('serve-stoplight.')
->prefix(config('serve-stoplight.path'))
->group(function () {
Route::get('assets/{asset}/{ext}', [StoplightController::class, 'asset'])->name('asset');
Route::get('{version?}', [StoplightController::class, 'documentation']);
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/views/stoplight.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>{{ $name }}</title>
<!-- Embed elements Elements via Web Component -->
<script src="https://unpkg.com/@stoplight/elements@v7/web-components.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@stoplight/elements@v7/styles.min.css">
<script src="{{ route('serve-stoplight.asset', ['asset' => 'web-components.min', 'ext' => 'js'], false) }}"></script>
<link rel="stylesheet" href="{{ route('serve-stoplight.asset', ['asset' => 'styles.min', 'ext' => 'css'], false) }}">
</head>
<body>

Expand Down
Loading

0 comments on commit 0a442ee

Please sign in to comment.