public function buildLegacyKernelHandlerWeb($webHandlerClass, array $defaultLegacyOptions = [])
    {
        $legacyRootDir = $this->legacyRootDir;
        $webrootDir = $this->webrootDir;
        $uriHelper = $this->uriHelper;
        $eventDispatcher = $this->eventDispatcher;
        $container = $this->container;
        $that = $this;

        return function () use ($legacyRootDir, $webrootDir, $container, $defaultLegacyOptions, $webHandlerClass, $uriHelper, $eventDispatcher, $that) {
            if (!$that->getWebHandler()) {
                chdir($legacyRootDir);

                $legacyParameters = new ParameterBag($defaultLegacyOptions);
                $legacyParameters->set('service-container', $container);
                $request = $this->requestStack->getCurrentRequest();

                if ($that->getBuildEventsEnabled()) {
                    // PRE_BUILD_LEGACY_KERNEL for non request related stuff
                    $eventDispatcher->dispatch(LegacyEvents::PRE_BUILD_LEGACY_KERNEL, new PreBuildKernelEvent($legacyParameters));

                    // Pure web stuff
                    $eventDispatcher->dispatch(
                        LegacyEvents::PRE_BUILD_LEGACY_KERNEL_WEB,
                        new PreBuildKernelWebHandlerEvent($legacyParameters, $request)
                    );
                }

                $interfaces = class_implements($webHandlerClass);
                if (!isset($interfaces['ezpKernelHandler'])) {
                    throw new \InvalidArgumentException('A legacy kernel handler must be an instance of ezpKernelHandler.');
                }

                $that->setWebHandler(new $webHandlerClass($legacyParameters->all()));
                // Fix up legacy URI for global use cases (i.e. using runCallback()).
                $uriHelper->updateLegacyURI($request);
                chdir($webrootDir);
            }

            return $that->getWebHandler();
        };
    }