Implement kernel autodiscovering
This commit is contained in:
@@ -0,0 +1,214 @@
|
|||||||
|
Feature: Autodiscovering the application kernel
|
||||||
|
|
||||||
|
Background:
|
||||||
|
Given a standard Symfony autoloader configured
|
||||||
|
And a feature file containing:
|
||||||
|
"""
|
||||||
|
Feature:
|
||||||
|
Scenario:
|
||||||
|
Then the passed service should be an instance of "\Psr\Container\ContainerInterface"
|
||||||
|
"""
|
||||||
|
And a Behat configuration containing:
|
||||||
|
"""
|
||||||
|
default:
|
||||||
|
extensions:
|
||||||
|
FriendsOfBehat\SymfonyExtension: ~
|
||||||
|
|
||||||
|
suites:
|
||||||
|
default:
|
||||||
|
contexts:
|
||||||
|
- App\Tests\SomeContext
|
||||||
|
"""
|
||||||
|
And a context file "tests/SomeContext.php" containing:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Tests;
|
||||||
|
|
||||||
|
use Behat\Behat\Context\Context;
|
||||||
|
|
||||||
|
final class SomeContext implements Context {
|
||||||
|
private $service;
|
||||||
|
|
||||||
|
public function __construct($service = null) { $this->service = $service; }
|
||||||
|
|
||||||
|
/** @Then the passed service should be an instance of :expected */
|
||||||
|
public function serviceShouldBe(string $expected): void
|
||||||
|
{
|
||||||
|
assert(is_object($this->service));
|
||||||
|
assert($this->service instanceof $expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
And a services file "config/services.yaml" containing:
|
||||||
|
"""
|
||||||
|
services:
|
||||||
|
App\Tests\SomeContext:
|
||||||
|
public: true
|
||||||
|
arguments:
|
||||||
|
- "@service_container"
|
||||||
|
"""
|
||||||
|
|
||||||
|
Scenario: Autodiscovering kernel in Symfony 3 directory structure application
|
||||||
|
Given a kernel file "src/Kernel.php" containing:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||||
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
|
||||||
|
use Symfony\Component\Routing\RouteCollectionBuilder;
|
||||||
|
|
||||||
|
class Kernel extends HttpKernel
|
||||||
|
{
|
||||||
|
use MicroKernelTrait;
|
||||||
|
|
||||||
|
public function registerBundles(): iterable
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
|
||||||
|
new \FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
|
||||||
|
{
|
||||||
|
$container->loadFromExtension('framework', [
|
||||||
|
'test' => $this->getEnvironment() === 'test',
|
||||||
|
'secret' => 'Pigeon',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$loader->load(__DIR__ . '/../config/services.yaml');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configureRoutes(RouteCollectionBuilder $routes): void {}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
When I run Behat
|
||||||
|
Then it should pass
|
||||||
|
|
||||||
|
Scenario: Autodiscovering kernel in Symfony 4 directory structure application
|
||||||
|
Given a kernel file "app/AppKernel.php" containing:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||||
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
|
||||||
|
use Symfony\Component\Routing\RouteCollectionBuilder;
|
||||||
|
|
||||||
|
class AppKernel extends HttpKernel
|
||||||
|
{
|
||||||
|
use MicroKernelTrait;
|
||||||
|
|
||||||
|
public function registerBundles(): iterable
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
|
||||||
|
new \FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
|
||||||
|
{
|
||||||
|
$container->loadFromExtension('framework', [
|
||||||
|
'test' => $this->getEnvironment() === 'test',
|
||||||
|
'secret' => 'Pigeon',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$loader->load(__DIR__ . '/../config/services.yaml');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configureRoutes(RouteCollectionBuilder $routes): void {}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
When I run Behat
|
||||||
|
Then it should pass
|
||||||
|
|
||||||
|
Scenario: Failing to autodiscover the kernel
|
||||||
|
When I run Behat
|
||||||
|
Then it should fail with "Could not autodiscover the application kernel"
|
||||||
|
|
||||||
|
Scenario: Failing to autodiscover the kernel
|
||||||
|
Given a kernel file "src/Kernel.php" containing:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||||
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
|
||||||
|
use Symfony\Component\Routing\RouteCollectionBuilder;
|
||||||
|
|
||||||
|
class Kernel extends HttpKernel
|
||||||
|
{
|
||||||
|
use MicroKernelTrait;
|
||||||
|
|
||||||
|
public function registerBundles(): iterable
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
|
||||||
|
new \FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
|
||||||
|
{
|
||||||
|
$container->loadFromExtension('framework', [
|
||||||
|
'test' => $this->getEnvironment() === 'test',
|
||||||
|
'secret' => 'Pigeon',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$loader->load(__DIR__ . '/../config/services.yaml');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configureRoutes(RouteCollectionBuilder $routes): void {}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
And a kernel file "app/AppKernel.php" containing:
|
||||||
|
"""
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||||
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
|
||||||
|
use Symfony\Component\Routing\RouteCollectionBuilder;
|
||||||
|
|
||||||
|
class AppKernel extends HttpKernel
|
||||||
|
{
|
||||||
|
use MicroKernelTrait;
|
||||||
|
|
||||||
|
public function registerBundles(): iterable
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
|
||||||
|
new \FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
|
||||||
|
{
|
||||||
|
$container->loadFromExtension('framework', [
|
||||||
|
'test' => $this->getEnvironment() === 'test',
|
||||||
|
'secret' => 'Pigeon',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$loader->load(__DIR__ . '/../config/services.yaml');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configureRoutes(RouteCollectionBuilder $routes): void {}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
When I run Behat
|
||||||
|
Then it should fail with "Could not autodiscover the application kernel"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
Feature: Loading kernel based on configuration
|
Feature: Loading configured application kernel
|
||||||
|
|
||||||
Background:
|
Background:
|
||||||
Given a standard Symfony autoloader configured
|
Given a standard Symfony autoloader configured
|
||||||
@@ -56,7 +56,7 @@ final class SymfonyExtension implements Extension
|
|||||||
->addDefaultsIfNotSet()
|
->addDefaultsIfNotSet()
|
||||||
->children()
|
->children()
|
||||||
->scalarNode('path')->defaultNull()->end()
|
->scalarNode('path')->defaultNull()->end()
|
||||||
->scalarNode('class')->isRequired()->end()
|
->scalarNode('class')->defaultNull()->end()
|
||||||
->end()
|
->end()
|
||||||
->end()
|
->end()
|
||||||
->end()
|
->end()
|
||||||
@@ -65,7 +65,7 @@ final class SymfonyExtension implements Extension
|
|||||||
|
|
||||||
public function load(ContainerBuilder $container, array $config): void
|
public function load(ContainerBuilder $container, array $config): void
|
||||||
{
|
{
|
||||||
$this->loadKernel($container, $config['kernel']);
|
$this->loadKernel($container, $this->processKernelConfiguration($config['kernel']));
|
||||||
$this->loadDriverKernel($container);
|
$this->loadDriverKernel($container);
|
||||||
|
|
||||||
$this->loadKernelRebooter($container);
|
$this->loadKernelRebooter($container);
|
||||||
@@ -154,4 +154,35 @@ final class SymfonyExtension implements Extension
|
|||||||
|
|
||||||
$container->setDefinition('fob_symfony.mink.parameters', $minkParametersDefinition);
|
$container->setDefinition('fob_symfony.mink.parameters', $minkParametersDefinition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function processKernelConfiguration(array $config): array
|
||||||
|
{
|
||||||
|
if ($config['class'] !== null) {
|
||||||
|
return $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
$autoconfigured = 0;
|
||||||
|
|
||||||
|
if (class_exists('\App\Kernel')) {
|
||||||
|
$config['class'] = '\App\Kernel';
|
||||||
|
|
||||||
|
++$autoconfigured;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists('app/AppKernel.php')) {
|
||||||
|
$config['class'] = '\AppKernel';
|
||||||
|
$config['path'] = 'app/AppKernel.php';
|
||||||
|
|
||||||
|
++$autoconfigured;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($autoconfigured !== 1) {
|
||||||
|
throw new \RuntimeException(
|
||||||
|
'Could not autodiscover the application kernel. ' .
|
||||||
|
'Please define it manually with "FriendsOfBehat\SymfonyExtension.kernel" configuration option.'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $config;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user