Description
Symfony version(s) affected: 4.2.2
Description
This is a followup to #29322. The fix #29328 by @nicolas-grekas (thanks again) works most of the time, but it can still happen that invalid php code is generated.
How to reproduce
Initialize a new project with http-kernel
, debug
and framework-bundle
. Run the following script multiple times:
<?php
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Debug\Debug;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
require __DIR__.'/vendor/autoload.php';
Debug::enable();
$kernel = new class('prod', false) extends Kernel {
public function registerBundles()
{
yield new FrameworkBundle();
}
/**
* Loads the container configuration.
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function (ContainerBuilder $container) use ($loader) {
$container->loadFromExtension('framework', [
'secret' => 'foo',
]);
});
}
};
$kernel->boot();
Most of the times, this script will execute successfully. However, you will eventually run into an error like this:
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Undefined constant 'ContainerFXaIYUw\containertestSymfony_Component_HttpKernel_Kernelp' in <dir>/var/cache/prod/containertestSymfony_Component_HttpKernel_Kernelp.UcddJProdContainer.php:5
Stack trace:
#0 <dir>/vendor/symfony/http-kernel/Kernel.php(567): require()
#1 <dir>/vendor/symfony/http-kernel/Kernel.php(133): Symfony\Component\HttpKernel\Kernel->initializeContainer()
#2 <dir>/test.php(32): Symfony\Component\HttpKernel\Kernel->boot()
#3 {main}
thrown in <dir>/var/cache/prod/containertestSymfony_Component_HttpKernel_Kernelp.UcddJProdContainer.php on line 5
Possible Solution
The problem is, that the method ContainerBuilder::hash()
is used to calculate a part of the generated class name. That method might produce dot characters (.
) which are not allowed in class names. We could simply replace dots with underscores (_
). This will weaken the hash function a bit (as it might already produce underscores itself), but that shouldn't be much of an issue here.