Skip to content

Commit 9b93d3b

Browse files
authored
Fix routing in kernel (#37)
* Fix routing in kernel * Fix CS * Add missing ; * Fix CS
1 parent bdfa9c7 commit 9b93d3b

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/AppKernel.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class AppKernel extends Kernel
3939
*/
4040
private $compilerPasses = [];
4141

42+
/**
43+
* @var string|null
44+
*/
45+
private $routingFile = null;
46+
4247
/**
4348
* @param string $cachePrefix
4449
*/
@@ -121,7 +126,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
121126
$loader->load(function (ContainerBuilder $container) use ($loader) {
122127
$container->loadFromExtension('framework', [
123128
'router' => [
124-
'resource' => 'kernel:loadRoutes',
129+
'resource' => 'kernel::loadRoutes',
125130
'type' => 'service',
126131
],
127132
]);
@@ -131,6 +136,20 @@ public function registerContainerConfiguration(LoaderInterface $loader)
131136
$loader->load($path);
132137
}
133138

139+
$kernelClass = false !== strpos(static::class, "@anonymous\0") ? parent::class : static::class;
140+
141+
if (!$container->hasDefinition('kernel')) {
142+
$container->register('kernel', $kernelClass)
143+
->addTag('controller.service_arguments')
144+
->setAutoconfigured(true)
145+
->setSynthetic(true)
146+
->setPublic(true)
147+
;
148+
}
149+
150+
$kernelDefinition = $container->getDefinition('kernel');
151+
$kernelDefinition->addTag('routing.route_loader');
152+
134153
$container->addObjectResource($this);
135154
});
136155
}
@@ -143,7 +162,12 @@ public function registerContainerConfiguration(LoaderInterface $loader)
143162
public function loadRoutes(LoaderInterface $loader)
144163
{
145164
$routes = new RouteCollectionBuilder($loader);
146-
$routes->import(__DIR__.'/config/routing.yml');
165+
166+
if ($this->routingFile) {
167+
$routes->import($this->routingFile);
168+
} else {
169+
$routes->import(__DIR__.'/config/routing.yml');
170+
}
147171

148172
return $routes->build();
149173
}
@@ -169,4 +193,12 @@ public function addCompilerPasses(array $compilerPasses)
169193
{
170194
$this->compilerPasses = $compilerPasses;
171195
}
196+
197+
/**
198+
* @param string|null $routingFile
199+
*/
200+
public function setRoutingFile($routingFile)
201+
{
202+
$this->routingFile = $routingFile;
203+
}
172204
}

src/BaseBundleTestCase.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ abstract class BaseBundleTestCase extends TestCase
2727
*/
2828
abstract protected function getBundleClass();
2929

30+
/**
31+
* @var string|null
32+
*/
33+
private $routingFile = null;
34+
3035
/**
3136
* Boots the Kernel for this test.
3237
*
@@ -68,6 +73,7 @@ protected function createKernel()
6873
$this->kernel = new $class(uniqid('cache'));
6974
$this->kernel->addBundle($this->getBundleClass());
7075
$this->kernel->addCompilerPasses($this->compilerPasses);
76+
$this->kernel->setRoutingFile($this->routingFile);
7177

7278
return $this->kernel;
7379
}
@@ -92,11 +98,16 @@ public function ensureKernelShutdown()
9298
}
9399
}
94100

95-
/**
96-
* @param CompilerPassInterface $compilerPass
97-
*/
98101
protected function addCompilerPass(CompilerPassInterface $compilerPass)
99102
{
100103
$this->compilerPasses[] = $compilerPass;
101104
}
105+
106+
/**
107+
* @param string|null $routingFile
108+
*/
109+
public function setRoutingFile($routingFile)
110+
{
111+
$this->routingFile = $routingFile;
112+
}
102113
}

0 commit comments

Comments
 (0)