You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1015 B

2 years ago
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Documents\ModelDocument;
  4. use Closure;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Str;
  7. class BindModelDocumentMiddleware
  8. {
  9. /**
  10. * Handle an incoming request.
  11. *
  12. * @param \Illuminate\Http\Request $request
  13. * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
  14. * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
  15. */
  16. public function handle(Request $request, Closure $next)
  17. {
  18. $name = empty($request->route('resource_name')) ?
  19. Str::of(explode('.', $request->route()->getName())[1])->singular()->value() :
  20. $request->route('resource_name');
  21. $modelDocument = ModelDocument::where('name', $name)
  22. ->with('storePolicy', 'updatePolicy', 'getPolicy', 'deletePolicy', 'restorePolicy')->first();
  23. app()->bind('modelDocument', fn() => $modelDocument);
  24. return $next($request);
  25. }
  26. }