2016-07-21 22:01:57 +08:00
|
|
|
<?php
|
|
|
|
|
2016-08-28 10:05:21 +08:00
|
|
|
namespace App\Http\Middleware;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-10-16 18:16:15 +08:00
|
|
|
use App\Models\Player;
|
2016-08-30 08:55:02 +08:00
|
|
|
use App\Events\CheckPlayerExists;
|
|
|
|
use Event;
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-08-28 10:05:21 +08:00
|
|
|
class CheckPlayerExistMiddleware
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
2016-08-28 10:05:21 +08:00
|
|
|
public function handle($request, \Closure $next)
|
2016-07-21 22:01:57 +08:00
|
|
|
{
|
|
|
|
if (stripos($request->getUri(), '.json') != false) {
|
|
|
|
preg_match('/\/([^\/]*)\.json/', $request->getUri(), $matches);
|
|
|
|
} else {
|
|
|
|
preg_match('/\/([^\/]*)\.png/', $request->getUri(), $matches);
|
|
|
|
}
|
|
|
|
|
2016-08-29 15:10:51 +08:00
|
|
|
$player_name = urldecode($matches[1]);
|
2016-07-21 22:01:57 +08:00
|
|
|
|
2016-08-30 08:55:02 +08:00
|
|
|
Event::fire(new CheckPlayerExists($player_name));
|
|
|
|
|
2016-10-16 18:16:15 +08:00
|
|
|
if (Player::where('player_name', $player_name)->get()->isEmpty()) {
|
2016-09-25 14:21:51 +08:00
|
|
|
if (option('return_200_when_notfound') == "1") {
|
|
|
|
return json([
|
|
|
|
'player_name' => $player_name,
|
|
|
|
'errno' => 404,
|
|
|
|
'msg' => 'Player Not Found.'
|
2016-10-03 21:57:55 +08:00
|
|
|
])->header('Cache-Control', 'public, max-age='.option('cache_expire_time'));
|
2016-09-25 14:21:51 +08:00
|
|
|
} else {
|
|
|
|
abort(404, trans('general.unexistent-player'));
|
|
|
|
}
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
|
2016-08-28 10:05:21 +08:00
|
|
|
return $next($request);
|
|
|
|
|
2016-07-21 22:01:57 +08:00
|
|
|
}
|
|
|
|
}
|