Commit 513a869
committed
feature #46715 [Clock] A new component to decouple applications from the system clock (nicolas-grekas)
This PR was squashed before being merged into the 6.2 branch.
Discussion
----------
[Clock] A new component to decouple applications from the system clock
| Q | A
| ------------- | ---
| Branch? | 6.2
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets | -
| License | MIT
| Doc PR | -
After watching @afilina's talk at SymfonyWorld Online last week + listening to her using a "clock" service to improve testability of time-sensitive logics, I decided to propose this new "Clock" component. This also relates to the ongoing efforts to standardize a `ClockInterface` in [PSR-20](https://github.com/php-fig/clock).
This PR provides a `ClockInterface`, with 3 methods:
- `now(): \DateTimeImmutable` is designed to be compatible with the envisioned PSR-20;
- `sleep(float|int $seconds): void` advances the clock by the provided number of seconds;
- `withTimeZone(): static` changes the time zone returned by `now()`.
The `sleep()` methods takes inspiration from `ClockMock` in the PhpUnitBridge, where this proved useful to improve testability. Ideally, we could use this component everywhere measuring the current time is needed and stop relying on `ClockMock`.
This PR provides 3 clock implementations:
- `NativeClock` which relies on the system clock;
- `MockClock` which allows mocking the time;
- `MonotonicClock` which relies on `hrtime()` to provide a monotonic clock.
If this gets accepted, I'll follow up with a PR to add clock services to FrameworkBundle and we'll then be able to see where we could use such clock services in other components. I hope PSR-20 will be stabilized by Symfony 6.2, but this is not required for this PR to be useful.
Commits
-------
9db08e40d4 [Clock] A new component to decouple applications from the system clockFile tree
2 files changed
+11
-3
lines changed- DependencyInjection
- Resources/config
2 files changed
+11
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
| |||
40 | 39 | | |
41 | 40 | | |
42 | 41 | | |
| 42 | + | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| |||
277 | 277 | | |
278 | 278 | | |
279 | 279 | | |
280 | | - | |
281 | | - | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
282 | 283 | | |
283 | 284 | | |
284 | 285 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
| 17 | + | |
| 18 | + | |
16 | 19 | | |
17 | 20 | | |
18 | 21 | | |
| |||
77 | 80 | | |
78 | 81 | | |
79 | 82 | | |
| 83 | + | |
80 | 84 | | |
81 | 85 | | |
82 | 86 | | |
| |||
224 | 228 | | |
225 | 229 | | |
226 | 230 | | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
227 | 234 | | |
228 | 235 | | |
229 | 236 | | |
| |||
0 commit comments