. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Closure; use Fisharebest\Webtrees\Module\ModuleInterface; use Fisharebest\Webtrees\Module\ModuleThemeInterface; use Fisharebest\Webtrees\Services\ModuleService; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Throwable; use function method_exists; /** * Middleware to bootstrap the modules. */ class BootModules implements MiddlewareInterface { /** * @param Request $request * @param Closure $next * * @return Response * @throws Throwable */ public function handle(Request $request, Closure $next): Response { $module_service = app(ModuleService::class); $theme = app(ModuleThemeInterface::class); $bootable_modules = $module_service->all()->filter(function (ModuleInterface $module) { return method_exists($module, 'boot'); }); foreach ($bootable_modules as $module) { // Only bootstrap the current theme. if ($module instanceof ModuleThemeInterface && $module !== $theme) { continue; } app()->dispatch($module, 'boot'); } return $next($request); } }