Skip to content

[FrameworkBundle] Simpler Kernel setup with MicroKernelTrait #57408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CHANGELOG

* Add support for setting `headers` with `Symfony\Bundle\FrameworkBundle\Controller\TemplateController`
* Derivate `kernel.secret` from the decryption secret when its env var is not defined
* Make the `config/` directory optional in `MicroKernelTrait`, add support for service arguments in the
invokable Kernel class, and register `FrameworkBundle` by default when the `bundles.php` file is missing

7.1
---
Expand Down
17 changes: 13 additions & 4 deletions src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Kernel;

use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\AbstractConfigurator;
Expand Down Expand Up @@ -48,7 +49,7 @@ trait MicroKernelTrait
*/
private function configureContainer(ContainerConfigurator $container, LoaderInterface $loader, ContainerBuilder $builder): void
{
$configDir = $this->getConfigDir();
$configDir = preg_replace('{/config$}', '/{config}', $this->getConfigDir());

$container->import($configDir.'/{packages}/*.{php,yaml}');
$container->import($configDir.'/{packages}/'.$this->environment.'/*.{php,yaml}');
Expand All @@ -73,7 +74,7 @@ private function configureContainer(ContainerConfigurator $container, LoaderInte
*/
private function configureRoutes(RoutingConfigurator $routes): void
{
$configDir = $this->getConfigDir();
$configDir = preg_replace('{/config$}', '/{config}', $this->getConfigDir());

$routes->import($configDir.'/{routes}/'.$this->environment.'/*.{php,yaml}');
$routes->import($configDir.'/{routes}/*.{php,yaml}');
Expand All @@ -84,7 +85,7 @@ private function configureRoutes(RoutingConfigurator $routes): void
$routes->import($configDir.'/{routes}.php');
}

if (false !== ($fileName = (new \ReflectionObject($this))->getFileName())) {
if ($fileName = (new \ReflectionObject($this))->getFileName()) {
$routes->import($fileName, 'attribute');
}
}
Expand Down Expand Up @@ -130,7 +131,13 @@ public function getLogDir(): string

public function registerBundles(): iterable
{
$contents = require $this->getBundlesPath();
if (!is_file($bundlesPath = $this->getBundlesPath())) {
yield new FrameworkBundle();

return;
}

$contents = require $bundlesPath;
foreach ($contents as $class => $envs) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
yield new $class();
Expand Down Expand Up @@ -216,6 +223,8 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
$route->setDefault('_controller', ['kernel', $controller[1]]);
} elseif ($controller instanceof \Closure && $this === ($r = new \ReflectionFunction($controller))->getClosureThis() && !$r->isAnonymous()) {
$route->setDefault('_controller', ['kernel', $r->name]);
} elseif ($this::class === $controller && method_exists($this, '__invoke')) {
$route->setDefault('_controller', 'kernel');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Kernel;

use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -46,13 +45,6 @@ public function dangerousAction()
throw new Danger();
}

public function registerBundles(): iterable
{
return [
new FrameworkBundle(),
];
}

public function getCacheDir(): string
{
return $this->cacheDir = sys_get_temp_dir().'/sf_micro_kernel';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
Expand Down Expand Up @@ -140,6 +139,17 @@ protected function configureRoutes(RoutingConfigurator $routes): void

$this->assertSame('Hello World!', $response->getContent());
}

public function testSimpleKernel()
{
$kernel = $this->kernel = new SimpleKernel('simple_kernel');
$kernel->boot();

$request = Request::create('/');
$response = $kernel->handle($request, HttpKernelInterface::MAIN_REQUEST, false);

$this->assertSame('Hello World!', $response->getContent());
}
}

abstract class MinimalKernel extends Kernel
Expand All @@ -155,11 +165,6 @@ public function __construct(string $cacheDir)
$this->cacheDir = sys_get_temp_dir().'/'.$cacheDir;
}

public function registerBundles(): iterable
{
yield new FrameworkBundle();
}

public function getCacheDir(): string
{
return $this->cacheDir;
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/SimpleKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Kernel;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class SimpleKernel extends MinimalKernel
{
#[Route('/')]
public function __invoke(UrlGeneratorInterface $urlGenerator): Response
{
return new Response('Hello World!');
}
}
Loading