diff --git a/core/core.services.yml b/core/core.services.yml index de0b7bcbc2..693565d420 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1263,7 +1263,7 @@ services: class: Drupal\Core\EventSubscriber\FinishResponseSubscriber tags: - { name: event_subscriber } - arguments: ['@language_manager', '@config.factory', '@page_cache_request_policy', '@page_cache_response_policy', '@cache_contexts_manager', '%http.response.debug_cacheability_headers%', '@datetime.time'] + arguments: ['@language_manager', '@config.factory', '@page_cache_request_policy', '@page_cache_response_policy', '@cache_contexts_manager', '%http.response.debug_cacheability_headers%'] response_generator_subscriber: class: Drupal\Core\EventSubscriber\ResponseGeneratorSubscriber tags: @@ -1372,7 +1372,7 @@ services: arguments: [null, '@module_handler'] flood: class: Drupal\Core\Flood\DatabaseBackend - arguments: ['@database', '@request_stack', '@datetime.time'] + arguments: ['@database', '@request_stack'] tags: - { name: backend_overridable } plugin.manager.mail: @@ -1594,7 +1594,7 @@ services: alias: session_handler.storage session_handler.storage: class: Drupal\Core\Session\SessionHandler - arguments: ['@request_stack', '@database', '@datetime.time'] + arguments: ['@request_stack', '@database'] tags: - { name: backend_overridable } session_handler.write_safe: diff --git a/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php b/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php index 02245f0e8f..25ee4b3bfc 100644 --- a/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php +++ b/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php @@ -2,9 +2,9 @@ namespace Drupal\Core\Asset; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\State\StateInterface; -use Drupal\Component\Datetime\TimeInterface; /** * Optimizes CSS assets. @@ -205,9 +205,11 @@ public function getAll() { public function deleteAll() { $this->state->delete('drupal_css_cache_files'); - $delete_stale = function ($uri) { + $request_time = $this->time->getRequestTime(); + $threshold = \Drupal::config('system.performance')->get('stale_file_threshold'); + $delete_stale = function ($uri) use ($request_time, $threshold) { // Default stale file threshold is 30 days. - if ($this->time->getRequestTime() - filemtime($uri) > \Drupal::config('system.performance')->get('stale_file_threshold')) { + if ($request_time - filemtime($uri) > $threshold) { $this->fileSystem->delete($uri); } }; diff --git a/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php b/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php index afeabdc036..5f2ffb7e9b 100644 --- a/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php +++ b/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php @@ -2,9 +2,9 @@ namespace Drupal\Core\Asset; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\State\StateInterface; -use Drupal\Component\Datetime\TimeInterface; /** * Optimizes JavaScript assets. @@ -203,9 +203,11 @@ public function getAll() { */ public function deleteAll() { $this->state->delete('system.js_cache_files'); - $delete_stale = function ($uri) { + $request_time = $this->time->getRequestTime(); + $threshold = \Drupal::config('system.performance')->get('stale_file_threshold'); + $delete_stale = function ($uri) use ($request_time, $threshold) { // Default stale file threshold is 30 days. - if ($this->time->getRequestTime() - filemtime($uri) > \Drupal::config('system.performance')->get('stale_file_threshold')) { + if ($request_time - filemtime($uri) > $threshold) { $this->fileSystem->delete($uri); } }; diff --git a/core/lib/Drupal/Core/Asset/JsCollectionRenderer.php b/core/lib/Drupal/Core/Asset/JsCollectionRenderer.php index 307681a316..a05b11b14e 100644 --- a/core/lib/Drupal/Core/Asset/JsCollectionRenderer.php +++ b/core/lib/Drupal/Core/Asset/JsCollectionRenderer.php @@ -2,10 +2,10 @@ namespace Drupal\Core\Asset; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Component\Serialization\Json; use Drupal\Core\File\FileUrlGeneratorInterface; use Drupal\Core\State\StateInterface; -use Drupal\Component\Datetime\TimeInterface; /** * Renders JavaScript assets. @@ -38,7 +38,7 @@ class JsCollectionRenderer implements AssetCollectionRendererInterface { * * @param \Drupal\Core\State\StateInterface $state * The state key/value store. - * @param \Drupal\Core\File\FileUrlGeneratorInterface|null $file_url_generator + * @param \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator * The file URL generator. * @param \Drupal\Component\Datetime\TimeInterface|null $time * The time service. @@ -83,6 +83,7 @@ public function render(array $js_assets) { '#value' => '', ]; + $request_time = $this->time->getRequestTime(); // Loop through all JS assets. foreach ($js_assets as $js_asset) { // Element properties that do not depend on JS asset type. @@ -108,7 +109,7 @@ public function render(array $js_assets) { // Only add the cache-busting query string if this isn't an aggregate // file. if (!isset($js_asset['preprocessed'])) { - $element['#attributes']['src'] .= $query_string_separator . ($js_asset['cache'] ? $query_string : $this->time->getRequestTime()); + $element['#attributes']['src'] .= $query_string_separator . ($js_asset['cache'] ? $query_string : $request_time); } break; diff --git a/core/lib/Drupal/Core/Batch/BatchStorage.php b/core/lib/Drupal/Core/Batch/BatchStorage.php index 1f3766f410..c6728e9f95 100644 --- a/core/lib/Drupal/Core/Batch/BatchStorage.php +++ b/core/lib/Drupal/Core/Batch/BatchStorage.php @@ -2,11 +2,11 @@ namespace Drupal\Core\Batch; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Database\DatabaseException; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Drupal\Core\Access\CsrfTokenGenerator; -use Drupal\Component\Datetime\TimeInterface; class BatchStorage implements BatchStorageInterface { diff --git a/core/lib/Drupal/Core/Cache/ApcuBackend.php b/core/lib/Drupal/Core/Cache/ApcuBackend.php index e97e9c9754..65e3d2b60a 100644 --- a/core/lib/Drupal/Core/Cache/ApcuBackend.php +++ b/core/lib/Drupal/Core/Cache/ApcuBackend.php @@ -64,7 +64,11 @@ public function __construct($bin, $site_prefix, CacheTagsChecksumInterface $chec $this->sitePrefix = $site_prefix; $this->checksumProvider = $checksum_provider; $this->binPrefix = $this->sitePrefix . '::' . $this->bin . '::'; - $this->time = $time ?: \Drupal::service('datetime.time'); + if (!$time) { + @trigger_error('Calling ' . __METHOD__ . '() without the $time argument is deprecated in drupal:9.4.0 and $time argument will be required in drupal:10.0.0. See https://www.drupal.org/node/3113971', E_USER_DEPRECATED); + $time = \Drupal::service('datetime.time'); + } + $this->time = $time; } /** @@ -245,8 +249,9 @@ public function invalidate($cid) { * {@inheritdoc} */ public function invalidateMultiple(array $cids) { + $expire = $this->time->getRequestTime(); foreach ($this->getMultiple($cids) as $cache) { - $this->set($cache->cid, $cache, \Drupal::time()->getRequestTime() - 1); + $this->set($cache->cid, $cache, $expire - 1); } } @@ -254,9 +259,10 @@ public function invalidateMultiple(array $cids) { * {@inheritdoc} */ public function invalidateAll() { + $expire = $this->time->getRequestTime(); foreach ($this->getAll() as $data) { $cid = str_replace($this->binPrefix, '', $data['key']); - $this->set($cid, $data['value'], $this->time->getRequestTime() - 1); + $this->set($cid, $data['value'], $expire - 1); } } diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index bc00e2e236..ccad6eb879 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -334,9 +334,10 @@ public function invalidateMultiple(array $cids) { $cids = array_values(array_map([$this, 'normalizeCid'], $cids)); try { // Update in chunks when a large array is passed. + $request_time = \Drupal::time()->getRequestTime(); foreach (array_chunk($cids, 1000) as $cids_chunk) { $this->connection->update($this->bin) - ->fields(['expire' => \Drupal::time()->getRequestTime() - 1]) + ->fields(['expire' => $request_time - 1]) ->condition('cid', $cids_chunk, 'IN') ->execute(); } diff --git a/core/lib/Drupal/Core/Cache/MemoryBackend.php b/core/lib/Drupal/Core/Cache/MemoryBackend.php index 3cd2bfd845..332ee5ecc3 100644 --- a/core/lib/Drupal/Core/Cache/MemoryBackend.php +++ b/core/lib/Drupal/Core/Cache/MemoryBackend.php @@ -201,7 +201,14 @@ public function removeBin() { * @return int */ protected function getRequestTime() { - return defined('REQUEST_TIME') ? \Drupal::time()->getRequestTime() : (int) $_SERVER['REQUEST_TIME']; + // @todo Replace calls to this method in loops with a variable. + if (\Drupal::hasRequest()) { + $request_time = \Drupal::request()->server->getInt('REQUEST_TIME'); + } + else { + $request_time = defined('REQUEST_TIME') ? REQUEST_TIME : (int) $_SERVER['REQUEST_TIME']; + } + return $request_time; } /** diff --git a/core/lib/Drupal/Core/Cache/PhpBackend.php b/core/lib/Drupal/Core/Cache/PhpBackend.php index 20f8ef953b..be1a53467d 100644 --- a/core/lib/Drupal/Core/Cache/PhpBackend.php +++ b/core/lib/Drupal/Core/Cache/PhpBackend.php @@ -126,6 +126,7 @@ protected function prepareItem($cache, $allow_invalid) { } // Check expire time. + // @todo Properly inject time service as this method used in loops. $cache->valid = $cache->expire == Cache::PERMANENT || $cache->expire >= \Drupal::time()->getRequestTime(); // Check if invalidateTags() has been called with any of the item's tags. @@ -195,6 +196,7 @@ public function invalidate($cid) { */ protected function invalidateByHash($cidhash) { if ($item = $this->getByHash($cidhash)) { + // @todo Properly inject time service as this method used in loops. $item->expire = \Drupal::time()->getRequestTime() - 1; $this->writeItem($cidhash, $item); } diff --git a/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php index d9523c2c95..068c0eb58a 100644 --- a/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/FinishResponseSubscriber.php @@ -15,7 +15,6 @@ use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; -use Drupal\Component\Datetime\TimeInterface; /** * Response subscriber to handle finished responses. @@ -64,13 +63,6 @@ class FinishResponseSubscriber implements EventSubscriberInterface { */ protected $debugCacheabilityHeaders = FALSE; - /** - * The time service. - * - * @var \Drupal\Component\Datetime\TimeInterface - */ - protected $time; - /** * Constructs a FinishResponseSubscriber object. * @@ -86,21 +78,14 @@ class FinishResponseSubscriber implements EventSubscriberInterface { * The cache contexts manager service. * @param bool $http_response_debug_cacheability_headers * (optional) Whether to send cacheability headers for debugging purposes. - * @param \Drupal\Component\Datetime\TimeInterface|null $time - * The time service. */ - public function __construct(LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, CacheContextsManager $cache_contexts_manager, $http_response_debug_cacheability_headers = FALSE, TimeInterface $time = NULL) { + public function __construct(LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, CacheContextsManager $cache_contexts_manager, $http_response_debug_cacheability_headers = FALSE) { $this->languageManager = $language_manager; $this->config = $config_factory->get('system.performance'); $this->requestPolicy = $request_policy; $this->responsePolicy = $response_policy; $this->cacheContextsManager = $cache_contexts_manager; $this->debugCacheabilityHeaders = $http_response_debug_cacheability_headers; - if (!$time) { - @trigger_error('Calling ' . __METHOD__ . '() without the $time argument is deprecated in drupal:9.4.0 and $time argument will be required in drupal:10.0.0. See https://www.drupal.org/node/3113971', E_USER_DEPRECATED); - $time = \Drupal::service('datetime.time'); - } - $this->time = $time; } /** @@ -281,8 +266,8 @@ protected function setResponseCacheable(Response $response, Request $request) { // In order to support HTTP cache-revalidation, ensure that there is a // Last-Modified and an ETag header on the response. if (!$response->headers->has('Last-Modified')) { - $timestamp = $this->time->getRequestTime(); - $response->setLastModified(new \DateTime(gmdate(DateTimePlus::RFC7231, $this->time->getRequestTime()))); + $timestamp = $request->server->getInt('REQUEST_TIME'); + $response->setLastModified(new \DateTime(gmdate(DateTimePlus::RFC7231, $timestamp))); } else { $timestamp = $response->getLastModified()->getTimestamp(); diff --git a/core/lib/Drupal/Core/Flood/DatabaseBackend.php b/core/lib/Drupal/Core/Flood/DatabaseBackend.php index 6bc1588ffe..85b3d9c44c 100644 --- a/core/lib/Drupal/Core/Flood/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Flood/DatabaseBackend.php @@ -5,7 +5,6 @@ use Drupal\Core\Database\DatabaseException; use Symfony\Component\HttpFoundation\RequestStack; use Drupal\Core\Database\Connection; -use Drupal\Component\Datetime\TimeInterface; /** * Defines the database flood backend. This is the default Drupal backend. @@ -31,13 +30,6 @@ class DatabaseBackend implements FloodInterface { */ protected $requestStack; - /** - * The time service. - * - * @var \Drupal\Component\Datetime\TimeInterface - */ - protected $time; - /** * Construct the DatabaseBackend. * @@ -46,17 +38,10 @@ class DatabaseBackend implements FloodInterface { * information. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * The request stack used to retrieve the current request. - * @param \Drupal\Component\Datetime\TimeInterface|null $time - * The time service. */ - public function __construct(Connection $connection, RequestStack $request_stack, TimeInterface $time = NULL) { + public function __construct(Connection $connection, RequestStack $request_stack) { $this->connection = $connection; $this->requestStack = $request_stack; - if (!$time) { - @trigger_error('Calling ' . __METHOD__ . '() without the $time argument is deprecated in drupal:9.4.0 and $time argument will be required in drupal:10.0.0. See https://www.drupal.org/node/3113971', E_USER_DEPRECATED); - $time = \Drupal::service('datetime.time'); - } - $this->time = $time; } /** @@ -94,12 +79,13 @@ public function register($name, $window = 3600, $identifier = NULL) { * @see \Drupal\Core\Flood\DatabaseBackend::register */ protected function doInsert($name, $window, $identifier) { + $request_time = $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME'); $this->connection->insert(static::TABLE_NAME) ->fields([ 'event' => $name, 'identifier' => $identifier, - 'timestamp' => $this->time->getRequestTime(), - 'expiration' => $this->time->getRequestTime() + $window, + 'timestamp' => $request_time, + 'expiration' => $request_time + $window, ]) ->execute(); } @@ -133,7 +119,7 @@ public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) $number = $this->connection->select(static::TABLE_NAME, 'f') ->condition('event', $name) ->condition('identifier', $identifier) - ->condition('timestamp', $this->time->getRequestTime() - $window, '>') + ->condition('timestamp', $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME') - $window, '>') ->countQuery() ->execute() ->fetchField(); @@ -151,7 +137,7 @@ public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) public function garbageCollection() { try { $this->connection->delete(static::TABLE_NAME) - ->condition('expiration', $this->time->getRequestTime(), '<') + ->condition('expiration', $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME'), '<') ->execute(); } catch (\Exception $e) { diff --git a/core/lib/Drupal/Core/Flood/MemoryBackend.php b/core/lib/Drupal/Core/Flood/MemoryBackend.php index 5f0c723571..d1cd021dd8 100644 --- a/core/lib/Drupal/Core/Flood/MemoryBackend.php +++ b/core/lib/Drupal/Core/Flood/MemoryBackend.php @@ -38,7 +38,7 @@ public function register($name, $window = 3600, $identifier = NULL) { if (!isset($identifier)) { $identifier = $this->requestStack->getCurrentRequest()->getClientIp(); } - // We can't use TimeInterface::getRequestTime() here, because that would not guarantee + // We can't use request time here, because that would not guarantee // uniqueness. $time = microtime(TRUE); $this->events[$name][$identifier][] = ['expire' => $time + $window, 'time' => $time]; diff --git a/core/lib/Drupal/Core/Session/SessionHandler.php b/core/lib/Drupal/Core/Session/SessionHandler.php index 46d343bf4d..38674a5460 100644 --- a/core/lib/Drupal/Core/Session/SessionHandler.php +++ b/core/lib/Drupal/Core/Session/SessionHandler.php @@ -8,7 +8,6 @@ use Drupal\Core\Utility\Error; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy; -use Drupal\Component\Datetime\TimeInterface; /** * Default session handler. @@ -31,13 +30,6 @@ class SessionHandler extends AbstractProxy implements \SessionHandlerInterface { */ protected $connection; - /** - * The time service. - * - * @var \Drupal\Component\Datetime\TimeInterface - */ - protected $time; - /** * Constructs a new SessionHandler instance. * @@ -45,17 +37,10 @@ class SessionHandler extends AbstractProxy implements \SessionHandlerInterface { * The request stack. * @param \Drupal\Core\Database\Connection $connection * The database connection. - * @param \Drupal\Component\Datetime\TimeInterface|null $time - * The time service. */ - public function __construct(RequestStack $request_stack, Connection $connection, TimeInterface $time = NULL) { + public function __construct(RequestStack $request_stack, Connection $connection) { $this->requestStack = $request_stack; $this->connection = $connection; - if (!$time) { - @trigger_error('Calling ' . __METHOD__ . '() without the $time argument is deprecated in drupal:9.4.0 and $time argument will be required in drupal:10.0.0. See https://www.drupal.org/node/3113971', E_USER_DEPRECATED); - $time = \Drupal::service('datetime.time'); - } - $this->time = $time; } /** @@ -94,7 +79,7 @@ public function write($sid, $value) { 'uid' => $request->getSession()->get('uid', 0), 'hostname' => $request->getClientIP(), 'session' => $value, - 'timestamp' => $this->time->getRequestTime(), + 'timestamp' => $request->server->getInt('REQUEST_TIME'), ]; $this->connection->merge('sessions') ->keys(['sid' => Crypt::hashBase64($sid)]) @@ -146,7 +131,7 @@ public function gc($lifetime) { // to '1814400'. At that value, only after a user doesn't log in after // three weeks (1814400 seconds) will their session be removed. $this->connection->delete('sessions') - ->condition('timestamp', $this->time->getRequestTime() - $lifetime, '<') + ->condition('timestamp', $this->requestStack->getCurrentRequest()->server->getInt('REQUEST_TIME') - $lifetime, '<') ->execute(); return TRUE; } diff --git a/core/lib/Drupal/Core/Session/SessionManager.php b/core/lib/Drupal/Core/Session/SessionManager.php index 46bab9b0a5..68262b63f2 100644 --- a/core/lib/Drupal/Core/Session/SessionManager.php +++ b/core/lib/Drupal/Core/Session/SessionManager.php @@ -7,7 +7,6 @@ use Drupal\Core\DependencyInjection\DependencySerializationTrait; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; -use Drupal\Component\Datetime\TimeInterface; /** * Manages user sessions. @@ -68,13 +67,6 @@ class SessionManager extends NativeSessionStorage implements SessionManagerInter */ protected $writeSafeHandler; - /** - * The time service. - * - * @var \Drupal\Component\Datetime\TimeInterface - */ - protected $time; - /** * Constructs a new session manager instance. * @@ -86,23 +78,15 @@ class SessionManager extends NativeSessionStorage implements SessionManagerInter * The session metadata bag. * @param \Drupal\Core\Session\SessionConfigurationInterface $session_configuration * The session configuration interface. - * @param null $handler + * @param \Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy|Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler|\SessionHandlerInterface|null $handler * The object to register as a PHP session handler. - * @param \Drupal\Component\Datetime\TimeInterface|null $time - * The time service. - * - * @see \Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage::setSaveHandler() + * @see \Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage::setSaveHandler() */ - public function __construct(RequestStack $request_stack, Connection $connection, MetadataBag $metadata_bag, SessionConfigurationInterface $session_configuration, $handler = NULL, TimeInterface $time = NULL) { + public function __construct(RequestStack $request_stack, Connection $connection, MetadataBag $metadata_bag, SessionConfigurationInterface $session_configuration, $handler = NULL) { $options = []; $this->sessionConfiguration = $session_configuration; $this->requestStack = $request_stack; $this->connection = $connection; - if (!$time) { - @trigger_error('Calling ' . __METHOD__ . '() without the $time argument is deprecated in drupal:9.4.0 and $time argument will be required in drupal:10.0.0. See https://www.drupal.org/node/3113971', E_USER_DEPRECATED); - $time = \Drupal::service('datetime.time'); - } - $this->time = $time; parent::__construct($options, $handler, $metadata_bag); } @@ -276,7 +260,7 @@ public function destroy() { // setcookie() can only be called when headers are not yet sent. if ($cookies->has($session_name) && !headers_sent()) { $params = session_get_cookie_params(); - setcookie($session_name, '', $this->time->getRequestTime() - 3600, $params['path'], $params['domain'], $params['secure'], $params['httponly']); + setcookie($session_name, '', $this->requestStack->getCurrentRequest()->server->getInt('REQUEST_TIME') - 3600, $params['path'], $params['domain'], $params['secure'], $params['httponly']); $cookies->remove($session_name); } } diff --git a/core/modules/comment/src/CommentStatistics.php b/core/modules/comment/src/CommentStatistics.php index 45ff590fc8..6042c01d70 100644 --- a/core/modules/comment/src/CommentStatistics.php +++ b/core/modules/comment/src/CommentStatistics.php @@ -2,6 +2,7 @@ namespace Drupal\comment; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; @@ -10,7 +11,6 @@ use Drupal\Core\State\StateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\user\EntityOwnerInterface; -use Drupal\Component\Datetime\TimeInterface; class CommentStatistics implements CommentStatisticsInterface { @@ -128,6 +128,7 @@ public function create(FieldableEntityInterface $entity, $fields) { 'last_comment_uid', 'comment_count', ]); + $request_time = $this->time->getRequestTime(); foreach ($fields as $field_name => $detail) { // Skip fields that entity does not have. if (!$entity->hasField($field_name)) { @@ -144,8 +145,8 @@ public function create(FieldableEntityInterface $entity, $fields) { // EntityOwnerInterface or author is not set. $last_comment_uid = $this->currentUser->id(); } - // Default to TimeInterface::getRequestTime() when entity does not have a changed property. - $last_comment_timestamp = $this->time->getRequestTime(); + // Default to request time when entity does not have a changed property. + $last_comment_timestamp = $request_time; // @todo Make comment statistics language aware and add some tests. See // https://www.drupal.org/node/2318875 if ($entity instanceof EntityChangedInterface) { @@ -266,7 +267,7 @@ public function update(CommentInterface $comment) { 'cid' => 0, 'comment_count' => 0, // Use the changed date of the entity if it's set, or default to - // TimeInterface::getRequestTime(). + // request time. 'last_comment_timestamp' => ($entity instanceof EntityChangedInterface) ? $entity->getChangedTimeAcrossTranslations() : $this->time->getRequestTime(), 'last_comment_name' => '', 'last_comment_uid' => $last_comment_uid, diff --git a/core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php b/core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php index cab3cf2e4f..57730485ae 100644 --- a/core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php +++ b/core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php @@ -3,9 +3,9 @@ namespace Drupal\Tests\comment\Unit; use Drupal\comment\CommentStatistics; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Tests\UnitTestCase; -use Drupal\Component\Datetime\TimeInterface; /** * @coversDefaultClass \Drupal\comment\CommentStatistics diff --git a/core/modules/search/src/SearchIndex.php b/core/modules/search/src/SearchIndex.php index 65ad92fecb..9cb4058003 100644 --- a/core/modules/search/src/SearchIndex.php +++ b/core/modules/search/src/SearchIndex.php @@ -2,11 +2,11 @@ namespace Drupal\search; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Database\Connection; use Drupal\search\Exception\SearchIndexException; -use Drupal\Component\Datetime\TimeInterface; /** * Provides search index management functions. diff --git a/core/modules/user/src/EventSubscriber/UserRequestSubscriber.php b/core/modules/user/src/EventSubscriber/UserRequestSubscriber.php index a2ed20658d..fddaccdbc7 100644 --- a/core/modules/user/src/EventSubscriber/UserRequestSubscriber.php +++ b/core/modules/user/src/EventSubscriber/UserRequestSubscriber.php @@ -8,7 +8,6 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\TerminateEvent; use Symfony\Component\HttpKernel\KernelEvents; -use Drupal\Component\Datetime\TimeInterface; /** * Updates the current user's last access time. @@ -29,13 +28,6 @@ class UserRequestSubscriber implements EventSubscriberInterface { */ protected $entityTypeManager; - /** - * The time service. - * - * @var \Drupal\Component\Datetime\TimeInterface - */ - protected $time; - /** * Constructs a new UserRequestSubscriber. * @@ -43,17 +35,10 @@ class UserRequestSubscriber implements EventSubscriberInterface { * The current user. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager service. - * @param \Drupal\Component\Datetime\TimeInterface|null $time - * The time service. */ - public function __construct(AccountInterface $account, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time = NULL) { + public function __construct(AccountInterface $account, EntityTypeManagerInterface $entity_type_manager) { $this->account = $account; $this->entityTypeManager = $entity_type_manager; - if (!$time) { - @trigger_error('Calling ' . __METHOD__ . '() without the $time argument is deprecated in drupal:9.4.0 and $time argument will be required in drupal:10.0.0. See https://www.drupal.org/node/3113971', E_USER_DEPRECATED); - $time = \Drupal::service('datetime.time'); - } - $this->time = $time; } /** @@ -63,11 +48,12 @@ public function __construct(AccountInterface $account, EntityTypeManagerInterfac * The event to process. */ public function onKernelTerminate(TerminateEvent $event) { - if ($this->account->isAuthenticated() && $this->time->getRequestTime() - $this->account->getLastAccessedTime() > Settings::get('session_write_interval', 180)) { + $timestamp = $event->getRequest()->server->getInt('REQUEST_TIME'); + if ($this->account->isAuthenticated() && $timestamp - $this->account->getLastAccessedTime() > Settings::get('session_write_interval', 180)) { // Do that no more than once per 180 seconds. /** @var \Drupal\user\UserStorageInterface $storage */ $storage = $this->entityTypeManager->getStorage('user'); - $storage->updateLastAccessTimestamp($this->account, $this->time->getRequestTime()); + $storage->updateLastAccessTimestamp($this->account, $timestamp); } } diff --git a/core/modules/user/user.services.yml b/core/modules/user/user.services.yml index 952b5e3cb9..2f82493c44 100644 --- a/core/modules/user/user.services.yml +++ b/core/modules/user/user.services.yml @@ -38,7 +38,7 @@ services: - { name: event_subscriber } user_last_access_subscriber: class: Drupal\user\EventSubscriber\UserRequestSubscriber - arguments: ['@current_user', '@entity_type.manager', '@datetime.time'] + arguments: ['@current_user', '@entity_type.manager'] tags: - { name: event_subscriber } theme.negotiator.admin_theme: diff --git a/core/tests/Drupal/Tests/Core/Asset/CssCollectionOptimizerUnitTest.php b/core/tests/Drupal/Tests/Core/Asset/CssCollectionOptimizerUnitTest.php index 35729d9b01..4e6b9c6524 100644 --- a/core/tests/Drupal/Tests/Core/Asset/CssCollectionOptimizerUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/CssCollectionOptimizerUnitTest.php @@ -32,9 +32,6 @@ class CssCollectionOptimizerUnitTest extends UnitTestCase { */ protected $optimizer; - /** - * {@inheritDoc} - */ protected function setUp(): void { parent::setUp(); $mock_grouper = $this->createMock(AssetCollectionGrouperInterface::class);