Skip to content

Commit

Permalink
Merge branch '5.1'
Browse files Browse the repository at this point in the history
* 5.1:
  [symfony#13919] Use ContainerConfigurator and fixes some bugs after testing the example code
  Fixed micro kernel demo
  • Loading branch information
wouterj committed Jul 7, 2020
2 parents 1f8d41c + e1c35a5 commit 9dc447a
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions configuration/micro_kernel_trait.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ Next, create an ``index.php`` file that defines the kernel class and executes it

// index.php
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
Expand All @@ -37,29 +36,27 @@ Next, create an ``index.php`` file that defines the kernel class and executes it
{
use MicroKernelTrait;

public function registerBundles()
public function registerBundles(): array
{
return [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle()
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
];
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
protected function configureContainer(ContainerConfigurator $c): void
{
// PHP equivalent of config/packages/framework.yaml
$c->loadFromExtension('framework', [
$c->extension('framework', [
'secret' => 'S0ME_SECRET'
]);
}

protected function configureRoutes(RoutingConfigurator $routes)
protected function configureRoutes(RoutingConfigurator $routes): void
{
// kernel is a service that points to this class
// optional 3rd argument is the route name
$routes->add('/random/{limit}', 'kernel::randomNumber');
$routes->add('random_number', '/random/{limit}')->controller([$this, 'randomNumber']);
}

public function randomNumber($limit)
public function randomNumber(int $limit): JsonResponse
{
return new JsonResponse([
'number' => random_int(0, $limit),
Expand Down Expand Up @@ -91,9 +88,9 @@ that define your bundles, your services and your routes:
**registerBundles()**
This is the same ``registerBundles()`` that you see in a normal kernel.

**configureContainer(ContainerBuilder $c, LoaderInterface $loader)**
**configureContainer(ContainerConfigurator $c)**
This method builds and configures the container. In practice, you will use
``loadFromExtension`` to configure different bundles (this is the equivalent
``extension()`` to configure different bundles (this is the equivalent
of what you see in a normal ``config/packages/*`` file). You can also register
services directly in PHP or load external configuration files (shown below).

Expand Down Expand Up @@ -134,43 +131,49 @@ hold the kernel. Now it looks like this::
namespace App;

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

class Kernel extends BaseKernel
{
use MicroKernelTrait;

public function registerBundles()
public function registerBundles(): array
{
$bundles = [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Symfony\Bundle\TwigBundle\TwigBundle(),
];

if ($this->getEnvironment() == 'dev') {
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new \Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
}

return $bundles;
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
protected function configureContainer(ContainerConfigurator $c): void
{
$loader->load(__DIR__.'/../config/framework.yaml');
$c->import(__DIR__.'/../config/framework.yaml');

// register all classes in /src/ as service
$c->services()
->load('App\\', __DIR__.'/*')
->autowire()
->autoconfigure()
;

// configure WebProfilerBundle only if the bundle is enabled
if (isset($this->bundles['WebProfilerBundle'])) {
$c->loadFromExtension('web_profiler', [
$c->extension('web_profiler', [
'toolbar' => true,
'intercept_redirects' => false,
]);
}
}

protected function configureRoutes(RoutingConfigurator $routes)
protected function configureRoutes(RoutingConfigurator $routes): void
{
// import the WebProfilerRoutes, only if the bundle is enabled
if (isset($this->bundles['WebProfilerBundle'])) {
Expand All @@ -179,17 +182,17 @@ hold the kernel. Now it looks like this::
}

// load the annotation routes
$routes->import(__DIR__.'/../src/Controller/', 'annotation');
$routes->import(__DIR__.'/Controller/', 'annotation');
}

// optional, to use the standard Symfony cache directory
public function getCacheDir()
public function getCacheDir(): string
{
return __DIR__.'/../var/cache/'.$this->getEnvironment();
}

// optional, to use the standard Symfony logs directory
public function getLogDir()
public function getLogDir(): string
{
return __DIR__.'/../var/log';
}
Expand Down Expand Up @@ -245,14 +248,15 @@ has one file in it::
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class MicroController extends AbstractController
{
/**
* @Route("/random/{limit}")
*/
public function randomNumber($limit)
public function randomNumber(int $limit): Response
{
$number = random_int(0, $limit);

Expand Down Expand Up @@ -327,7 +331,6 @@ As before you can use the :doc:`Symfony Local Web Server

.. code-block:: terminal
cd public/
$ symfony server:start
Then visit the page in your browser: http://localhost:8000/random/10

0 comments on commit 9dc447a

Please sign in to comment.