Skip to content

Commit d10e6fb

Browse files
Merge branch '5.4' into 6.0
* 5.4: [GHA] fix running "Patch return types" step [Serializer] Don't pass null to preg_match() [String] Update wcswidth data with Unicode 14 [Mailer][Sendgrid] Fix test Use identity operator to prevent type juggling [Form] Don't trim unassigned unicode characters anymore Allow injecting tagged iterator as service locator arguments [FrameworkBundle] Add configureContainer(), configureRoutes() and getConfigDir() to MicroKernelTrait
2 parents fbf02f4 + 21b79fc commit d10e6fb

File tree

3 files changed

+65
-52
lines changed

3 files changed

+65
-52
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ CHANGELOG
2828
* Deprecate the `AdapterInterface` autowiring alias, use `CacheItemPoolInterface` instead
2929
* Deprecate the public `profiler` service to private
3030
* Deprecate `get()`, `has()`, `getDoctrine()`, and `dispatchMessage()` in `AbstractController`, use method/constructor injection instead
31-
* Add `MicroKernelTrait::getBundlesPath` method to get bundles config path
3231
* Deprecate the `cache.adapter.doctrine` service
33-
* Add support for resetting container services after each messenger message.
32+
* Add support for resetting container services after each messenger message
33+
* Add `configureContainer()`, `configureRoutes()`, `getConfigDir()` and `getBundlesPath()` to `MicroKernelTrait`
3434

3535
5.3
3636
---

Kernel/MicroKernelTrait.php

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,79 @@
2626
*
2727
* @author Ryan Weaver <[email protected]>
2828
* @author Fabien Potencier <[email protected]>
29-
*
30-
* @method void configureContainer(ContainerConfigurator $container)
3129
*/
3230
trait MicroKernelTrait
3331
{
34-
/**
35-
* Adds or imports routes into your application.
36-
*
37-
* $routes->import($this->getProjectDir().'/config/*.{yaml,php}');
38-
* $routes
39-
* ->add('admin_dashboard', '/admin')
40-
* ->controller('App\Controller\AdminController::dashboard')
41-
* ;
42-
*/
43-
abstract protected function configureRoutes(RoutingConfigurator $routes): void;
44-
4532
/**
4633
* Configures the container.
4734
*
4835
* You can register extensions:
4936
*
50-
* $c->extension('framework', [
37+
* $container->extension('framework', [
5138
* 'secret' => '%secret%'
5239
* ]);
5340
*
5441
* Or services:
5542
*
56-
* $c->services()->set('halloween', 'FooBundle\HalloweenProvider');
43+
* $container->services()->set('halloween', 'FooBundle\HalloweenProvider');
5744
*
5845
* Or parameters:
5946
*
60-
* $c->parameters()->set('halloween', 'lot of fun');
47+
* $container->parameters()->set('halloween', 'lot of fun');
6148
*/
62-
//abstract protected function configureContainer(ContainerConfigurator $container): void;
49+
private function configureContainer(ContainerConfigurator $container, LoaderInterface $loader, ContainerBuilder $builder): void
50+
{
51+
$configDir = $this->getConfigDir();
52+
53+
$container->import($configDir.'/{packages}/*.yaml');
54+
$container->import($configDir.'/{packages}/'.$this->environment.'/*.yaml');
55+
56+
if (is_file($configDir.'/services.yaml')) {
57+
$container->import($configDir.'/services.yaml');
58+
$container->import($configDir.'/{services}_'.$this->environment.'.yaml');
59+
} else {
60+
$container->import($configDir.'/{services}.php');
61+
}
62+
}
63+
64+
/**
65+
* Adds or imports routes into your application.
66+
*
67+
* $routes->import($this->getConfigDir().'/*.{yaml,php}');
68+
* $routes
69+
* ->add('admin_dashboard', '/admin')
70+
* ->controller('App\Controller\AdminController::dashboard')
71+
* ;
72+
*/
73+
private function configureRoutes(RoutingConfigurator $routes): void
74+
{
75+
$configDir = $this->getConfigDir();
76+
77+
$routes->import($configDir.'/{routes}/'.$this->environment.'/*.yaml');
78+
$routes->import($configDir.'/{routes}/*.yaml');
79+
80+
if (is_file($configDir.'/routes.yaml')) {
81+
$routes->import($configDir.'/routes.yaml');
82+
} else {
83+
$routes->import($configDir.'/{routes}.php');
84+
}
85+
}
86+
87+
/**
88+
* Gets the path to the configuration directory.
89+
*/
90+
private function getConfigDir(): string
91+
{
92+
return $this->getProjectDir().'/config';
93+
}
94+
95+
/**
96+
* Gets the path to the bundles configuration file.
97+
*/
98+
private function getBundlesPath(): string
99+
{
100+
return $this->getConfigDir().'/bundles.php';
101+
}
63102

64103
/**
65104
* {@inheritdoc}
@@ -124,23 +163,18 @@ public function registerContainerConfiguration(LoaderInterface $loader)
124163
$container->addObjectResource($this);
125164
$container->fileExists($this->getBundlesPath());
126165

127-
try {
128-
$configureContainer = new \ReflectionMethod($this, 'configureContainer');
129-
} catch (\ReflectionException $e) {
130-
throw new \LogicException(sprintf('"%s" uses "%s", but does not implement the required method "protected function configureContainer(ContainerConfigurator $container): void".', get_debug_type($this), MicroKernelTrait::class), 0, $e);
131-
}
132-
166+
$configureContainer = new \ReflectionMethod($this, 'configureContainer');
133167
$configuratorClass = $configureContainer->getNumberOfParameters() > 0 && ($type = $configureContainer->getParameters()[0]->getType()) instanceof \ReflectionNamedType && !$type->isBuiltin() ? $type->getName() : null;
134168

135169
if ($configuratorClass && !is_a(ContainerConfigurator::class, $configuratorClass, true)) {
136-
$this->configureContainer($container, $loader);
170+
$configureContainer->getClosure($this)($container, $loader);
137171

138172
return;
139173
}
140174

141-
// the user has opted into using the ContainerConfigurator
175+
$file = (new \ReflectionObject($this))->getFileName();
142176
/* @var ContainerPhpFileLoader $kernelLoader */
143-
$kernelLoader = $loader->getResolver()->resolve($file = $configureContainer->getFileName());
177+
$kernelLoader = $loader->getResolver()->resolve($file);
144178
$kernelLoader->setCurrentDir(\dirname($file));
145179
$instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader, $kernelLoader)();
146180

@@ -150,7 +184,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
150184
};
151185

152186
try {
153-
$this->configureContainer(new ContainerConfigurator($container, $kernelLoader, $instanceof, $file, $file, $this->getEnvironment()), $loader);
187+
$configureContainer->getClosure($this)(new ContainerConfigurator($container, $kernelLoader, $instanceof, $file, $file, $this->getEnvironment()), $loader, $container);
154188
} finally {
155189
$instanceof = [];
156190
$kernelLoader->registerAliasesForSinglyImplementedInterfaces();
@@ -172,7 +206,8 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
172206
$kernelLoader->setCurrentDir(\dirname($file));
173207
$collection = new RouteCollection();
174208

175-
$this->configureRoutes(new RoutingConfigurator($collection, $kernelLoader, $file, $file, $this->getEnvironment()));
209+
$configureRoutes = new \ReflectionMethod($this, 'configureRoutes');
210+
$configureRoutes->getClosure($this)(new RoutingConfigurator($collection, $kernelLoader, $file, $file, $this->getEnvironment()));
176211

177212
foreach ($collection as $route) {
178213
$controller = $route->getDefault('_controller');
@@ -184,12 +219,4 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
184219

185220
return $collection;
186221
}
187-
188-
/**
189-
* Gets the path to the bundles configuration file.
190-
*/
191-
private function getBundlesPath(): string
192-
{
193-
return $this->getProjectDir().'/config/bundles.php';
194-
}
195222
}

Tests/Kernel/MicroKernelTraitTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,6 @@ protected function configureRoutes(RoutingConfigurator $routes): void
126126

127127
$this->assertSame('Hello World!', $response->getContent());
128128
}
129-
130-
public function testMissingConfigureContainer()
131-
{
132-
$kernel = new class('missing_configure_container') extends MinimalKernel {
133-
protected function configureRoutes(RoutingConfigurator $routes): void
134-
{
135-
}
136-
};
137-
138-
$this->expectException(\LogicException::class);
139-
$this->expectExceptionMessage('"Symfony\Bundle\FrameworkBundle\Tests\Kernel\MinimalKernel@anonymous" uses "Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait", but does not implement the required method "protected function configureContainer(ContainerConfigurator $container): void".');
140-
141-
$kernel->boot();
142-
}
143129
}
144130

145131
abstract class MinimalKernel extends Kernel

0 commit comments

Comments
 (0)