Skip to content

Commit 57f77e8

Browse files
committed
Replace "Voter" with LastModifiedDeterminator
- removed "parameter"-key from annotation configuration - put unique advantage forefront in readme
1 parent 68195e2 commit 57f77e8

5 files changed

Lines changed: 153 additions & 211 deletions

File tree

NotModified/Annotation/ReplaceWithNotModifiedResponse.php

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
use Symfony\Component\DependencyInjection\ContainerInterface;
1313
use Symfony\Component\HttpFoundation\Request;
14-
use Webfactory\HttpCacheBundle\NotModified\VoterInterface;
14+
use Webfactory\HttpCacheBundle\NotModified\LastModifiedDeterminator;
1515

1616
/**
17-
* This Annotation determines the last modified date over all of its parameterised voters. This date is used by the
18-
* \Webfactory\HttpCacheBundle\NotModified\EventListener to possibly replace the execution of a controller with
17+
* This Annotation determines the latest last modified date over all of its LastModifiedDeterminators. This date is used
18+
* by the \Webfactory\HttpCacheBundle\NotModified\EventListener to possibly replace the execution of a controller with
1919
* sending a Not Modified HTTP response.
2020
*
2121
* @Annotation
@@ -25,8 +25,8 @@ final class ReplaceWithNotModifiedResponse
2525
/** @var array */
2626
private $parameters;
2727

28-
/** @var VoterInterface[] */
29-
private $voters;
28+
/** @var LastModifiedDeterminator[] */
29+
private $lastModifiedDeterminators;
3030

3131
/** @var ContainerInterface */
3232
private $container;
@@ -48,12 +48,12 @@ public function __construct(array $parameters)
4848
*/
4949
public function determineLastModified(Request $request)
5050
{
51-
$this->initialiseVoters();
51+
$this->initialiseLastModifiedDeterminators();
5252

53-
foreach ($this->voters as $voter) {
54-
$lastModifiedOfCurrentVoter = $voter->getLastModified($request);
55-
if ($this->lastModified === null || $this->lastModified < $lastModifiedOfCurrentVoter) {
56-
$this->lastModified = $lastModifiedOfCurrentVoter;
53+
foreach ($this->lastModifiedDeterminators as $lastModifiedDeterminator) {
54+
$lastModifiedOfCurrentDeterminator = $lastModifiedDeterminator->getLastModified($request);
55+
if ($this->lastModified === null || $this->lastModified < $lastModifiedOfCurrentDeterminator) {
56+
$this->lastModified = $lastModifiedOfCurrentDeterminator;
5757
}
5858
}
5959

@@ -68,36 +68,36 @@ public function setContainer(ContainerInterface $container)
6868
$this->container = $container;
6969
}
7070

71-
private function initialiseVoters()
71+
private function initialiseLastModifiedDeterminators()
7272
{
73-
if (!array_key_exists('voters', $this->parameters) || count($this->parameters['voters']) === 0) {
74-
throw new \RuntimeException('The annotation ' . get_class($this) . ' has to be parametrised with voters.');
73+
if (count($this->parameters['value']) === 0) {
74+
throw new \RuntimeException('The annotation ' . get_class($this) . ' has to be parametrised with LastModifiedDeterminators.');
7575
}
7676

77-
foreach ($this->parameters['voters'] as $voterDescription) {
78-
$voter = null;
77+
foreach ($this->parameters['value'] as $lastModifiedDeterminatorDescription) {
78+
$lastModifiedDeterminator = null;
7979

80-
if (is_string($voterDescription)) {
81-
if ($voterDescription[0] === '@') {
82-
$voter = $this->container->get($voterDescription);
80+
if (is_string($lastModifiedDeterminatorDescription)) {
81+
if ($lastModifiedDeterminatorDescription[0] === '@') {
82+
$lastModifiedDeterminator = $this->container->get($lastModifiedDeterminatorDescription);
8383
} else {
84-
$voter = new $voterDescription;
84+
$lastModifiedDeterminator = new $lastModifiedDeterminatorDescription;
8585
}
8686
}
8787

88-
if (is_array($voterDescription)) {
89-
$voterClass = key($voterDescription);
90-
$voterParameter = current($voterDescription);
91-
$voter = new $voterClass($voterParameter);
88+
if (is_array($lastModifiedDeterminatorDescription)) {
89+
$lastModifiedDeterminatorClass = key($lastModifiedDeterminatorDescription);
90+
$lastModifiedDeterminatorParameter = current($lastModifiedDeterminatorDescription);
91+
$lastModifiedDeterminator = new $lastModifiedDeterminatorClass($lastModifiedDeterminatorParameter);
9292
}
9393

94-
if (!($voter instanceof VoterInterface)) {
94+
if (!($lastModifiedDeterminator instanceof LastModifiedDeterminator)) {
9595
throw new \RuntimeException(
96-
'The voter class "' . get_class($voter) . '" does not implement ' . VoterInterface::class . '.'
96+
'The class "' . get_class($lastModifiedDeterminator) . '" does not implement ' . LastModifiedDeterminator::class . '.'
9797
);
9898
}
9999

100-
$this->voters[] = $voter;
100+
$this->lastModifiedDeterminators[] = $lastModifiedDeterminator;
101101
}
102102
}
103103
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
use Symfony\Component\HttpFoundation\Request;
1313

1414
/**
15-
* The ReplaceWithNotModifiedResponse method annotation is parameterised with Voters. Each Voter should determine the
16-
* last modified date of one of the various underlying resources for a response. E.g. if your controller's indexAction
17-
* builds a response containing News and Users, you can write a NewsVoter determining the date of the last published
18-
* News and a UserVoter determining the creation date of the last registered User.
15+
* The ReplaceWithNotModifiedResponse method annotation is parameterised with instances of LastModifiedDeterminators.
16+
* Each of them should determine the last modified date of one of the various underlying resources for a response.
17+
* E.g. if your controller's indexAction builds a response containing News and Users, you can write a
18+
* NewsLastModifiedDeterminator determining the date of the last published News and a UserLastModifiedDeterminator
19+
* determining the creation date of the last registered User.
1920
*/
20-
interface VoterInterface
21+
interface LastModifiedDeterminator
2122
{
2223
/**
2324
* @param Request $request

0 commit comments

Comments
 (0)