Skip to content

Commit d6e4d6d

Browse files
[FrameworkBundle] Allow using a ContainerConfigurator in MicroKernelTrait::configureContainer()
1 parent 3409955 commit d6e4d6d

File tree

11 files changed

+58
-170
lines changed

11 files changed

+58
-170
lines changed

UPGRADE-5.1.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
UPGRADE FROM 5.0 to 5.1
22
=======================
33

4-
FrameworkBundle
5-
---------------
6-
7-
* Marked `MicroKernelTrait::configureRoutes()` as `@internal` and `@final`.
8-
* Deprecated not overriding `MicroKernelTrait::configureRouting()`.
9-
104
HttpFoundation
115
--------------
126

UPGRADE-6.0.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
UPGRADE FROM 5.x to 6.0
22
=======================
33

4-
FrameworkBundle
5-
---------------
6-
7-
* Removed `MicroKernelTrait::configureRoutes()`.
8-
* Made `MicroKernelTrait::configureRouting()` abstract.
9-
104
HttpFoundation
115
--------------
126

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ 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`
8+
* Made `MicroKernelTrait::configureRoutes()` compatible with `RoutingConfigurator`
99
* Added a new `mailer.message_bus` option to configure or disable the message bus to use to send mails.
1010
* Added flex-based default implementation for `MicroKernelTrait::registerBundles()`
1111

src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php

Lines changed: 46 additions & 32 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}
@@ -112,13 +97,33 @@ public function registerContainerConfiguration(LoaderInterface $loader)
11297
$kernelDefinition->addTag('kernel.event_subscriber');
11398
}
11499

100+
$container->addObjectResource($this);
115101
$container->fileExists($this->getProjectDir().'/config/bundles.php');
116102
$container->setParameter('container.dumper.inline_class_loader', \PHP_VERSION_ID < 70400 || !ini_get('opcache.preload'));
117103
$container->setParameter('container.dumper.inline_factories', true);
118104

119-
$this->configureContainer($container, $loader);
105+
try {
106+
$this->configureContainer($container, $loader);
120107

121-
$container->addObjectResource($this);
108+
return;
109+
} catch (\TypeError $e) {
110+
$file = $e->getFile();
111+
112+
if (0 !== strpos($e->getMessage(), sprintf('Argument 1 passed to %s::configureContainer() must be an instance of %s,', static::class, ContainerConfigurator::class))) {
113+
throw $e;
114+
}
115+
}
116+
117+
$kernelLoader = $loader->getResolver()->resolve($file);
118+
$kernelLoader->setCurrentDir(\dirname($file));
119+
$instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader, $kernelLoader)();
120+
121+
try {
122+
$this->configureContainer(new ContainerConfigurator($container, $kernelLoader, $instanceof, $file, $file), $loader);
123+
} finally {
124+
$instanceof = [];
125+
$kernelLoader->registerAliasesForSinglyImplementedInterfaces();
126+
}
122127
});
123128
}
124129

@@ -127,17 +132,26 @@ public function registerContainerConfiguration(LoaderInterface $loader)
127132
*/
128133
public function loadRoutes(LoaderInterface $loader)
129134
{
130-
$routes = new RouteCollectionBuilder($loader);
131-
$this->configureRoutes($routes);
132-
$collection = $routes->build();
135+
$file = (new \ReflectionObject($this))->getFileName();
136+
$kernelLoader = $loader->getResolver()->resolve($file);
137+
$kernelLoader->setCurrentDir(\dirname($file));
138+
$collection = new RouteCollection();
133139

134-
if (0 !== \count($collection)) {
135-
@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);
140+
try {
141+
$this->configureRoutes(new RoutingConfigurator($collection, $kernelLoader, $file, $file));
142+
143+
return $collection;
144+
} catch (\TypeError $e) {
145+
if (0 !== strpos($e->getMessage(), sprintf('Argument 1 passed to %s::configureRoutes() must be an instance of %s,', static::class, RouteCollectionBuilder::class))) {
146+
throw $e;
147+
}
136148
}
137149

138-
$file = (new \ReflectionObject($this))->getFileName();
139-
$this->configureRouting(new RoutingConfigurator($collection, $loader, null, $file));
150+
@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);
151+
152+
$routes = new RouteCollectionBuilder($loader);
153+
$this->configureRoutes($routes);
140154

141-
return $collection;
155+
return $routes->build();
142156
}
143157
}

src/Symfony/Bundle/FrameworkBundle/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');

src/Symfony/Bundle/FrameworkBundle/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);

src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/MicroKernelWithConfigureRoutes.php

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

src/Symfony/Bundle/FrameworkBundle/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",

src/Symfony/Component/Routing/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ CHANGELOG
55
-----
66

77
* Deprecated `RouteCollectionBuilder` in favor of `RoutingConfigurator`.
8-
* Added support for a generic loader to `RoutingConfigurator`.
98

109
5.0.0
1110
-----

src/Symfony/Component/Routing/Loader/Configurator/RoutingConfigurator.php

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
namespace Symfony\Component\Routing\Loader\Configurator;
1313

14-
use Symfony\Component\Config\Exception\LoaderLoadException;
15-
use Symfony\Component\Config\Loader\FileLoader;
16-
use Symfony\Component\Config\Loader\LoaderInterface;
14+
use Symfony\Component\Routing\Loader\PhpFileLoader;
1715
use Symfony\Component\Routing\RouteCollection;
1816

1917
/**
@@ -27,7 +25,7 @@ class RoutingConfigurator
2725
private $path;
2826
private $file;
2927

30-
public function __construct(RouteCollection $collection, LoaderInterface $loader, ?string $path, string $file)
28+
public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file)
3129
{
3230
$this->collection = $collection;
3331
$this->loader = $loader;
@@ -40,7 +38,9 @@ public function __construct(RouteCollection $collection, LoaderInterface $loader
4038
*/
4139
final public function import($resource, string $type = null, bool $ignoreErrors = false, $exclude = null): ImportConfigurator
4240
{
43-
$imported = $this->load($resource, $type, $ignoreErrors, $exclude) ?: [];
41+
$this->loader->setCurrentDir(\dirname($this->path));
42+
43+
$imported = $this->loader->import($resource, $type, $ignoreErrors, $this->file, $exclude) ?: [];
4444
if (!\is_array($imported)) {
4545
return new ImportConfigurator($this->collection, $imported);
4646
}
@@ -57,34 +57,4 @@ final public function collection(string $name = ''): CollectionConfigurator
5757
{
5858
return new CollectionConfigurator($this->collection, $name);
5959
}
60-
61-
/**
62-
* @param string|string[]|null $exclude
63-
*
64-
* @return RouteCollection|RouteCollection[]|null
65-
*/
66-
private function load($resource, ?string $type, bool $ignoreErrors, $exclude)
67-
{
68-
$loader = $this->loader;
69-
70-
if (!$loader->supports($resource, $type)) {
71-
if (null === $resolver = $loader->getResolver()) {
72-
throw new LoaderLoadException($resource, $this->file, null, null, $type);
73-
}
74-
75-
if (false === $loader = $resolver->resolve($resource, $type)) {
76-
throw new LoaderLoadException($resource, $this->file, null, null, $type);
77-
}
78-
}
79-
80-
if (!$loader instanceof FileLoader) {
81-
return $loader->load($resource, $type);
82-
}
83-
84-
if (null !== $this->path) {
85-
$this->loader->setCurrentDir(\dirname($this->path));
86-
}
87-
88-
return $this->loader->import($resource, $type, $ignoreErrors, $this->file, $exclude);
89-
}
9060
}

0 commit comments

Comments
 (0)