Skip to content

Commit

Permalink
Added a response macro
Browse files Browse the repository at this point in the history
  • Loading branch information
RoccoHoward committed Jan 3, 2018
1 parent 333ed84 commit aab0135
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Response/MacroServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace HnhDigital\HelperCollection\Response;

use Illuminate\Support\Facades\Response;
use Illuminate\Support\ServiceProvider;

class MacroServiceProvider extends ServiceProvider
{
/**
* Register the application's response macros.
*
* @return void
*/
public function boot()
{
// Providesd the ability to download a file from a given path.
// Will also return a not modified header if file has not changed.
Response::macro('downloadAndCache', function ($file_path) {
if (!empty(request()->header('If-Modified-Since'))) {
$cache_last_modified = strtotime(request()->header('If-Modified-Since'));
$file_last_modified = filemtime($file_path);
if ($file_last_modified == $cache_last_modified) {
header('HTTP/1.1 304 Not Modified');
exit();
}
}

return Response::file($file_path);
});
}
}

0 comments on commit aab0135

Please sign in to comment.