Auto-register kernel as an extension

This commit is contained in:
HypeMC 2020-09-20 02:23:25 +02:00
parent 772547ec02
commit 9c34980869
3 changed files with 34 additions and 0 deletions

View File

@ -11,6 +11,7 @@ CHANGELOG
* content of request parameter `_password` is now also hidden
in the request profiler raw content section
* Allowed adding attributes on controller arguments that will be passed to argument resolvers.
* kernels implementing the `ExtensionInterface` will now be auto-registered to the container
5.1.0
-----

View File

@ -23,6 +23,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Dumper\Preloader;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
@ -688,6 +689,9 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl
$container = new ContainerBuilder();
$container->getParameterBag()->add($this->getKernelParameters());
if ($this instanceof ExtensionInterface) {
$container->registerExtension($this);
}
if ($this instanceof CompilerPassInterface) {
$container->addCompilerPass($this, PassConfig::TYPE_BEFORE_OPTIMIZATION, -10000);
}

View File

@ -16,6 +16,7 @@ use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -475,6 +476,34 @@ EOF;
$this->assertFileExists(\dirname($containerFile).'.legacy');
}
public function testKernelExtension()
{
$kernel = new class() extends CustomProjectDirKernel implements ExtensionInterface {
public function load(array $configs, ContainerBuilder $container)
{
$container->setParameter('test.extension-registered', true);
}
public function getNamespace()
{
return '';
}
public function getXsdValidationBasePath()
{
return false;
}
public function getAlias()
{
return 'test-extension';
}
};
$kernel->boot();
$this->assertTrue($kernel->getContainer()->getParameter('test.extension-registered'));
}
public function testKernelPass()
{
$kernel = new PassKernel();