25
LIGHTNING TALKS SESSION 1 USING THE SYMFONY2 COMPONENTS FOR PAINLESS INTEGRATION CIARAN MCNULTY

Using HttpKernelInterface for Painless Integration

Embed Size (px)

DESCRIPTION

In this short session we will look at one of the Symfony2 components adopted since Drupal 8; the HttpKernelInterface. Attendees will gain familiarity with this component, the related Request and Response objects, and the overall top level structure of a Drupal 8 project's integration. We will also see how the Stack PHP project allows us to easily integrate a Drupal 8 site with another codebase that uses the HttpKernelInterface (including Symfony2, Silex, Laravel or even another Drupal 8 project). We will also explore some of the other functionality that can be added by Stack, all enabled by this powerful abstraction.

Citation preview

Page 1: Using HttpKernelInterface for Painless Integration

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

DRUPAL + SYMFONY

Page 3: Using HttpKernelInterface for Painless Integration

– 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

SYMFONY2 ADOPTION

http://zalas.eu/symfony2-adoption/

Page 5: Using HttpKernelInterface for Painless Integration

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

SYMFONY2 ADOPTION

SHARED COMPONENTS !

= !

INTEROPERABILITY

Page 7: Using HttpKernelInterface for Painless Integration

HTTPFOUNDATION+

HTTPKERNEL

Page 8: Using HttpKernelInterface for Painless Integration

REQUEST/RESPONSE MODEL

APPLICATION

HTTP REQUEST HTTP RESPONSE

Page 9: Using HttpKernelInterface for Painless Integration

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

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

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

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

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

HTTPKERNEL USAGE

Page 15: Using HttpKernelInterface for Painless Integration

WHAT IS THE BENEFIT?

Page 16: Using HttpKernelInterface for Painless Integration

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

“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

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

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

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

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

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

CIARAN MCNULTY

!@ciaranmcnulty

! Core contributor to PhpSpec

!BDD / Agile / DDD enthusiast

!Senior Trainer at Inviqa Group

Page 24: Using HttpKernelInterface for Painless Integration

See us on stand 309

Page 25: Using HttpKernelInterface for Painless Integration

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!