-
Notifications
You must be signed in to change notification settings - Fork 61
Provide Symfony HttpCache as trait #233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the FOSHttpCache package. | ||
* | ||
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FOS\HttpCache\SymfonyCache; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestMatcher; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Debug handler for the symfony built-in HttpCache. | ||
* | ||
* Add debug information to the response for use in cache tests. | ||
* | ||
* @author David Buchmann <[email protected]> | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
class DebugListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
Events::POST_HANDLE => 'handleDebug', | ||
]; | ||
} | ||
|
||
/** | ||
* Extract the cache HIT/MISS information from the X-Symfony-Cache header. | ||
* | ||
* For this header to be present, the HttpCache must be created with the | ||
* debug option set to true. | ||
* | ||
* @param CacheEvent $event | ||
*/ | ||
public function handleDebug(CacheEvent $event) | ||
{ | ||
$response = $event->getResponse(); | ||
if ($response->headers->has('X-Symfony-Cache')) { | ||
if (false !== strpos($response->headers->get('X-Symfony-Cache'), 'miss')) { | ||
$state = 'MISS'; | ||
} elseif (false !== strpos($response->headers->get('X-Symfony-Cache'), 'fresh')) { | ||
$state = 'HIT'; | ||
} else { | ||
$state = 'UNDETERMINED'; | ||
} | ||
$response->headers->set('X-Cache', $state); | ||
} | ||
} | ||
} |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,27 +11,36 @@ | |
|
||
namespace FOS\HttpCache\SymfonyCache; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\HttpCache\HttpCache; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
||
/** | ||
* Base class for enhanced Symfony reverse proxy based on the symfony component. | ||
* Trait for enhanced Symfony reverse proxy based on the symfony kernel component. | ||
* | ||
* <b>When using FOSHttpCacheBundle, look at FOS\HttpCacheBundle\HttpCache instead.</b> | ||
* Your kernel needs to implement CacheInvalidatorInterface and redeclare the | ||
* fetch method as public. (The latter is needed because the trait declaring it | ||
* public does not satisfy the interface for whatever reason. See also | ||
* http://stackoverflow.com/questions/31877844/php-trait-exposing-a-method-and-interfaces ) | ||
* | ||
* This kernel supports event subscribers that can act on the events defined in | ||
* FOS\HttpCache\SymfonyCache\Events and may alter the request flow. | ||
* CacheInvalidator kernels support event subscribers that can act on the | ||
* events defined in FOS\HttpCache\SymfonyCache\Events and may alter the | ||
* request flow. | ||
* | ||
* If your kernel overwrites any of the methods defined in this trait, make | ||
* sure to also call the trait method. You might get into issues with the order | ||
* of events, in which case you will need to copy event triggering into your | ||
* kernel. | ||
* | ||
* @author Jérôme Vieilledent <[email protected]> (courtesy of eZ Systems AS) | ||
* @author David Buchmann <[email protected]> | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
abstract class EventDispatchingHttpCache extends HttpCache implements CacheInvalidationInterface | ||
trait EventDispatchingHttpCache | ||
{ | ||
/** | ||
* @var EventDispatcherInterface | ||
|
@@ -69,17 +78,13 @@ public function addSubscriber(EventSubscriberInterface $subscriber) | |
*/ | ||
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) | ||
{ | ||
if ($this->getEventDispatcher()->hasListeners(Events::PRE_HANDLE)) { | ||
$event = new CacheEvent($this, $request); | ||
$this->getEventDispatcher()->dispatch(Events::PRE_HANDLE, $event); | ||
if ($event->getResponse()) { | ||
return $this->dispatchPostHandle($request, $event->getResponse()); | ||
} | ||
if ($response = $this->dispatch(Events::PRE_HANDLE, $request)) { | ||
return $this->dispatch(Events::POST_HANDLE, $request, $response); | ||
} | ||
|
||
$response = parent::handle($request, $type, $catch); | ||
|
||
return $this->dispatchPostHandle($request, $response); | ||
return $this->dispatch(Events::POST_HANDLE, $request, $response); | ||
} | ||
|
||
/** | ||
|
@@ -89,59 +94,42 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ | |
*/ | ||
protected function store(Request $request, Response $response) | ||
{ | ||
if ($this->getEventDispatcher()->hasListeners(Events::PRE_STORE)) { | ||
$event = new CacheEvent($this, $request, $response); | ||
$this->getEventDispatcher()->dispatch(Events::PRE_STORE, $event); | ||
$response = $event->getResponse(); | ||
} | ||
$response = $this->dispatch(Events::PRE_STORE, $request, $response); | ||
|
||
parent::store($request, $response); | ||
} | ||
|
||
/** | ||
* Dispatch the POST_HANDLE event if needed. | ||
* | ||
* @param Request $request | ||
* @param Response $response | ||
* {@inheritDoc} | ||
* | ||
* @return Response The response to return which might be altered by a POST_HANDLE listener. | ||
* Adding the Events::PRE_INVALIDATE event. | ||
*/ | ||
private function dispatchPostHandle(Request $request, Response $response) | ||
protected function invalidate(Request $request, $catch = false) | ||
{ | ||
if ($this->getEventDispatcher()->hasListeners(Events::POST_HANDLE)) { | ||
$event = new CacheEvent($this, $request, $response); | ||
$this->getEventDispatcher()->dispatch(Events::POST_HANDLE, $event); | ||
$response = $event->getResponse(); | ||
if ($response = $this->dispatch(Events::PRE_INVALIDATE, $request)) { | ||
return $response; | ||
} | ||
|
||
return $response; | ||
return parent::invalidate($request, $catch); | ||
} | ||
|
||
/** | ||
* Made public to allow event subscribers to do refresh operations. | ||
* Dispatch an event if needed. | ||
* | ||
* {@inheritDoc} | ||
*/ | ||
public function fetch(Request $request, $catch = false) | ||
{ | ||
return parent::fetch($request, $catch); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* @param string $name Name of the event to trigger. One of the constants in FOS\HttpCache\SymfonyCache\Events | ||
* @param Request $request | ||
* @param Response|null $response If already available | ||
* | ||
* Adding the Events::PRE_INVALIDATE event. | ||
* @return Response The response to return, which might be provided/altered by a listener. | ||
*/ | ||
protected function invalidate(Request $request, $catch = false) | ||
protected function dispatch($name, Request $request, Response $response = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removing as it does not work anyways |
||
{ | ||
if ($this->getEventDispatcher()->hasListeners(Events::PRE_INVALIDATE)) { | ||
$event = new CacheEvent($this, $request); | ||
$this->getEventDispatcher()->dispatch(Events::PRE_INVALIDATE, $event); | ||
if ($event->getResponse()) { | ||
return $event->getResponse(); | ||
} | ||
if ($this->getEventDispatcher()->hasListeners($name)) { | ||
$event = new CacheEvent($this, $request, $response); | ||
$this->getEventDispatcher()->dispatch($name, $event); | ||
$response = $event->getResponse(); | ||
} | ||
|
||
return parent::invalidate($request, $catch); | ||
return $response; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made this protected so that kernels using the trait can also use it, if they need to expand on a method and still need the pre and post event.