Using HttpKernelInterface for Painless Integration
-
Upload
ciaranmcnulty -
Category
Technology
-
view
160 -
download
1
description
Transcript of Using HttpKernelInterface for Painless Integration
![Page 1: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/1.jpg)
L I G H T N I N G T A L K S S E S S I O N 1
USING THE SYMFONY2 COMPONENTS FOR PAINLESS INTEGRATION
C I A R A N M C N U L T Y
![Page 2: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/2.jpg)
DRUPAL + SYMFONY
![Page 3: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/3.jpg)
– F A B I E N P O T E N C I E R , 2 0 1 1
”SYMFONY2 IS A REUSABLE SET OF STANDALONE, DECOUPLED, AND COHESIVE PHP COMPONENTS
THAT SOLVE COMMON WEB DEVELOPMENT PROBLEMS.”
![Page 4: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/4.jpg)
SYMFONY2 ADOPTION
http://zalas.eu/symfony2-adoption/
![Page 5: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/5.jpg)
SYMFONY2 ADOPTION
Out of 4994 packages that depend on Symfony: !
1911 depend on the symfony/framework-bundle, !
1985 depend on specific components, !
1098 depend on symfony/symfony.
Driven by Components
http://zalas.eu/symfony2-adoption/
![Page 6: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/6.jpg)
SYMFONY2 ADOPTION
SHARED COMPONENTS !
= !
INTEROPERABILITY
![Page 7: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/7.jpg)
HTTPFOUNDATION+
HTTPKERNEL
![Page 8: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/8.jpg)
REQUEST/RESPONSE MODEL
APPLICATION
HTTP REQUEST HTTP RESPONSE
![Page 9: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/9.jpg)
REQUEST/RESPONSE MODEL
APPLICATION
HTTP REQUEST HTTP RESPONSE
Request Object Response ObjectSymfony\Component\HttpFoundation\Request Symfony\Component\HttpFoundation\Response
![Page 10: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/10.jpg)
REQUEST/RESPONSE MODEL
APPLICATION
HTTP REQUEST HTTP RESPONSE
Request Object Response ObjectSymfony\Component\HttpFoundation\Request Symfony\Component\HttpFoundation\Response
Kernel InterfaceSymfony\Component\HttpKernel\HttpKernelInterface
![Page 11: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/11.jpg)
KERNEL INTERFACEinterface HttpKernelInterface { const MASTER_REQUEST = 1; const SUB_REQUEST = 2; ! /** * @return Response */ public function handle( Request $request, $type = self::MASTER_REQUEST, $catch = true ); }
![Page 12: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/12.jpg)
SYMFONY FRAMEWORK USAGE
$kernel = new AppKernel('prod', false); $kernel->loadClassCache(); !$request = Request::createFromGlobals(); !$response = $kernel->handle($request); !$response->send();
![Page 13: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/13.jpg)
DRUPAL 8 USAGE
$request = Request::createFromGlobals(); !$kernel = DrupalKernel::createFromRequest( $request, $autoloader, ‘prod' ); !$response = $kernel->handle($request); !$response->send();
![Page 14: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/14.jpg)
HTTPKERNEL USAGE
![Page 15: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/15.jpg)
WHAT IS THE BENEFIT?
![Page 16: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/16.jpg)
SYMFONY2 PAGE CACHING
$kernel = new AppKernel('prod', false); $kernel->loadClassCache(); !$kernel = new AppCache($kernel); !$request = Request::createFromGlobals(); !$response = $kernel->handle($request); !$response->send();
![Page 17: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/17.jpg)
“Symfony's HttpKernelInterface provides a solid interface, which makes creating and sharing framework-agnostic HTTP filters a breeze”
![Page 18: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/18.jpg)
WRITING A MIDDLEWAREclass DrupalConPromoter implements HttpKernelInterface { private $innerKernel; ! public function __construct(HttpKernelInterface $kernel) { $this->innerKernel = $kernel; } ! public function handle( Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true ) { $response = $this->innerKernel->handle($request, $type, $catch); $newContent = str_replace( '<body>', '<body><h1>DRUPALCON ROCKS</h1>', $response->getContents()); $response->setContents($newContent); ! return $response; } }
![Page 19: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/19.jpg)
USING OUR OWN MIDDLEWARE
$kernel = new AppKernel('prod', false); $kernel->loadClassCache(); $kernel = new AppCache($kernel); !$kernel = new DrupalConPromoter($kernel); !$request = Request::createFromGlobals(); !$response = $kernel->handle($request); !$response->send();
![Page 20: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/20.jpg)
AVAILABLE MIDDLEWARES
CookieGuard - Encrypts/decrypts cookies !
GeoIP - Looks up country from IP and adds to Request !
CORS - Adds CORS headers !
Negotiation - Works out what content-type to send client !
IpRestrict - block blacklisted IP Requests !
Backstage - displays maintenance page if one exists !
+ plenty more!
![Page 21: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/21.jpg)
USING IN DRUPAL
Problem: ’Edit index.php’ is not something a typical Drupal
developer is happy with !
Solution: Middlewares are registered via the Service Container
(another Symfony component)
<service id=“my_drupalcon_promoter” class=“DrupalConPromoter”>"" <tag name=“http_middleware” priority=“1” />"</service>
![Page 22: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/22.jpg)
RUNNING MULTIPLE KERNELS!$request = Request::createFromGlobals(); !$symfonyKernel = new AppKernel('prod', false); $symfonyKernel->loadClassCache(); !$drupalKernel = DrupalKernel::createFromRequest( $request, $autoloader, 'prod' ); !$kernel = UrlMap( $drupalKernel, ['/blog' => $symfonyKernel] ); !$response = $kernel->handle($request); $response->send();
![Page 23: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/23.jpg)
CIARAN MCNULTY
!@ciaranmcnulty
! Core contributor to PhpSpec
!BDD / Agile / DDD enthusiast
!Senior Trainer at Inviqa Group
![Page 24: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/24.jpg)
See us on stand 309
![Page 25: Using HttpKernelInterface for Painless Integration](https://reader033.fdocuments.us/reader033/viewer/2022051412/5481b739b07959290c8b45e0/html5/thumbnails/25.jpg)
WHAT DID YOU THINK?E V A U L A T E T H I S S E S S I O N - A M S T E R D A M 2 0 1 4 . D R U P A L . O R G / S C H E D U L E
THANK YOU!