Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
USING THE SYMFONY2 COMPONENTS 
FOR PAINLESS INTEGRATION 
C I A R A N M C N U L T Y 
L I G H T N I N G T A L K S S E S S I ...
DRUPAL + SYMFONY
”SYMFONY2 IS A REUSABLE SET OF STANDALONE, 
DECOUPLED, AND COHESIVE PHP COMPONENTS 
THAT SOLVE COMMON WEB DEVELOPMENT 
PRO...
SYMFONY2 ADOPTION 
http://zalas.eu/symfony2-adoption/
SYMFONY2 ADOPTION 
Driven by Components 
Out of 4994 packages that depend on Symfony: 
! 
1911 depend on the symfony/frame...
SYMFONY2 ADOPTION 
SHARED COMPONENTS 
! 
= 
! 
INTEROPERABILITY
HTTPFOUNDATION 
+ 
HTTPKERNEL
REQUEST/RESPONSE MODEL 
HTTP REQUEST HTTP RESPONSE 
APPLICATION
REQUEST/RESPONSE MODEL 
HTTP REQUEST HTTP RESPONSE 
APPLICATION 
Request Object Response Object 
SymfonyComponentHttpFound...
REQUEST/RESPONSE MODEL 
HTTP REQUEST HTTP RESPONSE 
APPLICATION 
Request Object Response Object 
SymfonyComponentHttpFound...
KERNEL INTERFACE 
interface HttpKernelInterface 
{ 
const MASTER_REQUEST = 1; 
const SUB_REQUEST = 2; 
! 
/** 
* @return R...
SYMFONY FRAMEWORK USAGE 
$kernel = new AppKernel('prod', false); 
$kernel->loadClassCache(); 
! 
$request = Request::creat...
DRUPAL 8 USAGE 
$request = Request::createFromGlobals(); 
! 
$kernel = DrupalKernel::createFromRequest( 
$request, 
$autol...
HTTPKERNEL USAGE
WHAT IS THE 
BENEFIT?
SYMFONY2 PAGE CACHING 
$kernel = new AppKernel('prod', false); 
$kernel->loadClassCache(); 
! 
$kernel = new AppCache($ker...
“Symfony's HttpKernelInterface provides a solid interface, which 
makes creating and sharing framework-agnostic HTTP filte...
WRITING A MIDDLEWARE 
class DrupalConPromoter implements HttpKernelInterface 
{ 
private $innerKernel; 
! 
public function...
USING OUR OWN MIDDLEWARE 
$kernel = new AppKernel('prod', false); 
$kernel->loadClassCache(); 
$kernel = new AppCache($ker...
AVAILABLE MIDDLEWARES 
CookieGuard - Encrypts/decrypts cookies 
! 
GeoIP - Looks up country from IP and adds to Request 
!...
USING IN DRUPAL 
Problem: 
’Edit index.php’ is not something a typical Drupal 
developer is happy with 
! 
Solution: 
Midd...
RUNNING MULTIPLE KERNELS 
! 
$request = Request::createFromGlobals(); 
! 
$symfonyKernel = new AppKernel('prod', false); 
...
CIARAN MCNULTY 
! 
@ciaranmcnulty 
! 
Core contributor to PhpSpec 
! 
BDD / Agile / DDD enthusiast 
! 
Senior Trainer at I...
See us on stand 309
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...
Upcoming SlideShare
Loading in …5
×

Using HttpKernelInterface for Painless Integration

934 views

Published on

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.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Using HttpKernelInterface for Painless Integration

  1. 1. USING THE SYMFONY2 COMPONENTS FOR PAINLESS INTEGRATION C I A R A N M C N U L T Y L I G H T N I N G T A L K S S E S S I O N 1
  2. 2. DRUPAL + SYMFONY
  3. 3. ”SYMFONY2 IS A REUSABLE SET OF STANDALONE, DECOUPLED, AND COHESIVE PHP COMPONENTS THAT SOLVE COMMON WEB DEVELOPMENT PROBLEMS.” – F A B I E N P O T E N C I E R , 2 0 1 1
  4. 4. SYMFONY2 ADOPTION http://zalas.eu/symfony2-adoption/
  5. 5. SYMFONY2 ADOPTION Driven by Components 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. http://zalas.eu/symfony2-adoption/
  6. 6. SYMFONY2 ADOPTION SHARED COMPONENTS ! = ! INTEROPERABILITY
  7. 7. HTTPFOUNDATION + HTTPKERNEL
  8. 8. REQUEST/RESPONSE MODEL HTTP REQUEST HTTP RESPONSE APPLICATION
  9. 9. REQUEST/RESPONSE MODEL HTTP REQUEST HTTP RESPONSE APPLICATION Request Object Response Object SymfonyComponentHttpFoundationRequest SymfonyComponentHttpFoundationResponse
  10. 10. REQUEST/RESPONSE MODEL HTTP REQUEST HTTP RESPONSE APPLICATION Request Object Response Object SymfonyComponentHttpFoundationRequest SymfonyComponentHttpFoundationResponse Kernel Interface SymfonyComponentHttpKernelHttpKernelInterface
  11. 11. KERNEL INTERFACE interface HttpKernelInterface { const MASTER_REQUEST = 1; const SUB_REQUEST = 2; ! /** * @return Response */ public function handle( Request $request, $type = self::MASTER_REQUEST, $catch = true ); }
  12. 12. SYMFONY FRAMEWORK USAGE $kernel = new AppKernel('prod', false); $kernel->loadClassCache(); ! $request = Request::createFromGlobals(); ! $response = $kernel->handle($request); ! $response->send();
  13. 13. DRUPAL 8 USAGE $request = Request::createFromGlobals(); ! $kernel = DrupalKernel::createFromRequest( $request, $autoloader, ‘prod' ); ! $response = $kernel->handle($request); ! $response->send();
  14. 14. HTTPKERNEL USAGE
  15. 15. WHAT IS THE BENEFIT?
  16. 16. SYMFONY2 PAGE CACHING $kernel = new AppKernel('prod', false); $kernel->loadClassCache(); ! $kernel = new AppCache($kernel); ! $request = Request::createFromGlobals(); ! $response = $kernel->handle($request); ! $response->send();
  17. 17. “Symfony's HttpKernelInterface provides a solid interface, which makes creating and sharing framework-agnostic HTTP filters a breeze”
  18. 18. WRITING A MIDDLEWARE class 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; } }
  19. 19. 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();
  20. 20. 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!
  21. 21. 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>
  22. 22. 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();
  23. 23. CIARAN MCNULTY ! @ciaranmcnulty ! Core contributor to PhpSpec ! BDD / Agile / DDD enthusiast ! Senior Trainer at Inviqa Group
  24. 24. See us on stand 309
  25. 25. 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!

×