Post on 05-Dec-2014
description
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
DRUPAL + SYMFONY
– 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.”
SYMFONY2 ADOPTION
http://zalas.eu/symfony2-adoption/
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/
SYMFONY2 ADOPTION
SHARED COMPONENTS !
= !
INTEROPERABILITY
HTTPFOUNDATION+
HTTPKERNEL
REQUEST/RESPONSE MODEL
APPLICATION
HTTP REQUEST HTTP RESPONSE
REQUEST/RESPONSE MODEL
APPLICATION
HTTP REQUEST HTTP RESPONSE
Request Object Response ObjectSymfony\Component\HttpFoundation\Request Symfony\Component\HttpFoundation\Response
REQUEST/RESPONSE MODEL
APPLICATION
HTTP REQUEST HTTP RESPONSE
Request Object Response ObjectSymfony\Component\HttpFoundation\Request Symfony\Component\HttpFoundation\Response
Kernel InterfaceSymfony\Component\HttpKernel\HttpKernelInterface
KERNEL INTERFACEinterface HttpKernelInterface { const MASTER_REQUEST = 1; const SUB_REQUEST = 2; ! /** * @return Response */ public function handle( Request $request, $type = self::MASTER_REQUEST, $catch = true ); }
SYMFONY FRAMEWORK USAGE
$kernel = new AppKernel('prod', false); $kernel->loadClassCache(); !$request = Request::createFromGlobals(); !$response = $kernel->handle($request); !$response->send();
DRUPAL 8 USAGE
$request = Request::createFromGlobals(); !$kernel = DrupalKernel::createFromRequest( $request, $autoloader, ‘prod' ); !$response = $kernel->handle($request); !$response->send();
HTTPKERNEL USAGE
WHAT IS THE BENEFIT?
SYMFONY2 PAGE CACHING
$kernel = new AppKernel('prod', false); $kernel->loadClassCache(); !$kernel = new AppCache($kernel); !$request = Request::createFromGlobals(); !$response = $kernel->handle($request); !$response->send();
“Symfony's HttpKernelInterface provides a solid interface, which makes creating and sharing framework-agnostic HTTP filters a breeze”
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; } }
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();
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!
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>
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();
CIARAN MCNULTY
!@ciaranmcnulty
! Core contributor to PhpSpec
!BDD / Agile / DDD enthusiast
!Senior Trainer at Inviqa Group
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 L E
THANK YOU!