Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitab6b038

Browse files
[Clock] AddClock class andnow() function
1 parentbd6219d commitab6b038

File tree

9 files changed

+248
-2
lines changed

9 files changed

+248
-2
lines changed

‎composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@
187187
},
188188
"autoload-dev": {
189189
"files": [
190+
"src/Symfony/Component/Clock/Resources/now.php",
190191
"src/Symfony/Component/VarDumper/Resources/functions/dump.php"
191192
]
192193
},

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/services.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
usePsr\EventDispatcher\EventDispatcherInterfaceasPsrEventDispatcherInterface;
1616
useSymfony\Bundle\FrameworkBundle\CacheWarmer\ConfigBuilderCacheWarmer;
1717
useSymfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
18+
useSymfony\Component\Clock\Clock;
1819
useSymfony\Component\Clock\ClockInterface;
19-
useSymfony\Component\Clock\NativeClock;
2020
useSymfony\Component\Config\Loader\LoaderInterface;
2121
useSymfony\Component\Config\Resource\SelfCheckingResourceChecker;
2222
useSymfony\Component\Config\ResourceCheckerConfigCacheFactory;
@@ -229,7 +229,7 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
229229
->args([service(KernelInterface::class),service('logger')->nullOnInvalid()])
230230
->tag('kernel.cache_warmer')
231231

232-
->set('clock',NativeClock::class)
232+
->set('clock',Clock::class)
233233
->alias(ClockInterface::class,'clock')
234234
->alias(PsrClockInterface::class,'clock')
235235

‎src/Symfony/Bundle/FrameworkBundle/composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"phpdocumentor/type-resolver":"<1.4.0",
7777
"phpunit/phpunit":"<5.4.3",
7878
"symfony/asset":"<5.4",
79+
"symfony/clock":"<6.3",
7980
"symfony/console":"<5.4",
8081
"symfony/dotenv":"<5.4",
8182
"symfony/dom-crawler":"<5.4",

‎src/Symfony/Component/Clock/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add`ClockAwareTrait` to help write time-sensitive classes
8+
* Add`Clock` class and`now()` function
89

910
6.2
1011
---
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Clock;
13+
14+
usePsr\Clock\ClockInterfaceasPsrClockInterface;
15+
16+
/**
17+
* A global clock.
18+
*
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
*/
21+
finalclass Clockimplements ClockInterface
22+
{
23+
privatestaticClockInterface$globalClock;
24+
25+
publicfunction__construct(
26+
private ?PsrClockInterface$clock =null,
27+
private ?\DateTimeZone$timezone =null,
28+
) {
29+
}
30+
31+
/**
32+
* Returns the current global clock.
33+
*
34+
* Note that you should prefer injecting a ClockInterface or using
35+
* ClockAwareTrait when possible instead of using this method.
36+
*/
37+
publicstaticfunctionget():ClockInterface
38+
{
39+
returnself::$globalClock ??=newNativeClock();
40+
}
41+
42+
publicstaticfunctionset(PsrClockInterface$clock):void
43+
{
44+
self::$globalClock =$clockinstanceof ClockInterface ?$clock :newself($clock);
45+
}
46+
47+
publicfunctionnow():\DateTimeImmutable
48+
{
49+
$now = ($this->clock ??self::$globalClock)->now();
50+
51+
returnisset($this->timezone) ?$now->setTimezone($this->timezone) :$now;
52+
}
53+
54+
publicfunctionsleep(float|int$seconds):void
55+
{
56+
$clock =$this->clock ??self::$globalClock;
57+
58+
if ($clockinstanceof ClockInterface) {
59+
$clock->sleep($seconds);
60+
}else {
61+
(newNativeClock())->sleep($seconds);
62+
}
63+
}
64+
65+
publicfunctionwithTimeZone(\DateTimeZone|string$timezone):static
66+
{
67+
$clone =clone$this;
68+
$clone->timezone =\is_string($timezone) ?new \DateTimeZone($timezone) :$timezone;
69+
70+
return$clone;
71+
}
72+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Clock;
13+
14+
/**
15+
* Returns the current time as a DateTimeImmutable.
16+
*
17+
* Note that you should prefer injecting a ClockInterface or using
18+
* ClockAwareTrait when possible instead of using this function.
19+
*/
20+
functionnow():\DateTimeImmutable
21+
{
22+
return Clock::get()->now();
23+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Clock\Test;
13+
14+
usePsr\Clock\ClockInterface;
15+
useSymfony\Component\Clock\Clock;
16+
useSymfony\Component\Clock\MockClock;
17+
18+
usefunctionSymfony\Component\Clock\now;
19+
20+
trait ClockSensitiveTrait
21+
{
22+
publicstaticfunctionmockTime(string|\DateTimeImmutable|ClockInterface|bool$when =true):ClockInterface
23+
{
24+
Clock::set(match (true) {
25+
false ===$when =>self::saveClockBeforeTest(false),
26+
true ===$when =>newMockClock(),
27+
$wheninstanceof ClockInterface =>$when,
28+
$wheninstanceof \DateTimeImmutable =>newMockClock($when),
29+
default =>newMockClock(now()->modify($when)),
30+
});
31+
32+
return Clock::get();
33+
}
34+
35+
/**
36+
* @before
37+
*
38+
* @internal
39+
*/
40+
protectedstaticfunctionsaveClockBeforeTest(bool$save =true):ClockInterface
41+
{
42+
static$originalClock;
43+
44+
return$save ?$originalClock = Clock::get() :$originalClock;
45+
}
46+
47+
/**
48+
* @after
49+
*
50+
* @internal
51+
*/
52+
protectedstaticfunctionrestoreClockAfterTest():void
53+
{
54+
Clock::set(self::saveClockBeforeTest(false));
55+
}
56+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\Clock\Tests;
13+
14+
usePHPUnit\Framework\TestCase;
15+
usePsr\Clock\ClockInterface;
16+
useSymfony\Component\Clock\Clock;
17+
useSymfony\Component\Clock\MockClock;
18+
useSymfony\Component\Clock\NativeClock;
19+
useSymfony\Component\Clock\Test\ClockSensitiveTrait;
20+
21+
usefunctionSymfony\Component\Clock\now;
22+
23+
class ClockTestextends TestCase
24+
{
25+
use ClockSensitiveTrait;
26+
27+
publicfunctiontestMockClock()
28+
{
29+
$this->assertInstanceOf(NativeClock::class, Clock::get());
30+
31+
$clock =self::mockTime();
32+
$this->assertInstanceOf(MockClock::class, Clock::get());
33+
$this->assertSame(Clock::get(),$clock);
34+
}
35+
36+
publicfunctiontestNativeClock()
37+
{
38+
$this->assertInstanceOf(\DateTimeImmutable::class,now());
39+
$this->assertInstanceOf(NativeClock::class, Clock::get());
40+
}
41+
42+
publicfunctiontestMockClockDisable()
43+
{
44+
$this->assertInstanceOf(NativeClock::class, Clock::get());
45+
46+
$this->assertInstanceOf(MockClock::class,self::mockTime(true));
47+
$this->assertInstanceOf(NativeClock::class,self::mockTime(false));
48+
}
49+
50+
publicfunctiontestMockClockFreeze()
51+
{
52+
self::mockTime(new \DateTimeImmutable('2021-12-19'));
53+
54+
$this->assertSame('2021-12-19',now()->format('Y-m-d'));
55+
56+
self::mockTime('+1 days');
57+
$this->assertSame('2021-12-20',now()->format('Y-m-d'));
58+
}
59+
60+
publicfunctiontestMockClockWithClock()
61+
{
62+
$mockClock =newMockClock();
63+
64+
self::mockTime($mockClock);
65+
66+
$this->assertSame($mockClock, Clock::get());
67+
}
68+
69+
publicfunctiontestPsrClock()
70+
{
71+
$psrClock =newclass()implements ClockInterface {
72+
publicfunctionnow():\DateTimeImmutable
73+
{
74+
returnnew \DateTimeImmutable('@1234567');
75+
}
76+
};
77+
78+
Clock::set($psrClock);
79+
80+
$this->assertInstanceOf(Clock::class, Clock::get());
81+
82+
$this->assertSame(1234567,now()->getTimestamp());
83+
84+
$this->assertSame('UTC', Clock::get()->withTimeZone('UTC')->now()->getTimezone()->getName());
85+
$this->assertSame('Europe/Paris', Clock::get()->withTimeZone('Europe/Paris')->now()->getTimezone()->getName());
86+
87+
Clock::get()->sleep(0.1);
88+
89+
$this->assertSame(1234567,now()->getTimestamp());
90+
}
91+
}

‎src/Symfony/Component/Clock/composer.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"psr/clock":"^1.0"
2424
},
2525
"autoload": {
26+
"files": ["Resources/now.php" ],
2627
"psr-4": {"Symfony\\Component\\Clock\\":"" },
2728
"exclude-from-classmap": [
2829
"/Tests/"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp