Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ final class PostsLastModifiedDeterminator implements LastModifiedDeterminator
private readonly BlogPostRepository $blogPostRepository,
) {

public function getLastModified(Request $request): ?\DateTime
public function getLastModified(Request $request): ?\DateTimeInterface
{
$post = $this->blogPostRepository->findLatest();

Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/WebfactoryHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

/**
* @internal
* @psalm-internal Webfactory\HttpCacheBundle
*/
class WebfactoryHttpCacheExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
Expand Down
14 changes: 11 additions & 3 deletions src/NotModified/Attribute/ReplaceWithNotModifiedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace Webfactory\HttpCacheBundle\NotModified\Attribute;

use Attribute;
use DateTime;
use DateTimeInterface;
use RuntimeException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -27,14 +27,18 @@ final class ReplaceWithNotModifiedResponse
/** @var LastModifiedDeterminator[] */
private array $lastModifiedDeterminators;
private ContainerInterface $container;
private ?DateTime $lastModified = null;
private ?DateTimeInterface $lastModified = null;

public function __construct(
private readonly array $parameters,
) {
}

public function determineLastModified(Request $request): ?DateTime
/**
* @internal
* @psalm-internal Webfactory\HttpCacheBundle
*/
public function determineLastModified(Request $request): ?DateTimeInterface
{
$this->initialiseLastModifiedDeterminators();

Expand All @@ -48,6 +52,10 @@ public function determineLastModified(Request $request): ?DateTime
return $this->lastModified;
}

/**
* @internal
* @psalm-internal Webfactory\HttpCacheBundle
*/
public function setContainer(ContainerInterface $container): void
{
$this->container = $container;
Expand Down
3 changes: 3 additions & 0 deletions src/NotModified/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
* Symfony EventListener for adding a "last modified" header to the response on the one hand. On the other hand, it
* replaces the execution of a controller action with a Not Modified HTTP response, if no newer "last modified" date is
* determined than the one in the header of a subsequent request.
*
* @internal
* @psalm-internal Webfactory\HttpCacheBundle
*/
final class EventListener
{
Expand Down
4 changes: 2 additions & 2 deletions src/NotModified/LastModifiedDeterminator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Webfactory\HttpCacheBundle\NotModified;

use DateTime;
use DateTimeInterface;
use Symfony\Component\HttpFoundation\Request;

/**
Expand All @@ -22,7 +22,7 @@
interface LastModifiedDeterminator
{
/**
* @return DateTime|null
* @return ?DateTimeInterface
*/
public function getLastModified(Request $request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Webfactory\HttpCacheBundle\Tests\NotModified\Attribute;

use DateTime;
use DateTimeInterface;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -103,14 +104,14 @@ final class FakeLastModifiedDeterminatorWithoutInterface

final class MyLastModifedDeterminator implements LastModifiedDeterminator
{
private DateTime $lastModified;
private ?DateTimeInterface $lastModified;

public function __construct(?DateTime $lastModified = null)
public function __construct(?DateTimeInterface $lastModified = null)
{
$this->lastModified = $lastModified ?: DateTime::createFromFormat('U', time());
}

public function getLastModified(Request $request): DateTime
public function getLastModified(Request $request): ?DateTimeInterface
{
return $this->lastModified;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/NotModified/EventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Closure;
use DateTime;
use DateTimeInterface;
use Error;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -269,23 +270,23 @@ public static function actionWithMoreThanOneAttribute(): Response

final class AbstainingLastModifiedDeterminator implements LastModifiedDeterminator
{
public function getLastModified(Request $request): ?DateTime
public function getLastModified(Request $request): ?DateTimeInterface
{
return null;
}
}

final class OneDayAgoModifiedLastModifiedDeterminator implements LastModifiedDeterminator
{
public function getLastModified(Request $request): DateTime
public function getLastModified(Request $request): ?DateTimeInterface
{
return DateTime::createFromFormat('U', time() - 86400);
}
}

final class FixedDateAgoModifiedLastModifiedDeterminator implements LastModifiedDeterminator
{
public function getLastModified(Request $request): DateTime
public function getLastModified(Request $request): ?DateTimeInterface
{
return new DateTime('2000-01-01');
}
Expand Down
Loading