-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceWithNotModifiedResponse.php
More file actions
94 lines (79 loc) · 3.45 KB
/
ReplaceWithNotModifiedResponse.php
File metadata and controls
94 lines (79 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/*
* (c) webfactory GmbH <info@webfactory.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webfactory\HttpCacheBundle\NotModified\Attribute;
use Attribute;
use DateTimeInterface;
use RuntimeException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Webfactory\HttpCacheBundle\NotModified\LastModifiedDeterminator;
/**
* This attribute determines the latest last modified date over all of its LastModifiedDeterminators. This date is used
* by the \Webfactory\HttpCacheBundle\NotModified\EventListener to possibly replace the execution of a controller with
* sending a Not Modified HTTP response.
*/
#[Attribute(Attribute::TARGET_METHOD)]
final class ReplaceWithNotModifiedResponse
{
/** @var LastModifiedDeterminator[] */
private array $lastModifiedDeterminators;
private ContainerInterface $container;
private ?DateTimeInterface $lastModified = null;
public function __construct(
private readonly array $parameters,
) {
}
/**
* @internal
* @psalm-internal Webfactory\HttpCacheBundle
*/
public function determineLastModified(Request $request): ?DateTimeInterface
{
$this->initialiseLastModifiedDeterminators();
foreach ($this->lastModifiedDeterminators as $lastModifiedDeterminator) {
$lastModifiedOfCurrentDeterminator = $lastModifiedDeterminator->getLastModified($request);
if (null === $this->lastModified || $this->lastModified < $lastModifiedOfCurrentDeterminator) {
$this->lastModified = $lastModifiedOfCurrentDeterminator;
}
}
return $this->lastModified;
}
/**
* @internal
* @psalm-internal Webfactory\HttpCacheBundle
*/
public function setContainer(ContainerInterface $container): void
{
$this->container = $container;
}
private function initialiseLastModifiedDeterminators(): void
{
if (0 === count($this->parameters)) {
throw new RuntimeException('The attribute '.get_class($this).' has to be parametrised with LastModifiedDeterminators.');
}
foreach ($this->parameters as $lastModifiedDeterminatorDescription) {
$lastModifiedDeterminator = null;
if (is_string($lastModifiedDeterminatorDescription)) {
if ('@' === $lastModifiedDeterminatorDescription[0]) {
$lastModifiedDeterminator = $this->container->get(substr($lastModifiedDeterminatorDescription, 1));
} else {
$lastModifiedDeterminator = new $lastModifiedDeterminatorDescription();
}
}
if (is_array($lastModifiedDeterminatorDescription)) {
$lastModifiedDeterminatorClass = key($lastModifiedDeterminatorDescription);
$lastModifiedDeterminatorParameter = current($lastModifiedDeterminatorDescription);
$lastModifiedDeterminator = new $lastModifiedDeterminatorClass($lastModifiedDeterminatorParameter);
}
if (!($lastModifiedDeterminator instanceof LastModifiedDeterminator)) {
throw new RuntimeException('The class "'.get_class($lastModifiedDeterminator).'" does not implement '.LastModifiedDeterminator::class.'.');
}
$this->lastModifiedDeterminators[] = $lastModifiedDeterminator;
}
}
}