Skip to content

Instantly share code, notes, and snippets.

@felthy
Created June 4, 2014 00:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felthy/3fc1675a6a89db891396 to your computer and use it in GitHub Desktop.
Save felthy/3fc1675a6a89db891396 to your computer and use it in GitHub Desktop.
Use laravel-cachebuster in conjunction with Laravel's built-in PHP development web server. A slightly modified version of code by @RTC1
if (App::runningUnitTests() && is_development_server()) {
/**
* CACHE BUSTERS
*/
Route::pattern('hash', '[a-zA-Z0-9]+');
Route::pattern('allowedExtensions', '(jpg|png|gif|js|css|woff|ttf|svg|eot){1}');
Route::pattern('folders', '[a-zA-Z0-9_\/]*');
Route::pattern('fileName', '.+');
$guesser = \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser::getInstance();
Route::get(
'{folders}{fileName}-{hash}.{allowedExtensions}',
array(
function ($folders, $fileName, $hash, $extension) use ($guesser) {
$shortPath = $folders . $fileName . '.' . $extension;
$path = public_path() . DIRECTORY_SEPARATOR . $shortPath;
if (!file_exists($path)) {
return App::abort(404);
}
$headers = [
'content-type' => $guesser->guess($path),
'Cache-Control' => 'max-age=31536000',
'Pragma' => 'cache',
'Expires' => 'Sun, 17 Jan 2038 19:14:07 GMT'
];
if (strtolower($extension) == 'css') {
return Response::make(Bust::css($shortPath), 200, $headers);
}
return Response::make(file_get_contents($path), 200, $headers);
}
)
);
}
/*
|--------------------------------------------------------------------------
| Helper to know whether we're running in Laravel's built-in dev server
|--------------------------------------------------------------------------
*/
if (!function_exists('is_development_server')) {
function is_development_server()
{
// See whether we're running from the testing server by inspecting the stack
$trace = debug_backtrace();
$bottom = end($trace);
return isset($bottom['file']) && ends_with($bottom['file'], '/server.php');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment