-
-
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 6ef4125
Showing
3 changed files
with
90 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,58 @@ | ||
<?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; | ||
|
||
use Psr\Clock\ClockInterface; | ||
|
||
/** | ||
* A global clock. | ||
* | ||
* Note that you should prefer injecting a ClockInterface or using | ||
* ClockAwareTrait when possible instead of using this class. | ||
* | ||
* Defined as enum to be final and non-instantiable. | ||
*/ | ||
enum Clock | ||
{ | ||
public static function now(): \DateTimeImmutable | ||
{ | ||
return self::get()->now(); | ||
} | ||
|
||
public static function with(ClockInterface $clock, callable $callback, mixed ...$arguments): mixed | ||
{ | ||
$prevClock = self::get($clock); | ||
|
||
try { | ||
return $callback(...$arguments); | ||
} finally { | ||
self::get($prevClock); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the current clock, or the previous one if a new clock is passed as argument. | ||
*/ | ||
public static function get(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()); | ||
} | ||
} |