According to the change record change record
usage of global constant REQUEST_TIME and usage of $time = time(); should be replaced by using the service \Drupal::time()->getRequestTime(); added for the time.
$request_time = $_SERVER['REQUEST_TIME'];
$request_time = REQUEST_TIME;
$request_time = $requestStack->getCurrentRequest()->server->get('REQUEST_TIME');
$request_time = $request->server->get('REQUEST_TIME');
and most instances of
$time = time();
can be replaced with
$request_time = \Drupal::time()->getRequestTime();
or the equivalent using the injected service.
$request_time_float = $_SERVER['REQUEST_TIME_FLOAT'];
$request_time_float = $requestStack->getCurrentRequest()->server->get('REQUEST_TIME_FLOAT');
$request_time_float = $request->server->get('REQUEST_TIME_FLOAT');and many instances of
$microtime = microtime();
$microtime = microtime(TRUE);can be replaced with
$request_time = \Drupal::time()->getRequestMicroTime();
or the equivalent using the injected service.
$time = time();
can be replaced with
$request_time = \Drupal::time()->getCurrentTime();
$microtime = microtime();
$microtime = microtime(TRUE);can be replaced with
$request_time = \Drupal::time()->getCurrentMicroTime();
or the equivalent using the injected service.
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | 2820345-23.patch | 5.74 KB | jofitz |
| #23 | interdiff-21-23.txt | 3.77 KB | jofitz |
| #21 | 2820345-21.patch | 2.49 KB | jofitz |
| #15 | test_for_time_service_injection_2820345-2.patch | 2.36 KB | lomasr |
| #13 | test_for_time_service_injection_2820345.patch | 2.36 KB | lomasr |
Comments
Comment #2
prashant.cComment #3
prashant.cComment #4
catchWe should do this, but it's a normal task rather than critical feature request :)
Comment #5
lomasr commentedAdded a patch for core migrate module. Replaced time() with \Drupal::time()->getRequestTime()
Comment #6
lomasr commentedComment #7
lomasr commentedReplacing all of them can take some time, so I have created a sub issue
Comment #8
alexpott@lomasr before splitting this in many separate issues we need to consider what is the best scope. "Scope" means the set of changes included in an issue. See https://www.drupal.org/core/scope for guidelines and examples for Drupal core issue scope.
Comment #9
alexpottSince this can be done with a find and replace to start it probably does not make sense to splitting into sub-issues. Yes, sometimes the service will need injecting but I still think that a single should suffice.
Comment #10
lomasr commented@alexpott Thanks I will go through the link that you have suggested before adding new issues.
Comment #11
lomasr commentedYou are right splitting this into child issues does not make sense. Lets add patch to this issue only.
Comment #12
alexpottOne scope suggestion is to deal with time() and REQUEST_TIME in different issues. Also though we need to be careful when we use the service. In a test there is no point using the time service to get the time... ie. consider the following code:
This is from LocalePluralFormatTest and it should not be changed.
Comment #13
lomasr commentedThis is my first time to try to inject a service in Drupal 8 . Adding a test patch in order to continue injecting time services correctly in drupal core. Please review. Thanks.
Comment #15
lomasr commentedAdding the patch to test the injection of time service.
Comment #16
lomasr commentedComment #20
joachim commentedComment #21
jofitzRe-roll (hence no interdiff) and corrections to proof-of-concept dependency injection.
Comment #23
jofitzAddress coding standard errors and test failures from #21.
Comment #24
heddnThis fix should get picked up by a much more broad, replace all deprecated REQUEST_TIME issue.