-
-
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.
- Loading branch information
1 parent
bd6219d
commit e2e654f
Showing
3 changed files
with
75 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,42 @@ | ||
<?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::get()->now(); | ||
} | ||
|
||
/** | ||
* Returns the current clock, or the previous one when passing a new clock as argument. | ||
*/ | ||
public static function get(ClockInterface $newClock = null): ClockInterface | ||
{ | ||
static $clock; | ||
|
||
if (null === $newClock) { | ||
return $clock ??= new NativeClock(); | ||
} | ||
|
||
$previousClock = $clock ?? new NativeClock(); | ||
$clock = $newClock; | ||
|
||
return $previousClock; | ||
} | ||
} |
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,32 @@ | ||
<?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()); | ||
|
||
$previousClock = Clock::get(); | ||
$this->assertInstanceOf(NativeClock::class, $previousClock); | ||
|
||
$newClock = new MockClock(); | ||
$this->assertSame($previousClock, Clock::get($newClock)); | ||
$this->assertSame($newClock, Clock::get()); | ||
} | ||
} |