Skip to content

Commit 93605cc

Browse files
[FrameworkBundle] Allow using a ContainerConfigurator in MicroKernelTrait::configureContainer()
1 parent 4235a36 commit 93605cc

File tree

6 files changed

+51
-121
lines changed

6 files changed

+51
-121
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ CHANGELOG
44
5.1.0
55
-----
66

7-
* Marked `MicroKernelTrait::configureRoutes()` as `@internal` and `@final`.
8-
* Deprecated not overriding `MicroKernelTrait::configureRouting()`.
7+
* Made `MicroKernelTrait::configureContainer()` compatible with `ContainerConfigurator`
98
* Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails.
109
* Added flex-compatible default implementations for `MicroKernelTrait::registerBundles()` and `getProjectDir()`
10+
* Deprecated passing a `RouteCollectionBuiler` to `MicroKernelTrait::configureRoutes()`, type-hint `RoutingConfigurator` instead
1111

1212
5.0.0
1313
-----

Kernel/MicroKernelTrait.php

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
use Symfony\Component\Config\Loader\LoaderInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1617
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1718
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
19+
use Symfony\Component\Routing\RouteCollection;
1820
use Symfony\Component\Routing\RouteCollectionBuilder;
1921

2022
/**
@@ -25,20 +27,6 @@
2527
*/
2628
trait MicroKernelTrait
2729
{
28-
/**
29-
* Add or import routes into your application.
30-
*
31-
* $routes->import('config/routing.yml');
32-
* $routes->add('/admin', 'App\Controller\AdminController::dashboard', 'admin_dashboard');
33-
*
34-
* @final since Symfony 5.1, override configureRouting() instead
35-
*
36-
* @internal since Symfony 5.1, use configureRouting() instead
37-
*/
38-
protected function configureRoutes(RouteCollectionBuilder $routes)
39-
{
40-
}
41-
4230
/**
4331
* Adds or imports routes into your application.
4432
*
@@ -48,29 +36,26 @@ protected function configureRoutes(RouteCollectionBuilder $routes)
4836
* ->controller('App\Controller\AdminController::dashboard')
4937
* ;
5038
*/
51-
protected function configureRouting(RoutingConfigurator $routes): void
52-
{
53-
@trigger_error(sprintf('Not overriding the "%s()" method is deprecated since Symfony 5.1 and will trigger a fatal error in 6.0.', __METHOD__), E_USER_DEPRECATED);
54-
}
39+
abstract protected function configureRoutes(RoutingConfigurator $routes);
5540

5641
/**
5742
* Configures the container.
5843
*
5944
* You can register extensions:
6045
*
61-
* $c->loadFromExtension('framework', [
46+
* $c->extension('framework', [
6247
* 'secret' => '%secret%'
6348
* ]);
6449
*
6550
* Or services:
6651
*
67-
* $c->register('halloween', 'FooBundle\HalloweenProvider');
52+
* $c->services()->set('halloween', 'FooBundle\HalloweenProvider');
6853
*
6954
* Or parameters:
7055
*
71-
* $c->setParameter('halloween', 'lot of fun');
56+
* $c->parameters()->set('halloween', 'lot of fun');
7257
*/
73-
abstract protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader);
58+
abstract protected function configureContainer(ContainerConfigurator $c);
7459

7560
/**
7661
* {@inheritdoc}
@@ -120,9 +105,31 @@ public function registerContainerConfiguration(LoaderInterface $loader)
120105
$kernelDefinition->addTag('kernel.event_subscriber');
121106
}
122107

123-
$this->configureContainer($container, $loader);
124108
$container->addObjectResource($this);
125109
$container->fileExists($this->getProjectDir().'/config/bundles.php');
110+
111+
try {
112+
$this->configureContainer($container, $loader);
113+
114+
return;
115+
} catch (\TypeError $e) {
116+
$file = $e->getFile();
117+
118+
if (0 !== strpos($e->getMessage(), sprintf('Argument 1 passed to %s::configureContainer() must be an instance of %s,', static::class, ContainerConfigurator::class))) {
119+
throw $e;
120+
}
121+
}
122+
123+
$kernelLoader = $loader->getResolver()->resolve($file);
124+
$kernelLoader->setCurrentDir(\dirname($file));
125+
$instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader, $kernelLoader)();
126+
127+
try {
128+
$this->configureContainer(new ContainerConfigurator($container, $kernelLoader, $instanceof, $file, $file), $loader);
129+
} finally {
130+
$instanceof = [];
131+
$kernelLoader->registerAliasesForSinglyImplementedInterfaces();
132+
}
126133
});
127134
}
128135

@@ -131,17 +138,26 @@ public function registerContainerConfiguration(LoaderInterface $loader)
131138
*/
132139
public function loadRoutes(LoaderInterface $loader)
133140
{
134-
$routes = new RouteCollectionBuilder($loader);
135-
$this->configureRoutes($routes);
136-
$collection = $routes->build();
141+
$file = (new \ReflectionObject($this))->getFileName();
142+
$kernelLoader = $loader->getResolver()->resolve($file);
143+
$kernelLoader->setCurrentDir(\dirname($file));
144+
$collection = new RouteCollection();
137145

138-
if (0 !== \count($collection)) {
139-
@trigger_error(sprintf('Adding routes via the "%s:configureRoutes()" method is deprecated since Symfony 5.1 and will have no effect in 6.0; use "configureRouting()" instead.', self::class), E_USER_DEPRECATED);
146+
try {
147+
$this->configureRoutes(new RoutingConfigurator($collection, $kernelLoader, $file, $file));
148+
149+
return $collection;
150+
} catch (\TypeError $e) {
151+
if (0 !== strpos($e->getMessage(), sprintf('Argument 1 passed to %s::configureRoutes() must be an instance of %s,', static::class, RouteCollectionBuilder::class))) {
152+
throw $e;
153+
}
140154
}
141155

142-
$file = (new \ReflectionObject($this))->getFileName();
143-
$this->configureRouting(new RoutingConfigurator($collection, $loader, null, $file));
156+
@trigger_error(sprintf('Using type "%s" for argument 1 of method "%s:configureRoutes()" is deprecated since Symfony 5.1, use "%s" instead.', RouteCollectionBuilder::class, self::class, RoutingConfigurator::class), E_USER_DEPRECATED);
157+
158+
$routes = new RouteCollectionBuilder($loader);
159+
$this->configureRoutes($routes);
144160

145-
return $collection;
161+
return $routes->build();
146162
}
147163
}

Tests/Kernel/ConcreteMicroKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __destruct()
8080
$fs->remove($this->cacheDir);
8181
}
8282

83-
protected function configureRouting(RoutingConfigurator $routes): void
83+
protected function configureRoutes(RoutingConfigurator $routes): void
8484
{
8585
$routes->add('halloween', '/')->controller('kernel::halloweenAction');
8686
$routes->add('danger', '/danger')->controller('kernel::dangerousAction');

Tests/Kernel/MicroKernelTraitTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,6 @@
1919

2020
class MicroKernelTraitTest extends TestCase
2121
{
22-
/**
23-
* @group legacy
24-
* @expectedDeprecation Adding routes via the "Symfony\Bundle\FrameworkBundle\Tests\Kernel\MicroKernelWithConfigureRoutes:configureRoutes()" method is deprecated since Symfony 5.1 and will have no effect in 6.0; use "configureRouting()" instead.
25-
* @expectedDeprecation Not overriding the "Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait::configureRouting()" method is deprecated since Symfony 5.1 and will trigger a fatal error in 6.0.
26-
*/
27-
public function testConfigureRoutingDeprecated()
28-
{
29-
$kernel = new MicroKernelWithConfigureRoutes('test', false);
30-
$kernel->boot();
31-
$kernel->handle(Request::create('/'));
32-
}
33-
3422
public function test()
3523
{
3624
$kernel = new ConcreteMicroKernel('test', false);

Tests/Kernel/MicroKernelWithConfigureRoutes.php

Lines changed: 0 additions & 74 deletions
This file was deleted.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"symfony/polyfill-mbstring": "~1.0",
2828
"symfony/filesystem": "^4.4|^5.0",
2929
"symfony/finder": "^4.4|^5.0",
30-
"symfony/routing": "^5.1"
30+
"symfony/routing": "^5.0"
3131
},
3232
"require-dev": {
3333
"doctrine/annotations": "~1.7",

0 commit comments

Comments
 (0)