Skip to content

Commit 80315be

Browse files
ddeboerdbu
authored andcommitted
Provide Symfony HttpCache as trait
1 parent 33db556 commit 80315be

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

src/SymfonyCache/EventDispatchingHttpCache.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@
1111

1212
namespace FOS\HttpCache\SymfonyCache;
1313

14-
use Symfony\Component\HttpFoundation\Response;
15-
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
1614
use Symfony\Component\EventDispatcher\EventDispatcher;
1715
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1816
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1917
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Response;
2019
use Symfony\Component\HttpKernel\HttpKernelInterface;
2120

2221
/**
23-
* Base class for enhanced Symfony reverse proxy based on the symfony component.
22+
* Trait for enhanced Symfony reverse proxy based on the symfony component.
2423
*
2524
* <b>When using FOSHttpCacheBundle, look at FOS\HttpCacheBundle\HttpCache instead.</b>
2625
*
@@ -31,7 +30,7 @@
3130
*
3231
* {@inheritdoc}
3332
*/
34-
abstract class EventDispatchingHttpCache extends HttpCache implements CacheInvalidationInterface
33+
trait EventDispatchingHttpCache
3534
{
3635
/**
3736
* @var EventDispatcherInterface

tests/Functional/Fixtures/Symfony/AppCache.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace FOS\HttpCache\Tests\Functional\Fixtures\Symfony;
44

5+
use FOS\HttpCache\SymfonyCache\CacheInvalidationInterface;
56
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
67
use Symfony\Component\HttpFoundation\Request;
8+
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
79
use Symfony\Component\HttpKernel\HttpKernelInterface;
810

9-
class AppCache extends EventDispatchingHttpCache
11+
class AppCache extends HttpCache implements CacheInvalidationInterface
1012
{
13+
use EventDispatchingHttpCache;
14+
1115
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
1216
{
1317
$response = parent::handle($request, $type, $catch);
@@ -25,4 +29,14 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
2529

2630
return $response;
2731
}
32+
33+
/**
34+
* Made public to allow event subscribers to do refresh operations.
35+
*
36+
* {@inheritDoc}
37+
*/
38+
public function fetch(Request $request, $catch = false)
39+
{
40+
return parent::fetch($request, $catch);
41+
}
2842
}

tests/Unit/SymfonyCache/EventDispatchingHttpCacheTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
namespace FOS\HttpCache\Tests\Unit\SymfonyCache;
1313

14+
use FOS\HttpCache\SymfonyCache\CacheInvalidationInterface;
1415
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
1516
use FOS\HttpCache\SymfonyCache\CacheEvent;
1617
use FOS\HttpCache\SymfonyCache\Events;
1718
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1819
use Symfony\Component\HttpFoundation\Request;
1920
use Symfony\Component\HttpFoundation\Response;
21+
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
2022
use Symfony\Component\HttpKernel\HttpKernelInterface;
2123

2224
class EventDispatchingHttpCacheTest extends \PHPUnit_Framework_TestCase
@@ -27,7 +29,7 @@ class EventDispatchingHttpCacheTest extends \PHPUnit_Framework_TestCase
2729
protected function getHttpCachePartialMock(array $mockedMethods = null)
2830
{
2931
$mock = $this
30-
->getMockBuilder('\FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache')
32+
->getMockBuilder('\FOS\HttpCache\Tests\Unit\SymfonyCache\AppCache')
3133
->setMethods( $mockedMethods )
3234
->disableOriginalConstructor()
3335
->getMock()
@@ -142,6 +144,16 @@ public function testAbortInvalidate()
142144
}
143145
}
144146

147+
class AppCache extends HttpCache implements CacheInvalidationInterface
148+
{
149+
use EventDispatchingHttpCache;
150+
151+
public function fetch(Request $request, $catch = false)
152+
{
153+
return $this->fetch($request, $catch);
154+
}
155+
}
156+
145157
class TestSubscriber implements EventSubscriberInterface
146158
{
147159
public $handleHits = 0;

0 commit comments

Comments
 (0)