Skip to content

Fixed missing request type in event #452

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----

Expand Down
26 changes: 22 additions & 4 deletions src/SymfonyCache/CacheEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Event raised by the HttpCache kernel.
Expand All @@ -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 KernelInterface::MASTER_REQUEST)
*/
public function __construct(CacheInvalidation $kernel, Request $request, Response $response = null)
public function __construct(CacheInvalidation $kernel, Request $request, Response $response = null, $requestType = KernelInterface::MASTER_REQUEST)
{
$this->kernel = $kernel;
$this->request = $request;
$this->response = $response;
$this->requestType = $requestType;
}

/**
Expand All @@ -71,6 +79,16 @@ public function getRequest()
return $this->request;
}

/**
* Get the request type.
*
* @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.
Expand Down
16 changes: 9 additions & 7 deletions src/SymfonyCache/EventDispatchingHttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Trait for enhanced Symfony reverse proxy based on the symfony kernel component.
Expand Down Expand Up @@ -91,13 +92,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);
}

/**
Expand Down Expand Up @@ -129,16 +130,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 KernelInterface::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 = KernelInterface::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();
}
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/SymfonyCache/CacheEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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\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;
use Symfony\Component\HttpKernel\KernelInterface;

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(KernelInterface::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(KernelInterface::SUB_REQUEST, $event->getRequestType());
}
}