private function getSymfonyContainer()
    {
        // 1. Preferred way in contao 4.0+
        if (method_exists('Contao\System', 'getContainer')
            && ($container = System::getContainer()) instanceof ContainerInterface
        ) {
            return $container;
        }

        // 2. Fallback to fetch from kernel.
        if (isset($GLOBALS['kernel'])
            && $GLOBALS['kernel'] instanceof KernelInterface
            && ($container = $GLOBALS['kernel']->getContainer()) instanceof ContainerInterface
        ) {
            return $container;
        }

        // 3. Nothing worked out, throw Exception as this may never happen.
        throw new \RuntimeException('Could not obtain symfony container.');
    }