diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e33b1a4..d6504342 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ Changelog See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpCache/releases). +2.7.0 +----- + +### Symfony HttpCache + +* Added request type to the CacheEvent. + 2.6.0 ----- diff --git a/src/SymfonyCache/CacheEvent.php b/src/SymfonyCache/CacheEvent.php index 4474519c..242371fe 100644 --- a/src/SymfonyCache/CacheEvent.php +++ b/src/SymfonyCache/CacheEvent.php @@ -14,6 +14,7 @@ use Symfony\Component\EventDispatcher\Event; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\HttpKernelInterface; /** * Event raised by the HttpCache kernel. @@ -37,18 +38,25 @@ class CacheEvent extends Event */ private $response; + /** + * @var int + */ + private $requestType; + /** * Make sure your $kernel implements CacheInvalidationInterface. * - * @param CacheInvalidation $kernel the kernel raising with this event - * @param Request $request the request being processed - * @param Response $response the response, if available + * @param CacheInvalidation $kernel the kernel raising with this event + * @param Request $request the request being processed + * @param Response $response the response, if available + * @param int $requestType the request type (default HttpKernelInterface::MASTER_REQUEST) */ - public function __construct(CacheInvalidation $kernel, Request $request, Response $response = null) + public function __construct(CacheInvalidation $kernel, Request $request, Response $response = null, $requestType = HttpKernelInterface::MASTER_REQUEST) { $this->kernel = $kernel; $this->request = $request; $this->response = $response; + $this->requestType = $requestType; } /** @@ -71,6 +79,16 @@ public function getRequest() return $this->request; } + /** + * One of the constants HttpKernelInterface::MASTER_REQUEST or SUB_REQUEST. + * + * @return int + */ + public function getRequestType() + { + return $this->requestType; + } + /** * Events that occur after the response is created provide the default response. * Event listeners can also set the response to make it available here. diff --git a/src/SymfonyCache/EventDispatchingHttpCache.php b/src/SymfonyCache/EventDispatchingHttpCache.php index d329e93c..ad8c95fd 100644 --- a/src/SymfonyCache/EventDispatchingHttpCache.php +++ b/src/SymfonyCache/EventDispatchingHttpCache.php @@ -91,13 +91,13 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ // trigger loading the CacheEvent to avoid fatal error when HttpKernel::loadClassCache is used. class_exists(CacheEvent::class); - if ($response = $this->dispatch(Events::PRE_HANDLE, $request)) { - return $this->dispatch(Events::POST_HANDLE, $request, $response); + if ($response = $this->dispatch(Events::PRE_HANDLE, $request, null, $type)) { + return $this->dispatch(Events::POST_HANDLE, $request, $response, $type); } $response = parent::handle($request, $type, $catch); - return $this->dispatch(Events::POST_HANDLE, $request, $response); + return $this->dispatch(Events::POST_HANDLE, $request, $response, $type); } /** @@ -129,16 +129,17 @@ protected function invalidate(Request $request, $catch = false) /** * Dispatch an event if needed. * - * @param string $name Name of the event to trigger. One of the constants in FOS\HttpCache\SymfonyCache\Events + * @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 + * @param Response|null $response If already available + * @param int $requestType The request type (default HttpKernelInterface::MASTER_REQUEST) * * @return Response The response to return, which might be provided/altered by a listener */ - protected function dispatch($name, Request $request, Response $response = null) + protected function dispatch($name, Request $request, Response $response = null, $requestType = HttpKernelInterface::MASTER_REQUEST) { if ($this->getEventDispatcher()->hasListeners($name)) { - $event = new CacheEvent($this, $request, $response); + $event = new CacheEvent($this, $request, $response, $requestType); $this->getEventDispatcher()->dispatch($name, $event); $response = $event->getResponse(); } diff --git a/tests/Unit/SymfonyCache/CacheEventTest.php b/tests/Unit/SymfonyCache/CacheEventTest.php new file mode 100644 index 00000000..c41bf28d --- /dev/null +++ b/tests/Unit/SymfonyCache/CacheEventTest.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\HttpCache\Tests\Unit\SymfonyCache; + +use FOS\HttpCache\SymfonyCache\CacheEvent; +use FOS\HttpCache\SymfonyCache\CacheInvalidation; +use PHPUnit\Framework\TestCase; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\HttpKernelInterface; + +class CacheEventTest extends TestCase +{ + /** + * @var CacheInvalidation|\PHPUnit_Framework_MockObject_MockObject + */ + private $kernel; + + public function setUp() + { + $this->kernel = $this->createMock(CacheInvalidation::class); + } + + public function testEventGetters() + { + $request = Request::create('/'); + + $event = new CacheEvent($this->kernel, $request); + + $this->assertSame($this->kernel, $event->getKernel()); + $this->assertSame($request, $event->getRequest()); + $this->assertNull($event->getResponse()); + $this->assertSame(HttpKernelInterface::MASTER_REQUEST, $event->getRequestType()); + + $response = new Response(); + + $event = new CacheEvent($this->kernel, $request, $response, HttpKernelInterface::SUB_REQUEST); + + $this->assertSame($this->kernel, $event->getKernel()); + $this->assertSame($request, $event->getRequest()); + $this->assertSame($response, $event->getResponse()); + $this->assertSame(HttpKernelInterface::SUB_REQUEST, $event->getRequestType()); + } +}