diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 357209072b968..5da86e19be2a2 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -428,16 +428,12 @@ protected function initializeContainer() $cache = new ConfigCache($cacheDir.'/'.$class.'.php', $this->debug); $cachePath = $cache->getPath(); - // Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors - $errorLevel = error_reporting(E_ALL ^ E_WARNING); - try { - if (file_exists($cachePath) && \is_object($this->container = include $cachePath) + if (\is_object($this->container = $this->includeSafe($cachePath)) && (!$this->debug || (self::$freshCache[$cachePath] ?? $cache->isFresh())) ) { self::$freshCache[$cachePath] = true; $this->container->set('kernel', $this); - error_reporting($errorLevel); return; } @@ -455,7 +451,7 @@ protected function initializeContainer() if (!flock($lock, $wouldBlock ? LOCK_SH : LOCK_EX)) { fclose($lock); $lock = null; - } elseif (!\is_object($this->container = include $cachePath)) { + } elseif (!\is_object($this->container = $this->includeSafe($cachePath))) { $this->container = null; } elseif (!$oldContainer || \get_class($this->container) !== $oldContainer->name) { flock($lock, LOCK_UN); @@ -466,8 +462,6 @@ protected function initializeContainer() } } } catch (\Throwable $e) { - } finally { - error_reporting($errorLevel); } if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) { @@ -789,6 +783,23 @@ public static function stripComments(string $source) return $output; } + /** + * Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors. + * + * @return mixed + */ + private function includeSafe(string $path) + { + $errorLevel = error_reporting(E_ALL ^ E_WARNING); + try { + $result = include $path; + } finally { + error_reporting($errorLevel); + } + + return $result; + } + /** * @return array */