Skip to content

Commit 4711ba6

Browse files
committed
Adding support for user context for symfony HttpCache of the symfony/http-kernel component
1 parent 172c424 commit 4711ba6

File tree

8 files changed

+700
-2
lines changed

8 files changed

+700
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
1.2.0
5+
-----
6+
7+
* **2014-12-05** Added support for the symfony/http-kernel component reverse proxy HttpCache.
8+
49
1.1.2
510
-----
611

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"guzzle/plugin-mock": "*",
3030
"mockery/mockery": "*",
3131
"monolog/monolog": "*",
32-
"symfony/process": "~2.3"
32+
"symfony/process": "~2.3",
33+
"symfony/http-kernel": "~2.3"
3334
},
3435
"suggest": {
3536
"monolog/monolog": "For logging issues while invalidating"
@@ -42,7 +43,7 @@
4243
},
4344
"extra": {
4445
"branch-alias": {
45-
"dev-master": "1.1.x-dev"
46+
"dev-master": "1.2.x-dev"
4647
}
4748
}
4849
}

src/SymfonyCache/CacheEvent.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\SymfonyCache;
13+
14+
use Symfony\Component\EventDispatcher\Event;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\HttpCache\HttpCache as BaseHttpCache;
18+
19+
/**
20+
* Event raised by the HttpCache kernel.
21+
*
22+
* @author David Buchmann <[email protected]>
23+
*/
24+
class CacheEvent extends Event
25+
{
26+
/**
27+
* @var BaseHttpCache
28+
*/
29+
private $kernel;
30+
31+
/**
32+
* @var Request
33+
*/
34+
private $request;
35+
36+
/**
37+
* @var Response
38+
*/
39+
private $response;
40+
41+
/**
42+
* @param BaseHttpCache $kernel The kernel raising with this event.
43+
* @param Request $request The request being processed.
44+
*/
45+
public function __construct(BaseHttpCache $kernel, Request $request)
46+
{
47+
$this->kernel = $kernel;
48+
$this->request = $request;
49+
}
50+
51+
/**
52+
* Get the cache kernel that raised this event.
53+
*
54+
* @return BaseHttpCache
55+
*/
56+
public function getKernel()
57+
{
58+
return $this->kernel;
59+
}
60+
61+
/**
62+
* Get the request that is being processed.
63+
*
64+
* @return Request
65+
*/
66+
public function getRequest()
67+
{
68+
return $this->request;
69+
}
70+
71+
/**
72+
* @return Response|null The response if one was set.
73+
*/
74+
public function getResponse()
75+
{
76+
return $this->response;
77+
}
78+
79+
/**
80+
* Sets a response to use instead of continuing to handle this request.
81+
*
82+
* Setting a response stops propagation of the event to further event handlers.
83+
*
84+
* @param Response $response
85+
*/
86+
public function setResponse(Response $response)
87+
{
88+
$this->response = $response;
89+
90+
$this->stopPropagation();
91+
}
92+
}

src/SymfonyCache/Events.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\SymfonyCache;
13+
14+
/**
15+
* Events used in the customized Symfony built-in reverse proxy HttpCache.
16+
*/
17+
final class Events
18+
{
19+
const PRE_HANDLE = 'fos_http_cache.pre_handle';
20+
}

src/SymfonyCache/HttpCache.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\SymfonyCache;
13+
14+
use Symfony\Component\HttpKernel\HttpCache\HttpCache as BaseHttpCache;
15+
use Symfony\Component\EventDispatcher\EventDispatcher;
16+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpKernel\HttpKernelInterface;
20+
21+
/**
22+
* Base class for enhanced Symfony reverse proxy based on the symfony component.
23+
*
24+
* <b>When using FOSHttpCacheBundle, look at FOS\HttpCacheBundle\HttpCache instead.</b>
25+
*
26+
* This kernel supports event subscribers that can act on the events defined in
27+
* FOS\HttpCache\SymfonyCache\Events and may alter the request flow.
28+
*
29+
* @author Jérôme Vieilledent <[email protected]> (courtesy of eZ Systems AS)
30+
*
31+
* {@inheritdoc}
32+
*/
33+
abstract class HttpCache extends BaseHttpCache
34+
{
35+
/**
36+
* @var EventDispatcherInterface
37+
*/
38+
private $eventDispatcher;
39+
40+
/**
41+
* Get event dispatcher
42+
*
43+
* @return EventDispatcherInterface
44+
*/
45+
public function getEventDispatcher()
46+
{
47+
if (null === $this->eventDispatcher) {
48+
$this->eventDispatcher = new EventDispatcher();
49+
}
50+
51+
return $this->eventDispatcher;
52+
}
53+
54+
/**
55+
* Add subscriber
56+
*
57+
* @param EventSubscriberInterface $subscriber
58+
*/
59+
public function addSubscriber(EventSubscriberInterface $subscriber)
60+
{
61+
$this->getEventDispatcher()->addSubscriber($subscriber);
62+
}
63+
64+
/**
65+
* {@inheritDoc}
66+
*
67+
* Adding the Events::PRE_HANDLE event.
68+
*/
69+
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
70+
{
71+
if ($this->getEventDispatcher()->hasListeners(Events::PRE_HANDLE)) {
72+
$event = new CacheEvent($this, $request);
73+
$this->getEventDispatcher()->dispatch(Events::PRE_HANDLE, $event);
74+
if ($event->getResponse()) {
75+
return $event->getResponse();
76+
}
77+
}
78+
79+
return parent::handle($request, $type, $catch);
80+
}
81+
}

0 commit comments

Comments
 (0)