Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60fe79a

Browse files
committed
Resolve DateTime value using the clock
1 parent ff56fe8 commit 60fe79a

File tree

5 files changed

+59
-13
lines changed

5 files changed

+59
-13
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/web.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
->tag('controller.argument_value_resolver', ['priority' => 100])
5656

5757
->set('argument_resolver.datetime', DateTimeValueResolver::class)
58+
->args([
59+
service('clock')->nullOnInvalid(),
60+
])
5861
->tag('controller.argument_value_resolver', ['priority' => 100])
5962

6063
->set('argument_resolver.request_attribute', RequestAttributeValueResolver::class)

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Update `DateTimeValueResolver` to accept an instance of `ClockInterface` for the current date
8+
49
6.2
510
---
611

src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/DateTimeValueResolver.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver;
1313

14+
use Symfony\Component\Clock\ClockInterface;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpKernel\Attribute\MapDateTime;
1617
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
@@ -26,6 +27,11 @@
2627
*/
2728
final class DateTimeValueResolver implements ArgumentValueResolverInterface, ValueResolverInterface
2829
{
30+
public function __construct(
31+
private readonly ?ClockInterface $clock = null,
32+
) {
33+
}
34+
2935
/**
3036
* @deprecated since Symfony 6.2, use resolve() instead
3137
*/
@@ -53,15 +59,20 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
5359
}
5460

5561
$class = \DateTimeInterface::class === $argument->getType() ? \DateTimeImmutable::class : $argument->getType();
62+
63+
if ($this->clock && !$value) {
64+
$date = $this->clock->now();
65+
66+
return $date instanceof $class ? [$date] : [$class::createFromInterface($date)];
67+
}
68+
5669
$format = null;
5770

5871
if ($attributes = $argument->getAttributes(MapDateTime::class, ArgumentMetadata::IS_INSTANCEOF)) {
5972
$attribute = $attributes[0];
6073
$format = $attribute->format;
6174
}
6275

63-
$date = false;
64-
6576
if (null !== $format) {
6677
$date = $class::createFromFormat($format, $value);
6778

src/Symfony/Component/HttpKernel/Tests/Controller/ArgumentResolver/DateTimeValueResolverTest.php

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Clock\MockClock;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpKernel\Attribute\MapDateTime;
1718
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DateTimeValueResolver;
@@ -34,9 +35,12 @@ protected function tearDown(): void
3435

3536
public function getTimeZones()
3637
{
37-
yield ['UTC'];
38-
yield ['Etc/GMT+9'];
39-
yield ['Etc/GMT-14'];
38+
yield ['UTC', false];
39+
yield ['Etc/GMT+9', false];
40+
yield ['Etc/GMT-14', false];
41+
yield ['UTC', true];
42+
yield ['Etc/GMT+9', true];
43+
yield ['Etc/GMT-14', true];
4044
}
4145

4246
/**
@@ -71,10 +75,10 @@ public function testUnsupportedArgument()
7175
/**
7276
* @dataProvider getTimeZones
7377
*/
74-
public function testFullDate(string $timezone)
78+
public function testFullDate(string $timezone, bool $withClock)
7579
{
7680
date_default_timezone_set($timezone);
77-
$resolver = new DateTimeValueResolver();
81+
$resolver = new DateTimeValueResolver($withClock ? new MockClock() : null);
7882

7983
$argument = new ArgumentMetadata('dummy', \DateTimeImmutable::class, false, false, null);
8084
$request = self::requestWithAttributes(['dummy' => '2012-07-21 00:00:00']);
@@ -90,10 +94,10 @@ public function testFullDate(string $timezone)
9094
/**
9195
* @dataProvider getTimeZones
9296
*/
93-
public function testUnixTimestamp(string $timezone)
97+
public function testUnixTimestamp(string $timezone, bool $withClock)
9498
{
9599
date_default_timezone_set($timezone);
96-
$resolver = new DateTimeValueResolver();
100+
$resolver = new DateTimeValueResolver($withClock ? new MockClock('now', $timezone) : null);
97101

98102
$argument = new ArgumentMetadata('dummy', \DateTimeImmutable::class, false, false, null);
99103
$request = self::requestWithAttributes(['dummy' => '989541720']);
@@ -150,10 +154,10 @@ public function testCustomClass()
150154
/**
151155
* @dataProvider getTimeZones
152156
*/
153-
public function testDateTimeImmutable(string $timezone)
157+
public function testDateTimeImmutable(string $timezone, bool $withClock)
154158
{
155159
date_default_timezone_set($timezone);
156-
$resolver = new DateTimeValueResolver();
160+
$resolver = new DateTimeValueResolver($withClock ? new MockClock('now', $timezone) : null);
157161

158162
$argument = new ArgumentMetadata('dummy', \DateTimeImmutable::class, false, false, null);
159163
$request = self::requestWithAttributes(['dummy' => '2016-09-08 00:00:00 +05:00']);
@@ -169,10 +173,10 @@ public function testDateTimeImmutable(string $timezone)
169173
/**
170174
* @dataProvider getTimeZones
171175
*/
172-
public function testWithFormat(string $timezone)
176+
public function testWithFormat(string $timezone, bool $withClock)
173177
{
174178
date_default_timezone_set($timezone);
175-
$resolver = new DateTimeValueResolver();
179+
$resolver = new DateTimeValueResolver($withClock ? new MockClock('now', $timezone) : null);
176180

177181
$argument = new ArgumentMetadata('dummy', \DateTimeInterface::class, false, false, null, false, [
178182
MapDateTime::class => new MapDateTime('m-d-y H:i:s'),
@@ -218,6 +222,28 @@ public function test404Exception(ArgumentMetadata $argument, Request $request)
218222
$resolver->resolve($request, $argument);
219223
}
220224

225+
/**
226+
* @param class-string<\DateTimeInterface> $class
227+
*
228+
* @testWith ["DateTimeInterface"]
229+
* ["DateTimeImmutable"]
230+
* ["DateTime"]
231+
*/
232+
public function testNowFromClock(string $class)
233+
{
234+
$clock = new MockClock('2022-02-20 22:20:02');
235+
$resolver = new DateTimeValueResolver($clock);
236+
237+
$argument = new ArgumentMetadata('dummy', $class, false, false, null, false);
238+
$request = self::requestWithAttributes(['dummy' => null]);
239+
240+
$results = $resolver->resolve($request, $argument);
241+
242+
$this->assertCount(1, $results);
243+
$this->assertInstanceOf($class, $results[0]);
244+
$this->assertEquals($clock->now(), $results[0]);
245+
}
246+
221247
private static function requestWithAttributes(array $attributes): Request
222248
{
223249
$request = Request::create('/');

src/Symfony/Component/HttpKernel/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"require-dev": {
2828
"symfony/browser-kit": "^5.4|^6.0",
29+
"symfony/clock": "^6.2",
2930
"symfony/config": "^6.1",
3031
"symfony/console": "^5.4|^6.0",
3132
"symfony/css-selector": "^5.4|^6.0",

0 commit comments

Comments
 (0)