-
Notifications
You must be signed in to change notification settings - Fork 61
Adding support for user context for symfony HttpCache #145
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
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
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
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,116 @@ | ||
| Symfony HttpCache Configuration | ||
| ------------------------------- | ||
|
|
||
| The ``symfony/http-kernel`` component provides a reverse proxy implemented | ||
| completely in PHP, called `HttpCache`_. While it is certainly less efficient | ||
| than using Varnish or Nginx, it can still provide considerable performance | ||
| gains over an installation that is not cached at all. It can be useful for | ||
| running an application on shared hosting for instance. | ||
|
|
||
| You can use features of this library with the Symfony ``HttpCache``. The basic | ||
| concept is to use event subscribers on the HttpCache class. | ||
|
|
||
| .. note:: | ||
|
|
||
| If you are using the full stack Symfony framework, have a look at the | ||
| HttpCache provided by the FOSHttpCacheBundle_ instead. | ||
|
|
||
| .. warning:: | ||
|
|
||
| Symfony HttpCache support is currently limited to following features: | ||
|
|
||
| * User context | ||
|
|
||
| Extending the right HttpCache | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Instead of extending ``Symfony\Component\HttpKernel\HttpCache\HttpCache``, your | ||
| ``AppCache`` should extend ``FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache``. | ||
|
|
||
| .. tip:: | ||
|
|
||
| If your class already needs to extend a different class, simply copy the | ||
| event handling code from the EventDispatchingHttpCache into your | ||
| ``AppCache`` class. The drawback is that you need to manually check whether | ||
| you need to adjust your ``AppCache`` each time you update the FOSHttpCache | ||
| library. | ||
|
|
||
| Now that you have an event dispatching kernel, you can make it register the | ||
| subscribers you need. While you could do that from your bootstrap code, this is | ||
| not the recommended way. You would need to adjust every place you instantiate | ||
| the cache. Instead, overwrite the constructor of AppCache and register the | ||
| subscribers there. A simple cache will look like this:: | ||
|
|
||
| require_once __DIR__.'/AppKernel.php'; | ||
|
|
||
| use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache; | ||
| use FOS\HttpCache\SymfonyCache\UserContextSubscriber; | ||
|
|
||
| class AppCache extends EventDispatchingHttpCache | ||
| { | ||
| /** | ||
| * Overwrite constructor to register event subscribers for FOSHttpCache. | ||
| */ | ||
| public function __construct(HttpKernelInterface $kernel, $cacheDir = null) | ||
| { | ||
| parent::__construct($kernel, $cacheDir); | ||
|
|
||
| $this->addSubscriber(new UserContextSubscriber()); | ||
| } | ||
| } | ||
|
|
||
| User Context | ||
| ~~~~~~~~~~~~ | ||
|
|
||
| To support :doc:`user context hashing <user-context>` you need to register the | ||
| ``UserContextSubscriber``. If the default settings are right for you, you don't | ||
| need to do anything more. You can customize a number of options through the | ||
| constructor: | ||
|
|
||
| * **anonymous_hash**: Hash used for anonymous user. This is a performance | ||
| optimization to not do a backend request for users that are not logged in. | ||
|
|
||
| * **user_hash_accept_header**: Accept header value to be used to request the | ||
| user hash to the backend application. Must match the setup of the backend | ||
| application. | ||
|
|
||
| **default**: ``application/vnd.fos.user-context-hash`` | ||
|
|
||
| * **user_hash_header**: Name of the header the user context hash will be stored | ||
| into. Must match the setup for the Vary header in the backend application. | ||
|
|
||
| **default**: ``X-User-Context-Hash`` | ||
|
|
||
| * **user_hash_uri**: Target URI used in the request for user context hash | ||
| generation. | ||
|
|
||
| **default**: ``/_fos_user_context_hash`` | ||
|
|
||
| * **user_hash_method**: HTTP Method used with the hash lookup request for user | ||
| context hash generation. | ||
|
|
||
| **default**: ``GET`` | ||
|
|
||
| * **session_name_prefix**: Prefix for session cookies. Must match your PHP session configuration. | ||
|
|
||
| **default**: ``PHPSESSID`` | ||
|
|
||
| .. warning:: | ||
|
|
||
| If you have a customized session name, it is **very important** that this | ||
| constant matches it. | ||
| Session IDs are indeed used as keys to cache the generated use context hash. | ||
|
|
||
| Wrong session name will lead to unexpected results such as having the same | ||
| user context hash for every users, | ||
| or not having it cached at all (painful for performance. | ||
|
|
||
| Cleaning the Cookie Header | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| By default, the UserContextSubscriber only sets the session cookie (according to | ||
| the ``session_name_prefix`` option) in the requests to the backend. If you need | ||
| a different behaviour, overwrite ``UserContextSubscriber::cleanupHashLookupRequest`` | ||
| with your own logic. | ||
|
|
||
| .. _HttpCache: http://symfony.com/doc/current/book/http_cache.html#symfony-reverse-proxy |
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,92 @@ | ||
| <?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\Event; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\Response; | ||
| use Symfony\Component\HttpKernel\HttpCache\HttpCache; | ||
|
|
||
| /** | ||
| * Event raised by the HttpCache kernel. | ||
| * | ||
| * @author David Buchmann <[email protected]> | ||
| */ | ||
| class CacheEvent extends Event | ||
| { | ||
| /** | ||
| * @var HttpCache | ||
| */ | ||
| private $kernel; | ||
|
|
||
| /** | ||
| * @var Request | ||
| */ | ||
| private $request; | ||
|
|
||
| /** | ||
| * @var Response | ||
| */ | ||
| private $response; | ||
|
|
||
| /** | ||
| * @param HttpCache $kernel The kernel raising with this event. | ||
| * @param Request $request The request being processed. | ||
| */ | ||
| public function __construct(HttpCache $kernel, Request $request) | ||
| { | ||
| $this->kernel = $kernel; | ||
| $this->request = $request; | ||
| } | ||
|
|
||
| /** | ||
| * Get the cache kernel that raised this event. | ||
| * | ||
| * @return HttpCache | ||
| */ | ||
| public function getKernel() | ||
| { | ||
| return $this->kernel; | ||
| } | ||
|
|
||
| /** | ||
| * Get the request that is being processed. | ||
| * | ||
| * @return Request | ||
| */ | ||
| public function getRequest() | ||
| { | ||
| return $this->request; | ||
| } | ||
|
|
||
| /** | ||
| * @return Response|null The response if one was set. | ||
| */ | ||
| public function getResponse() | ||
| { | ||
| return $this->response; | ||
| } | ||
|
|
||
| /** | ||
| * Sets a response to use instead of continuing to handle this request. | ||
| * | ||
| * Setting a response stops propagation of the event to further event handlers. | ||
| * | ||
| * @param Response $response | ||
| */ | ||
| public function setResponse(Response $response) | ||
| { | ||
| $this->response = $response; | ||
|
|
||
| $this->stopPropagation(); | ||
| } | ||
| } |
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,81 @@ | ||
| <?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\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\HttpKernel\HttpKernelInterface; | ||
|
|
||
| /** | ||
| * Base class for enhanced Symfony reverse proxy based on the symfony component. | ||
| * | ||
| * <b>When using FOSHttpCacheBundle, look at FOS\HttpCacheBundle\HttpCache instead.</b> | ||
| * | ||
| * This kernel supports event subscribers that can act on the events defined in | ||
| * FOS\HttpCache\SymfonyCache\Events and may alter the request flow. | ||
| * | ||
| * @author Jérôme Vieilledent <[email protected]> (courtesy of eZ Systems AS) | ||
| * | ||
| * {@inheritdoc} | ||
| */ | ||
| abstract class EventDispatchingHttpCache extends HttpCache | ||
| { | ||
| /** | ||
| * @var EventDispatcherInterface | ||
| */ | ||
| private $eventDispatcher; | ||
|
|
||
| /** | ||
| * Get event dispatcher | ||
| * | ||
| * @return EventDispatcherInterface | ||
| */ | ||
| public function getEventDispatcher() | ||
| { | ||
| if (null === $this->eventDispatcher) { | ||
| $this->eventDispatcher = new EventDispatcher(); | ||
| } | ||
|
|
||
| return $this->eventDispatcher; | ||
| } | ||
|
|
||
| /** | ||
| * Add subscriber | ||
| * | ||
| * @param EventSubscriberInterface $subscriber | ||
| */ | ||
| public function addSubscriber(EventSubscriberInterface $subscriber) | ||
| { | ||
| $this->getEventDispatcher()->addSubscriber($subscriber); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * Adding the Events::PRE_HANDLE event. | ||
| */ | ||
| 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 $event->getResponse(); | ||
| } | ||
| } | ||
|
|
||
| return parent::handle($request, $type, $catch); | ||
| } | ||
| } |
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,20 @@ | ||
| <?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; | ||
|
|
||
| /** | ||
| * Events used in the customized Symfony built-in reverse proxy HttpCache. | ||
| */ | ||
| final class Events | ||
| { | ||
| const PRE_HANDLE = 'fos_http_cache.pre_handle'; | ||
| } |
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.
👍