Commit e7e2c318 authored by catch's avatar catch

Issue #3113476 by alexpott, mpdonadio: Fallback when request is not available...

Issue #3113476 by alexpott, mpdonadio: Fallback when request is not available on the stack in Time service

(cherry picked from commit da21d14a)
parent 8f72af89
......@@ -30,14 +30,26 @@ public function __construct(RequestStack $request_stack) {
* {@inheritdoc}
*/
public function getRequestTime() {
return $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME');
$request = $this->requestStack->getCurrentRequest();
if ($request) {
return $request->server->get('REQUEST_TIME');
}
// If this is called prior to the request being pushed to the stack fallback
// to built-in globals (if available) or the system time.
return $_SERVER['REQUEST_TIME'] ?? $this->getCurrentTime();
}
/**
* {@inheritdoc}
*/
public function getRequestMicroTime() {
return $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME_FLOAT');
$request = $this->requestStack->getCurrentRequest();
if ($request) {
return $request->server->get('REQUEST_TIME_FLOAT');
}
// If this is called prior to the request being pushed to the stack fallback
// to built-in globals (if available) or the system time.
return $_SERVER['REQUEST_TIME_FLOAT'] ?? $this->getCurrentMicroTime();
}
/**
......
......@@ -14,6 +14,9 @@ interface TimeInterface {
* of the request. It will be the same value for the life of the request
* (even for long execution times).
*
* If the request is not available it will fallback to the current system
* time.
*
* This method can replace instances of
* @code
* $request_time = $_SERVER['REQUEST_TIME'];
......@@ -50,6 +53,9 @@ public function getRequestTime();
* microsecond precision, at the start of the request. It will be the same
* value for the life of the request (even for long execution times).
*
* If the request is not available it will fallback to the current system
* time with microsecond precision.
*
* This method can replace instances of
* @code
* $request_time_float = $_SERVER['REQUEST_TIME_FLOAT'];
......
......@@ -79,6 +79,28 @@ public function testGetRequestMicroTime() {
$this->assertEquals($expected, $this->time->getRequestMicroTime());
}
/**
* @covers ::getRequestTime
*/
public function testGetRequestTimeNoRequest() {
$expected = 12345678;
unset($_SERVER['REQUEST_TIME']);
$this->assertEquals($expected, $this->time->getRequestTime());
$_SERVER['REQUEST_TIME'] = 23456789;
$this->assertEquals(23456789, $this->time->getRequestTime());
}
/**
* @covers ::getRequestMicroTime
*/
public function testGetRequestMicroTimeNoRequest() {
$expected = 1234567.89;
unset($_SERVER['REQUEST_TIME_FLOAT']);
$this->assertEquals($expected, $this->time->getRequestMicroTime());
$_SERVER['REQUEST_TIME_FLOAT'] = 2345678.90;
$this->assertEquals(2345678.90, $this->time->getRequestMicroTime());
}
/**
* Tests the getCurrentTime method.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment