MicroKernel aneb spatny nazev pro Helper (5. sraz pratel Symfony)

Post on 09-Jan-2017

160 views 1 download

transcript

5. sraz přátel Symfony - Kdo ví víc?Praha, 18. 2. 2016, Vím Víc Hub

Martin Zeman

@zemistr

K Vašim službám!

Dnešní téma:

MicroKernel(aneb špatný název pro Helper)

Co to, k*rva, je?

Micro + Kernel

Micro

Kernel

Anglický název kernel znamená (v překladu):● jádro pecky● zrno● ztvrdlá dužina ovoce

https://cs.wikipedia.org/wiki/Jádro_operačního_systému

MicroKernel=

malé jádro pecky

Chápou všichni?

Kernel

Jádro operačního systému (anglicky kernel) je v informatice částoperačního systému, která je zavedena do operační paměti přistartu (bootování) počítače a je jí předáno řízení. U pokročilýchoperačních systémů jádro nikdy neztrácí kontrolu nad počítačema po celou dobu jeho běhu koordinuje činnost všech spuštěnýchprocesů. Bla bla bla ...

https://cs.wikipedia.org/wiki/Jádro_operačního_systému

MicroKernel

Mikrojádro je v informatice typ jádra operačního systému, které jevelmi malé a obsahuje jen nejzákladnější funkce (typicky správupaměti a podporu pro plánování procesů a meziprocesovékomunikace), čímž se minimalizuje objem běžícího kódu vprivilegovaném režimu. Bla bla bla ...

https://cs.wikipedia.org/wiki/Mikrojádro

Kernel(Symfony kernel)

Co to vlastně je ten Symfony Kernel?

MicroKernel(Symfony kernel)

Co to vlastně je ten Symfony MicroKernel?

MicroKernel(Symfony kernel)

NOVINKA !!!

MicroKernel(Symfony kernel)

NOVINKA !!! v 2.8 +

http://symfony.com/blog/new-in-symfony-2-8-symfony-as-a-microframeworkhttps://knpuniversity.com/screencast/new-in-symfony3/micro-kernel

// vendor/symfony/framework-bundle/Kernel/MicroKernelTrait.phptrait MicroKernelTrait { abstract protected function configureRoutes(RouteCollectionBuilder $routes); abstract protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader);

public function registerContainerConfiguration(LoaderInterface $loader) { $loader->load( function (ContainerBuilder $container) use ($loader) { $container->loadFromExtension('framework', [ 'router' => [ 'resource' => 'kernel:loadRoutes', 'type' => 'service' ] ] ); $this->configureContainer($container, $loader); $container->addObjectResource($this); } ); }

public function loadRoutes(LoaderInterface $loader) { $routes = new RouteCollectionBuilder($loader); $this->configureRoutes($routes);

return $routes->build(); }}

Ano, Trait je malý, ale to je asi vše...

{ "require": { "symfony/framework-bundle": "3.0.*" }}

Pro použití stačí jen stáhnout symfony/framework-bundle

Složka “vendor”:Složek: 486

Souborů: 2063Velikost: 5.32 MB

Po stažení

// index.phpclass LittleKernel extends Kernel { use MicroKernelTrait;

public function registerBundles() { return [ new FrameworkBundle() ]; }

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) { $c->loadFromExtension('framework', ['secret' => 'James Bond 007']); }

protected function configureRoutes(RouteCollectionBuilder $routes) { $routes->add('/hello/{name}', 'Kernel:helloAction'); }

public function helloAction($name) { return new Response("Hello $name"); }}

$kernel = new LittleKernel('dev', true);$kernel->loadClassCache();$kernel->handle(Request::createFromGlobals())->send();

A přidat pár řádků do index.php

Tadá!

server.local/index.php/hello/007

// index.phpclass LittleKernel extends Kernel { use MicroKernelTrait;

public function registerBundles() { return [ new FrameworkBundle(), ]; }

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) { $c->loadFromExtension('framework', ['secret' => 'James Bond 007']); }

protected function configureRoutes(RouteCollectionBuilder $routes) { $routes->add('/hello/{name}', 'Kernel:helloAction'); }

public function helloAction($name) { return new Response("Hello $name"); }}

$kernel = new LittleKernel('dev', true);$kernel->loadClassCache();$kernel->handle(Request::createFromGlobals())->send();

"Hello $name"

A přidat pár řádků do index.php

+ Twig

{ "require": { "symfony/framework-bundle": "3.0.*", "symfony/twig-bundle": "3.0.*" }}

Pro použití stačí jen stáhnout symfony/twig-bundle

Složka “vendor”:Složek: 638 (+152)

Souborů: 2859 (+796)Velikost: 6.72 MB (+1.4)

Po stažení

// index.phpclass LittleKernel extends Kernel {... public function registerBundles() { return [ new FrameworkBundle(), new TwigBundle() ]; }

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) { $c->loadFromExtension( 'framework', [ 'secret' => 'James Bond 007', 'templating' => ['engines' => ['twig']] ] ); }... public function helloAction($name) { $twig = $this->getContainer()->get('twig'); $template = $twig->createTemplate('Hello {{ name }}'); $html = $template->render(['name' => $name]);

return new Response($html); }}...

'Hello {{ name }}'

server.local/index.php/hello/007

+ Controllery+ Šablony

{ "require": { "symfony/framework-bundle": "3.0.*", "symfony/twig-bundle": "3.0.*" }, "autoload": { "psr-4": { "": "src/" } }}

// src/AppBundle/AppBundle.php<?phpnamespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AppBundle extends Bundle {}

Bond komunikuje s M

// src/AppBundle/AppBundle.php<?phpnamespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AppBundle extends Bundle {}

// src/AppBundle/Controller/MController.php<?phpnamespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MController extends Controller { public function helloAction($name) { return $this->render('AppBundle:M:hello.html.twig', ['name' => $name]); }}

// src/AppBundle/Resources/views/M/hello.html.twigHello {{ name }}

// index.phpclass LittleKernel extends Kernel { use MicroKernelTrait;

public function registerBundles() { return [ new FrameworkBundle(), new TwigBundle(), new AppBundle(), ]; }

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) { $c->loadFromExtension( 'framework', [ 'secret' => 'James Bond 007', 'templating' => ['engines' => ['twig']] ] ); }

protected function configureRoutes(RouteCollectionBuilder $routes) { $routes->add('/hello/{name}', 'AppBundle:M:hello'); }}...

server.local/index.php/hello/007

+ Lepší routy+ Lepší šablony

{ "require": { "symfony/framework-bundle": "3.0.*", "symfony/twig-bundle": "3.0.*", "sensio/framework-extra-bundle": "3.0.*" }, "autoload": { "psr-4": { "": "src/" } }}

Pro použití stačí jen stáhnout sensio/framework-extra-bundle

Složka “vendor”:Složek: 707 (+69)

Souborů: 3036 (+177)Velikost: 7.28 MB (+0.56)

Po stažení

// src/AppBundle/Controller/MController.phpclass MController extends Controller { /** * @Route("/hello/{name}") * @Template() */ public function helloAction($name) { return ['name' => $name]; }}

// index.php$loader = require __DIR__ . '/vendor/autoload.php';AnnotationRegistry::registerLoader([$loader, 'loadClass']);

class LittleKernel extends Kernel { use MicroKernelTrait;

public function registerBundles() { return [ new FrameworkBundle(), new TwigBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), new AppBundle(), ]; }

...

protected function configureRoutes(RouteCollectionBuilder $routes) { $routes->import('@AppBundle/Controller', '/', 'annotation'); }}

...

server.local/index.php/hello/007

Prosím!Rozděl ten index!

// index.phpuse Doctrine\Common\Annotations\AnnotationRegistry;use Symfony\Component\HttpFoundation\Request;

$loader = require __DIR__ . '/vendor/autoload.php';AnnotationRegistry::registerLoader([$loader, 'loadClass']);

require __DIR__ . '/LittleKernel.php';

$kernel = new LittleKernel('dev', true);$request = Request::createFromGlobals();$response = $kernel->handle($request);$response->send();$kernel->terminate($request, $response);

<?php// LittleKernel.php...class LittleKernel extends Kernel { use MicroKernelTrait;

public function registerBundles() { return [ new FrameworkBundle(), new TwigBundle(), new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), new AppBundle(), ]; }

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) { $c->loadFromExtension( 'framework', [ 'secret' => 'James Bond 007', 'templating' => ['engines' => ['twig']] ] ); }

protected function configureRoutes(RouteCollectionBuilder $routes) { $routes->import('@AppBundle/Controller', '/', 'annotation'); }}

Výsledek (fujky):Controllery: 0

Šablony: 0

Bundly: 1

Jiné soubory: 1 (index)

Výsledek (mňam):Controllery: 1

Šablony: 1

Bundly: 4

Jiné soubory: 2 (index + LittleKernel)

Složka “vendor”:Složek: 707

Souborů: 3036Velikost: 7.28 MB

Micro?

MicroKernel(Symfony kernel)

KernelHelper?

+1

Hmmm, nejsem jediný, komu se ten název nelíbí.

Používat?x

Nepoužívat?

Otázky?