2025-10-13 14:57:11 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use App\Http\Middleware\ForceJsonResponse;
|
2026-01-07 01:08:58 +01:00
|
|
|
use App\Http\Middleware\RequireSaasMode;
|
2026-01-05 23:51:50 +01:00
|
|
|
use App\Http\Middleware\RequireSubscription;
|
2025-10-13 14:57:11 +02:00
|
|
|
use App\Services\OutputService;
|
|
|
|
|
use Illuminate\Foundation\Application;
|
|
|
|
|
use Illuminate\Foundation\Configuration\Exceptions;
|
|
|
|
|
use Illuminate\Foundation\Configuration\Middleware;
|
|
|
|
|
use Illuminate\Http\Request;
|
2026-01-05 23:51:50 +01:00
|
|
|
use Illuminate\Support\Facades\Route;
|
2025-10-13 14:57:11 +02:00
|
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
|
|
|
|
|
|
return Application::configure(basePath: dirname(__DIR__))
|
|
|
|
|
->withRouting(
|
|
|
|
|
web: __DIR__.'/../routes/web.php',
|
|
|
|
|
api: __DIR__.'/../routes/api.php',
|
|
|
|
|
commands: __DIR__.'/../routes/console.php',
|
|
|
|
|
health: '/up',
|
2026-01-05 23:51:50 +01:00
|
|
|
then: function () {
|
|
|
|
|
Route::middleware('web')
|
|
|
|
|
->group(base_path('routes/web/subscription.php'));
|
|
|
|
|
},
|
2025-10-13 14:57:11 +02:00
|
|
|
)
|
|
|
|
|
->withMiddleware(function (Middleware $middleware) {
|
2025-12-27 21:18:09 +01:00
|
|
|
// Apply ForceJsonResponse only to API routes
|
|
|
|
|
$middleware->api(ForceJsonResponse::class);
|
2026-01-05 23:51:50 +01:00
|
|
|
|
|
|
|
|
$middleware->alias([
|
|
|
|
|
'subscription' => RequireSubscription::class,
|
2026-01-07 01:08:58 +01:00
|
|
|
'saas' => RequireSaasMode::class,
|
2026-01-05 23:51:50 +01:00
|
|
|
]);
|
2026-01-06 20:59:16 +01:00
|
|
|
|
|
|
|
|
// Exclude Stripe webhook from CSRF verification
|
|
|
|
|
$middleware->validateCsrfTokens(except: [
|
|
|
|
|
'stripe/webhook',
|
|
|
|
|
]);
|
2025-10-13 14:57:11 +02:00
|
|
|
})
|
|
|
|
|
->withExceptions(function (Exceptions $exceptions) {
|
|
|
|
|
$exceptions->shouldRenderJsonWhen(function (Request $request, Throwable $e) {
|
|
|
|
|
if ($request->is('api/*')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $request->expectsJson();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/** @var OutputService $outputService */
|
|
|
|
|
$outputService = resolve(OutputService::class);
|
|
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
$exceptions->render(function (ValidationException $e, Request $request) use ($outputService) {
|
|
|
|
|
if ($request->is('api/*') || $request->expectsJson()) {
|
|
|
|
|
return response()->json(
|
|
|
|
|
$outputService->response(false, null, [$e->getMessage()]),
|
|
|
|
|
404
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-10-13 14:57:11 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
$exceptions->render(function (NotFoundHttpException $e, Request $request) use ($outputService) {
|
|
|
|
|
if ($request->is('api/*') || $request->expectsJson()) {
|
|
|
|
|
return response()->json(
|
|
|
|
|
$outputService->response(false, null, ['MODEL_NOT_FOUND']),
|
|
|
|
|
404
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-10-13 14:57:11 +02:00
|
|
|
|
2025-12-29 02:57:25 +01:00
|
|
|
$exceptions->render(function (AccessDeniedHttpException $e, Request $request) use ($outputService) {
|
|
|
|
|
if ($request->is('api/*') || $request->expectsJson()) {
|
|
|
|
|
return response()->json(
|
|
|
|
|
$outputService->response(false, null, [$e->getMessage()]),
|
|
|
|
|
403
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-10-13 14:57:11 +02:00
|
|
|
})
|
|
|
|
|
->create();
|