Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8c80c5b

Browse files
feature #34873 [FrameworkBundle] Allow using a ContainerConfigurator in MicroKernelTrait::configureContainer() (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [FrameworkBundle] Allow using a ContainerConfigurator in MicroKernelTrait::configureContainer() | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This PR is a continuation of #32937 (it reverts some parts of it that are not needed anymore). It builds on #34872 for now. This PR allows using the PHP-DSL natively in our `Kernel::configureContainer()` methods. It allows the same in our `Kernel::configureRoutes()` methods. Both signatures are handled gracefully with no deprecations to let existing code in peace: - `protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader);` - `protected function configureContainer(ContainerConfigurator $c);` (a loader is always passed as 2nd arg to ease FC) Same for routes: - `protected function configureRoutes(RoutingConfigurator $routes);` - `protected function configureRoutes(RouteCollectionBuilder $routes);` (this one is deprecated because `RouteCollectionBuilder` is deprecated) Here is my working `src/Kernel.php` after this change: ```php class Kernel extends BaseKernel { use MicroKernelTrait; protected function configureContainer(ContainerConfigurator $container): void { $container->import('../config/{packages}/*.yaml'); $container->import('../config/{packages}/'.$this->environment.'/*.yaml'); $container->import('../config/services.yaml'); $container->import('../config/{services}_'.$this->environment.'.yaml'); } protected function configureRoutes(RoutingConfigurator $routes): void { $routes->import('../config/{routes}/'.$this->environment.'/*.yaml'); $routes->import('../config/{routes}/*.yaml'); $routes->import('../config/routes.yaml'); } } ``` Commits ------- cf45eec [FrameworkBundle] Allow using a ContainerConfigurator in MicroKernelTrait::configureContainer()
2 parents 4445812 + cf45eec commit 8c80c5b

File tree

11 files changed

+61
-161
lines changed

11 files changed

+61
-161
lines changed

UPGRADE-5.1.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ UPGRADE FROM 5.0 to 5.1
44
FrameworkBundle
55
---------------
66

7-
* Marked `MicroKernelTrait::configureRoutes()` as `@internal` and `@final`.
8-
* Deprecated not overriding `MicroKernelTrait::configureRouting()`.
7+
* Deprecated passing a `RouteCollectionBuiler` to `MicroKernelTrait::configureRoutes()`, type-hint `RoutingConfigurator` instead
98

109
HttpFoundation
1110
--------------

UPGRADE-6.0.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ UPGRADE FROM 5.x to 6.0
44
FrameworkBundle
55
---------------
66

7-
* Removed `MicroKernelTrait::configureRoutes()`.
8-
* Made `MicroKernelTrait::configureRouting()` abstract.
7+
* `MicroKernelTrait::configureRoutes()` is now always called with a `RoutingConfigurator`
98

109
HttpFoundation
1110
--------------

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

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

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
}

src/Symfony/Component/Routing/RouteCollectionBuilder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Symfony\Component\Config\Exception\LoaderLoadException;
1515
use Symfony\Component\Config\Loader\LoaderInterface;
1616
use Symfony\Component\Config\Resource\ResourceInterface;
17+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
18+
19+
@trigger_error(sprintf('The "%s" class is deprecated since Symfony 5.1, use "%s" instead.', RouteCollectionBuilder::class, RoutingConfigurator::class), E_USER_DEPRECATED);
1720

1821
/**
1922
* Helps add and import routes into a RouteCollection.

0 commit comments

Comments
 (0)