Skip to content

Ban DateTime from HttpFoundation component #47802

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
wants to merge 1 commit into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function setChunkSize(int $chunkSize): static
*/
public function setAutoLastModified(): static
{
$this->setLastModified(\DateTime::createFromFormat('U', $this->file->getMTime()));
$this->setLastModified(\DateTimeImmutable::createFromFormat('U', $this->file->getMTime()));

return $this;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/HttpFoundation/HeaderBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ public function getDate(string $key, \DateTime $default = null): ?\DateTimeInter
return $date;
}

/**
* Returns the HTTP header value converted to a immutable date.
*
* @throws \RuntimeException When the HTTP header is not parseable
*/
public function getImmutableDate(string $key, \DateTimeImmutable $default = null): ?\DateTimeImmutable
{
if (null === $value = $this->get($key)) {
return $default;
}

if (false === $date = \DateTimeImmutable::createFromFormat(\DATE_RFC2822, $value)) {
throw new \RuntimeException(sprintf('The "%s" HTTP header is not parseable (%s).', $key, $value));
}

return $date;
}

/**
* Adds a custom Cache-Control directive.
*/
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,20 @@ public function getExpires(): ?\DateTimeInterface
}
}

/**
* Returns the value of the Expires headers as a DateTimeImmutable instance
*
* @final
*/
public function getExpiresImmutable(): ?\DateTimeImmutable
{
try {
return $this->headers->getImmutableDate('Expires');
} catch (\RuntimeException) {
return \DateTimeImmutable::createFromFormat('U', time() - 172800);
}
}

/**
* Sets the Expires HTTP header with a DateTime instance.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/HeaderBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public function testGetDate()
$this->assertInstanceOf(\DateTime::class, $headerDate);
}

public function testGetImmutableDate()
{
$bag = new HeaderBag(['foo' => 'Tue, 4 Sep 2012 20:00:00 +0200']);
$headerDate = $bag->getImmutableDate('foo');
$this->assertInstanceOf(\DateTimeImmutable::class, $headerDate);
}

public function testGetDateNull()
{
$bag = new HeaderBag(['foo' => null]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ public function testSetExpires()
$response->setExpires($now);

$this->assertEquals($response->getExpires()->getTimestamp(), $now->getTimestamp());
$this->assertInstanceOf(\DateTimeImmutable::class, $response->getExpiresImmutable());
}

public function testSetExpiresWithImmutable()
Expand Down