-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Clock] Add Clock::now(), get() and with()
- Loading branch information
1 parent
bd6219d
commit 73474b7
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Clock; | ||
|
||
/** | ||
* A global clock. | ||
* | ||
* Defined as enum to be final and non-instantiable. | ||
*/ | ||
enum Clock | ||
{ | ||
public static function now(): \DateTimeImmutable | ||
{ | ||
return self::clock()->now(); | ||
} | ||
|
||
public static function get(): ClockInterface | ||
{ | ||
return self::clock(); | ||
} | ||
|
||
public static function with(ClockInterface $clock, callable $callback, ...$arguments): mixed | ||
{ | ||
$prevClock = self::clock($clock); | ||
|
||
try { | ||
return $callback(...$arguments); | ||
} finally { | ||
self::clock($prevClock); | ||
} | ||
} | ||
|
||
private static function clock(ClockInterface $newClock = null): ClockInterface | ||
{ | ||
static $clock; | ||
|
||
if (!$newClock) { | ||
return $clock ??= new NativeClock(); | ||
} | ||
|
||
$prevClock = $clock ?? new NativeClock(); | ||
$clock = $newClock; | ||
|
||
return $prevClock; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Clock\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Clock\Clock; | ||
use Symfony\Component\Clock\MockClock; | ||
use Symfony\Component\Clock\NativeClock; | ||
|
||
class ClockTest extends TestCase | ||
{ | ||
public function testClock() | ||
{ | ||
$this->assertInstanceOf(\DateTimeImmutable::class, Clock::now()); | ||
$this->assertInstanceOf(NativeClock::class, Clock::get()); | ||
|
||
$clock = new MockClock(); | ||
|
||
$this->assertSame([123, $clock], Clock::with($clock, fn ($arg) => [$arg, Clock::get()], 123)); | ||
$this->assertInstanceOf(NativeClock::class, Clock::get()); | ||
} | ||
} |