. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Fisharebest\Webtrees\Module\ModuleInterface; use Fisharebest\Webtrees\Module\ModuleThemeInterface; use Fisharebest\Webtrees\Services\ModuleService; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use function method_exists; /** * Middleware to bootstrap the modules. */ class BootModules implements MiddlewareInterface { /** @var ModuleService */ private $module_service; /** @var ModuleThemeInterface */ private $theme; /** * BootModules constructor. * * @param ModuleService $module_service * @param ModuleThemeInterface $theme */ public function __construct(ModuleService $module_service, ModuleThemeInterface $theme) { $this->module_service = $module_service; $this->theme = $theme; } /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $bootable_modules = $this->module_service->all()->filter(static function (ModuleInterface $module) { return method_exists($module, 'boot'); }); foreach ($bootable_modules as $module) { // Only bootstrap the current theme. if ($module instanceof ModuleThemeInterface && $module !== $this->theme) { continue; } app()->dispatch($module, 'boot'); } return $handler->handle($request); } }