Skip to content

Commit eb1a2c9

Browse files
authored
add request type (#453)
* Added missing request type in event
1 parent 8676d82 commit eb1a2c9

File tree

4 files changed

+90
-11
lines changed

4 files changed

+90
-11
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Changelog
33

44
See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpCache/releases).
55

6+
2.7.0
7+
-----
8+
9+
### Symfony HttpCache
10+
11+
* Added request type to the CacheEvent.
12+
613
2.6.0
714
-----
815

src/SymfonyCache/CacheEvent.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\EventDispatcher\Event;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
1718

1819
/**
1920
* Event raised by the HttpCache kernel.
@@ -37,18 +38,25 @@ class CacheEvent extends Event
3738
*/
3839
private $response;
3940

41+
/**
42+
* @var int
43+
*/
44+
private $requestType;
45+
4046
/**
4147
* Make sure your $kernel implements CacheInvalidationInterface.
4248
*
43-
* @param CacheInvalidation $kernel the kernel raising with this event
44-
* @param Request $request the request being processed
45-
* @param Response $response the response, if available
49+
* @param CacheInvalidation $kernel the kernel raising with this event
50+
* @param Request $request the request being processed
51+
* @param Response $response the response, if available
52+
* @param int $requestType the request type (default HttpKernelInterface::MASTER_REQUEST)
4653
*/
47-
public function __construct(CacheInvalidation $kernel, Request $request, Response $response = null)
54+
public function __construct(CacheInvalidation $kernel, Request $request, Response $response = null, $requestType = HttpKernelInterface::MASTER_REQUEST)
4855
{
4956
$this->kernel = $kernel;
5057
$this->request = $request;
5158
$this->response = $response;
59+
$this->requestType = $requestType;
5260
}
5361

5462
/**
@@ -71,6 +79,16 @@ public function getRequest()
7179
return $this->request;
7280
}
7381

82+
/**
83+
* One of the constants HttpKernelInterface::MASTER_REQUEST or SUB_REQUEST.
84+
*
85+
* @return int
86+
*/
87+
public function getRequestType()
88+
{
89+
return $this->requestType;
90+
}
91+
7492
/**
7593
* Events that occur after the response is created provide the default response.
7694
* Event listeners can also set the response to make it available here.

src/SymfonyCache/EventDispatchingHttpCache.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
9191
// trigger loading the CacheEvent to avoid fatal error when HttpKernel::loadClassCache is used.
9292
class_exists(CacheEvent::class);
9393

94-
if ($response = $this->dispatch(Events::PRE_HANDLE, $request)) {
95-
return $this->dispatch(Events::POST_HANDLE, $request, $response);
94+
if ($response = $this->dispatch(Events::PRE_HANDLE, $request, null, $type)) {
95+
return $this->dispatch(Events::POST_HANDLE, $request, $response, $type);
9696
}
9797

9898
$response = parent::handle($request, $type, $catch);
9999

100-
return $this->dispatch(Events::POST_HANDLE, $request, $response);
100+
return $this->dispatch(Events::POST_HANDLE, $request, $response, $type);
101101
}
102102

103103
/**
@@ -129,16 +129,17 @@ protected function invalidate(Request $request, $catch = false)
129129
/**
130130
* Dispatch an event if needed.
131131
*
132-
* @param string $name Name of the event to trigger. One of the constants in FOS\HttpCache\SymfonyCache\Events
132+
* @param string $name Name of the event to trigger. One of the constants in FOS\HttpCache\SymfonyCache\Events
133133
* @param Request $request
134-
* @param Response|null $response If already available
134+
* @param Response|null $response If already available
135+
* @param int $requestType The request type (default HttpKernelInterface::MASTER_REQUEST)
135136
*
136137
* @return Response The response to return, which might be provided/altered by a listener
137138
*/
138-
protected function dispatch($name, Request $request, Response $response = null)
139+
protected function dispatch($name, Request $request, Response $response = null, $requestType = HttpKernelInterface::MASTER_REQUEST)
139140
{
140141
if ($this->getEventDispatcher()->hasListeners($name)) {
141-
$event = new CacheEvent($this, $request, $response);
142+
$event = new CacheEvent($this, $request, $response, $requestType);
142143
$this->getEventDispatcher()->dispatch($name, $event);
143144
$response = $event->getResponse();
144145
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Tests\Unit\SymfonyCache;
13+
14+
use FOS\HttpCache\SymfonyCache\CacheEvent;
15+
use FOS\HttpCache\SymfonyCache\CacheInvalidation;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Response;
19+
use Symfony\Component\HttpKernel\HttpKernelInterface;
20+
21+
class CacheEventTest extends TestCase
22+
{
23+
/**
24+
* @var CacheInvalidation|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $kernel;
27+
28+
public function setUp()
29+
{
30+
$this->kernel = $this->createMock(CacheInvalidation::class);
31+
}
32+
33+
public function testEventGetters()
34+
{
35+
$request = Request::create('/');
36+
37+
$event = new CacheEvent($this->kernel, $request);
38+
39+
$this->assertSame($this->kernel, $event->getKernel());
40+
$this->assertSame($request, $event->getRequest());
41+
$this->assertNull($event->getResponse());
42+
$this->assertSame(HttpKernelInterface::MASTER_REQUEST, $event->getRequestType());
43+
44+
$response = new Response();
45+
46+
$event = new CacheEvent($this->kernel, $request, $response, HttpKernelInterface::SUB_REQUEST);
47+
48+
$this->assertSame($this->kernel, $event->getKernel());
49+
$this->assertSame($request, $event->getRequest());
50+
$this->assertSame($response, $event->getResponse());
51+
$this->assertSame(HttpKernelInterface::SUB_REQUEST, $event->getRequestType());
52+
}
53+
}

0 commit comments

Comments
 (0)