2016-08-29 22:48:55 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
|
|
|
use Response;
|
2016-09-25 11:40:50 +08:00
|
|
|
use Illuminate\Support\Arr;
|
2016-08-29 22:48:55 +08:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
|
|
|
|
class ResponseMacroServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Perform post-registration booting of services.
|
|
|
|
*
|
|
|
|
* @param ResponseFactory $factory
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot()
|
|
|
|
{
|
|
|
|
Response::macro('png', function ($src = "", $status = 200, $header = []) {
|
2016-09-25 11:40:50 +08:00
|
|
|
$last_modified = Arr::pull($header, 'Last-Modified', time());
|
|
|
|
$etag = md5($src);
|
|
|
|
|
|
|
|
// Checking if the client is validating his cache and if it is current.
|
|
|
|
if ((strtotime(Arr::get($_SERVER, 'If-Modified-Since')) == $last_modified) ||
|
|
|
|
trim(Arr::get($_SERVER, 'HTTP_IF_NONE_MATCH')) == $etag
|
|
|
|
) {
|
|
|
|
// Client's cache IS current, so we just respond '304 Not Modified'.
|
|
|
|
$status = 304;
|
|
|
|
$src = "";
|
|
|
|
}
|
|
|
|
|
2016-08-29 22:48:55 +08:00
|
|
|
return Response::stream(function() use ($src, $status) {
|
|
|
|
echo $src;
|
|
|
|
}, $status, array_merge([
|
2016-09-25 11:40:50 +08:00
|
|
|
'Content-type' => 'image/png',
|
|
|
|
'Last-Modified' => gmdate('D, d M Y H:i:s', $last_modified).' GMT',
|
2016-09-25 14:21:51 +08:00
|
|
|
'Cache-Control' => 'public, max-age='.option('cache_expire_time'), // 365 days
|
|
|
|
'Expires' => gmdate('D, d M Y H:i:s', $last_modified + option('cache_expire_time')).' GMT',
|
2016-09-25 11:40:50 +08:00
|
|
|
'Etag' => $etag
|
2016-08-29 22:48:55 +08:00
|
|
|
], $header));
|
|
|
|
});
|
2016-09-04 15:35:12 +08:00
|
|
|
|
|
|
|
Response::macro('rawJson', function ($src = "", $status = 200, $header = []) {
|
|
|
|
return Response::make($src)->header('Content-type', 'application/json');
|
|
|
|
});
|
2016-08-29 22:48:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|