mirror of
https://github.com/moodle/moodle.git
synced 2025-03-29 03:52:33 +01:00
This commit adds the PSR-20 ClockInterface to core, with a moodle-specific extension to the Interface at `\core\clock`, and a standard clock at `\core\system_clock`. Further clocks are provided as `\incrementing_clock` and `\frozen_clock` which are available to unit tests using: - `$this->mock_clock_with_incrementing(?int $starttime = null);` - `$this->mock_clock_with_frozen(?int $time = null);` For the incrementing clock, every call to fetch the time will bump the current time by one second. For the frozen clock the time will not change, but can be modified with: - `$clock->set_to(int $time);`; and - `$clock->bump(int $seconds = 1);`
PSR Clock
This repository holds the interface for PSR-20.
Note that this is not a clock of its own. It is merely an interface that describes a clock. See the specification for more details.
Installation
composer require psr/clock
Usage
If you need a clock, you can use the interface like this:
<?php
use Psr\Clock\ClockInterface;
class Foo
{
private ClockInterface $clock;
public function __construct(ClockInterface $clock)
{
$this->clock = $clock;
}
public function doSomething()
{
/** @var DateTimeImmutable $currentDateAndTime */
$currentDateAndTime = $this->clock->now();
// do something useful with that information
}
}
You can then pick one of the implementations of the interface to get a clock.
If you want to implement the interface, you can require this package and
implement Psr\Clock\ClockInterface
in your code.
Don't forget to add psr/clock-implementation
to your composer.json
s provide
-section like this:
{
"provide": {
"psr/clock-implementation": "1.0"
}
}
And please read the specification text for details on the interface.