Skip to content

Commit 89298b9

Browse files
[FrameworkBundle] Add configureContainer(), configureRoutes() and getConfigDir() to MicroKernelTrait
1 parent baaf7ea commit 89298b9

File tree

3 files changed

+65
-77
lines changed

3 files changed

+65
-77
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ CHANGELOG
88
* Deprecate the `AdapterInterface` autowiring alias, use `CacheItemPoolInterface` instead
99
* Deprecate the public `profiler` service to private
1010
* Deprecate `get()`, `has()`, `getDoctrine()`, and `dispatchMessage()` in `AbstractController`, use method/constructor injection instead
11-
* Add `MicroKernelTrait::getBundlesPath` method to get bundles config path
1211
* Deprecate the `cache.adapter.doctrine` service
13-
* Add support for resetting container services after each messenger message.
12+
* Add support for resetting container services after each messenger message
13+
* Add `configureContainer()`, `configureRoutes()`, `getConfigDir()` and `getBundlesPath()` to `MicroKernelTrait`
1414

1515
5.3
1616
---

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

+63-43
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,79 @@
2727
*
2828
* @author Ryan Weaver <ryan@knpuniversity.com>
2929
* @author Fabien Potencier <fabien@symfony.com>
30-
*
31-
* @method void configureRoutes(RoutingConfigurator $routes)
32-
* @method void configureContainer(ContainerConfigurator $container)
3330
*/
3431
trait MicroKernelTrait
3532
{
36-
/**
37-
* Adds or imports routes into your application.
38-
*
39-
* $routes->import($this->getProjectDir().'/config/*.{yaml,php}');
40-
* $routes
41-
* ->add('admin_dashboard', '/admin')
42-
* ->controller('App\Controller\AdminController::dashboard')
43-
* ;
44-
*/
45-
//abstract protected function configureRoutes(RoutingConfigurator $routes): void;
46-
4733
/**
4834
* Configures the container.
4935
*
5036
* You can register extensions:
5137
*
52-
* $c->extension('framework', [
38+
* $ccontainer->extension('framework', [
5339
* 'secret' => '%secret%'
5440
* ]);
5541
*
5642
* Or services:
5743
*
58-
* $c->services()->set('halloween', 'FooBundle\HalloweenProvider');
44+
* $container->services()->set('halloween', 'FooBundle\HalloweenProvider');
5945
*
6046
* Or parameters:
6147
*
62-
* $c->parameters()->set('halloween', 'lot of fun');
48+
* $container->parameters()->set('halloween', 'lot of fun');
6349
*/
64-
//abstract protected function configureContainer(ContainerConfigurator $container): void;
50+
private function configureContainer(ContainerConfigurator $container, LoaderInterface $loader, ContainerBuilder $builder): void
51+
{
52+
$configDir = $this->getConfigDir();
53+
54+
$container->import($configDir.'/{packages}/*.yaml');
55+
$container->import($configDir.'/{packages}/'.$this->environment.'/*.yaml');
56+
57+
if (is_file($configDir.'/services.yaml')) {
58+
$container->import($configDir.'/services.yaml');
59+
$container->import($configDir.'/{services}_'.$this->environment.'.yaml');
60+
} else {
61+
$container->import($configDir.'/{services}.php');
62+
}
63+
}
64+
65+
/**
66+
* Adds or imports routes into your application.
67+
*
68+
* $routes->import($this->getConfigDir().'/*.{yaml,php}');
69+
* $routes
70+
* ->add('admin_dashboard', '/admin')
71+
* ->controller('App\Controller\AdminController::dashboard')
72+
* ;
73+
*/
74+
private function configureRoutes(RoutingConfigurator $routes): void
75+
{
76+
$configDir = $this->getConfigDir();
77+
78+
$routes->import($configDir.'/{routes}/'.$this->environment.'/*.yaml');
79+
$routes->import($configDir.'/{routes}/*.yaml');
80+
81+
if (is_file($configDir.'/routes.yaml')) {
82+
$routes->import($configDir.'/routes.yaml');
83+
} else {
84+
$routes->import($configDir.'/{routes}.php');
85+
}
86+
}
87+
88+
/**
89+
* Gets the path to the configuration directory.
90+
*/
91+
private function getConfigDir(): string
92+
{
93+
return $this->getProjectDir().'/config';
94+
}
95+
96+
/**
97+
* Gets the path to the bundles configuration file.
98+
*/
99+
private function getBundlesPath(): string
100+
{
101+
return $this->getConfigDir().'/bundles.php';
102+
}
65103

66104
/**
67105
* {@inheritdoc}
@@ -126,23 +164,18 @@ public function registerContainerConfiguration(LoaderInterface $loader)
126164
$container->addObjectResource($this);
127165
$container->fileExists($this->getBundlesPath());
128166

129-
try {
130-
$configureContainer = new \ReflectionMethod($this, 'configureContainer');
131-
} catch (\ReflectionException $e) {
132-
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);
133-
}
134-
167+
$configureContainer = new \ReflectionMethod($this, 'configureContainer');
135168
$configuratorClass = $configureContainer->getNumberOfParameters() > 0 && ($type = $configureContainer->getParameters()[0]->getType()) instanceof \ReflectionNamedType && !$type->isBuiltin() ? $type->getName() : null;
136169

137170
if ($configuratorClass && !is_a(ContainerConfigurator::class, $configuratorClass, true)) {
138-
$this->configureContainer($container, $loader);
171+
$configureContainer->getClosure($this)($container, $loader);
139172

140173
return;
141174
}
142175

143-
// the user has opted into using the ContainerConfigurator
176+
$file = (new \ReflectionObject($this))->getFileName();
144177
/* @var ContainerPhpFileLoader $kernelLoader */
145-
$kernelLoader = $loader->getResolver()->resolve($file = $configureContainer->getFileName());
178+
$kernelLoader = $loader->getResolver()->resolve($file);
146179
$kernelLoader->setCurrentDir(\dirname($file));
147180
$instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader, $kernelLoader)();
148181

@@ -152,7 +185,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
152185
};
153186

154187
try {
155-
$this->configureContainer(new ContainerConfigurator($container, $kernelLoader, $instanceof, $file, $file, $this->getEnvironment()), $loader);
188+
$configureContainer->getClosure($this)(new ContainerConfigurator($container, $kernelLoader, $instanceof, $file, $file, $this->getEnvironment()), $loader, $container);
156189
} finally {
157190
$instanceof = [];
158191
$kernelLoader->registerAliasesForSinglyImplementedInterfaces();
@@ -174,12 +207,7 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
174207
$kernelLoader->setCurrentDir(\dirname($file));
175208
$collection = new RouteCollection();
176209

177-
try {
178-
$configureRoutes = new \ReflectionMethod($this, 'configureRoutes');
179-
} catch (\ReflectionException $e) {
180-
throw new \LogicException(sprintf('"%s" uses "%s", but does not implement the required method "protected function configureRoutes(RoutingConfigurator $routes): void".', get_debug_type($this), MicroKernelTrait::class), 0, $e);
181-
}
182-
210+
$configureRoutes = new \ReflectionMethod($this, 'configureRoutes');
183211
$configuratorClass = $configureRoutes->getNumberOfParameters() > 0 && ($type = $configureRoutes->getParameters()[0]->getType()) instanceof \ReflectionNamedType && !$type->isBuiltin() ? $type->getName() : null;
184212

185213
if ($configuratorClass && !is_a(RoutingConfigurator::class, $configuratorClass, true)) {
@@ -191,7 +219,7 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
191219
return $routes->build();
192220
}
193221

194-
$this->configureRoutes(new RoutingConfigurator($collection, $kernelLoader, $file, $file, $this->getEnvironment()));
222+
$configureRoutes->getClosure($this)(new RoutingConfigurator($collection, $kernelLoader, $file, $file, $this->getEnvironment()));
195223

196224
foreach ($collection as $route) {
197225
$controller = $route->getDefault('_controller');
@@ -203,12 +231,4 @@ public function loadRoutes(LoaderInterface $loader): RouteCollection
203231

204232
return $collection;
205233
}
206-
207-
/**
208-
* Gets the path to the bundles configuration file.
209-
*/
210-
private function getBundlesPath(): string
211-
{
212-
return $this->getProjectDir().'/config/bundles.php';
213-
}
214234
}

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

-32
Original file line numberDiff line numberDiff line change
@@ -126,38 +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-
}
143-
144-
public function testMissingConfigureRoutes()
145-
{
146-
$kernel = new class('missing_configure_routes') extends MinimalKernel {
147-
protected function configureContainer(ContainerConfigurator $c): void
148-
{
149-
$c->extension('framework', [
150-
'router' => ['utf8' => true],
151-
]);
152-
}
153-
};
154-
155-
$this->expectException(\LogicException::class);
156-
$this->expectExceptionMessage('"Symfony\Bundle\FrameworkBundle\Tests\Kernel\MinimalKernel@anonymous" uses "Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait", but does not implement the required method "protected function configureRoutes(RoutingConfigurator $routes): void".');
157-
158-
$request = Request::create('/');
159-
$kernel->handle($request, HttpKernelInterface::MAIN_REQUEST, false);
160-
}
161129
}
162130

163131
abstract class MinimalKernel extends Kernel

0 commit comments

Comments
 (0)