public function buildLegacyKernelHandlerCLI()
    {
        $legacyRootDir = $this->legacyRootDir;
        $eventDispatcher = $this->eventDispatcher;
        $container = $this->container;
        $that = $this;

        return static function () use ($legacyRootDir, $container, $eventDispatcher, $that) {
            if (!$that->getCLIHandler()) {
                $currentDir = getcwd();
                chdir($legacyRootDir);

                $legacyParameters = new ParameterBag($container->getParameter('ezpublish_legacy.kernel_handler.cli.options'));
                if ($that->getBuildEventsEnabled()) {
                    $eventDispatcher->dispatch(LegacyEvents::PRE_BUILD_LEGACY_KERNEL, new PreBuildKernelEvent($legacyParameters));
                }

                $that->setCLIHandler(
                    new CLIHandler($legacyParameters->all(), $container->get('ezpublish.siteaccess'), $container)
                );

                chdir($currentDir);
            }

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