Created
September 2, 2019 15:14
-
-
Save ThijsFeryn/abe63b6ac2e73fb35b188a6559ad97eb to your computer and use it in GitHub Desktop.
edgestash negotiation in Symfony
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\EdgestashBundle\EventSubscriber; | |
use Symfony\Component\EventDispatcher\Event; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | |
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
use Symfony\Component\HttpKernel\KernelEvents; | |
class EdgestashSubscriber implements EventSubscriberInterface | |
{ | |
public static function getSubscribedEvents() | |
{ | |
return [ | |
KernelEvents::REQUEST => [ | |
['request'] | |
], | |
KernelEvents::RESPONSE => [ | |
['response',-10] | |
], | |
]; | |
} | |
public function request(GetResponseEvent $event) | |
{ | |
$request = $event->getRequest(); | |
if ($request->headers->has('Surrogate-Capability') && | |
false !== strpos( | |
$request->headers->get('Surrogate-Capability'), | |
'edgestash="EDGESTASH/2.1"' | |
) | |
) { | |
$request->attributes->set('edgestash',true); | |
} else { | |
$request->attributes->set('edgestash',false); | |
} | |
} | |
public function response(FilterResponseEvent $event) | |
{ | |
$request = $event->getRequest(); | |
$response = $event->getResponse(); | |
if($request->attributes->has('edgestash') && $request->attributes->get('edgestash') !== false) { | |
$response->headers->set('Surrogate-Control','edgestash="EDGESTASH/2.1"',false); | |
if($request->attributes->has('edgestash-json-urls') | |
&& count($request->attributes->get('edgestash-json-urls')) > 0) { | |
$urls = array_unique($request->attributes->get('edgestash-json-urls')); | |
foreach ($urls as $url) { | |
$response->headers->set('Link','<'.$url.'>; rel=edgestash',false); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment