Skip to content

[Configuration] Actual code example for Multiple Kernels #15407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions configuration/multiple_kernels.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ Kernel. Be sure to also change the location of the cache, logs and configuration
files so they don't collide with the files from ``src/Kernel.php``::

// src/ApiKernel.php
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

class ApiKernel extends Kernel
{
Expand All @@ -101,36 +102,33 @@ files so they don't collide with the files from ``src/Kernel.php``::
}
}

public function getProjectDir(): string
{
return \dirname(__DIR__);
}

public function getCacheDir(): string
{
return $this->getProjectDir().'/var/cache/api/'.$this->getEnvironment();
return $this->getProjectDir().'/var/cache/api/'.$this->environment;
}

public function getLogDir(): string
{
return $this->getProjectDir().'/var/log/api';
}

public function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
protected function configureContainer(ContainerConfigurator $container): void
{
$container->addResource(new FileResource($this->getProjectDir().'/config/api_bundles.php'));
$container->setParameter('container.dumper.inline_factories', true);
$confDir = $this->getProjectDir().'/config/api';

$loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/{packages}/'.$this->environment.'/*'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob');
$container->import('../config/api/{packages}/*.yaml');
$container->import('../config/api/{packages}/'.$this->environment.'/*.yaml');

if (is_file(\dirname(__DIR__).'/config/api/services.yaml')) {
$container->import('../config/api/services.yaml');
$container->import('../config/api/{services}_'.$this->environment.'.yaml');
} else {
$container->import('../config/api/{services}.php');
}
}

protected function configureRoutes(RouteCollectionBuilder $routes): void
protected function configureRoutes(RoutingConfigurator $routes): void
{
$confDir = $this->getProjectDir().'/config/api';
$routes->import('../config/api/{routes}/'.$this->environment.'/*.yaml');
$routes->import('../config/api/{routes}/*.yaml');
// ... load only the config routes strictly needed for the API
}
}
Expand Down