@@ -108,17 +108,17 @@ making it especially useful for measuring performance.
108
108
Using a Clock inside a Service
109
109
------------------------------
110
110
111
- If your application uses :ref: ` service autoconfiguration < services-autoconfigure >`,
112
- any service whose class uses the :class: ` Symfony \\ Component \\ Clock \\ ClockAwareTrait ` will
113
- benefit of a `` ClockInterface `` typed-property `` $clock `` with the default implementation
114
- passed as a service. Services using this trait will also benefit of a `` now() `` method .
111
+ Using the Clock component in your services to retrieve the current time makes
112
+ them easier to test. For example, by using the `` MockClock `` implementation as
113
+ the default one during tests, you will have full control to set the "current time"
114
+ to any arbitrary date/time .
115
115
116
- By using this trait in your services to retrieve the current time, it will be easier to
117
- write time-sensitive classes. For example, by using the `` MockClock `` implementation as the
118
- default one during tests, you will have full control of the "current time" .
116
+ In order to use this component in your services, make their classes use the
117
+ :class: ` Symfony \\ Component \\ Clock \\ ClockAwareTrait ` and add a `` ClockInterface ``
118
+ typed-property `` $clock `` to their constructors .
119
119
120
- This is also true for the other clock implementations, as you will be assured to always use
121
- the same clock implementation across your code each time you need it ::
120
+ If your application uses :ref: ` service autoconfiguration < services-autoconfigure >`,
121
+ your services can now call the `` $this->now() `` method to get the current time ::
122
122
123
123
namespace App\TimeUtils;
124
124
@@ -139,10 +139,10 @@ the same clock implementation across your code each time you need it::
139
139
}
140
140
}
141
141
142
- Thanks to the ``ClockAwareTrait `` and by using the ``MockClock `` implementation,
143
- you will be able to test different times returned by `` now() ``. This allows you
144
- to test every case of your method without the need of actually being in a month
145
- or another.
142
+ Thanks to the ``ClockAwareTrait ``, and by using the ``MockClock `` implementation,
143
+ you can set the current time arbitrarily without having to change your service code.
144
+ This will help you test every case of your method without the need of actually
145
+ being in a month or another.
146
146
147
147
.. versionadded :: 6.3
148
148
0 commit comments