diff --git a/core/lib/Drupal/Core/Datetime/DateHelper.php b/core/lib/Drupal/Core/Datetime/DateHelper.php index 927afdf6c0b..d08bf8d5a43 100644 --- a/core/lib/Drupal/Core/Datetime/DateHelper.php +++ b/core/lib/Drupal/Core/Datetime/DateHelper.php @@ -290,11 +290,12 @@ class DateHelper { */ public static function years($min = 0, $max = 0, $required = FALSE) { // Ensure $min and $max are valid values. + $requestTime = \Drupal::time()->getRequestTime(); if (empty($min)) { - $min = intval(date('Y', REQUEST_TIME) - 3); + $min = intval(date('Y', $requestTime) - 3); } if (empty($max)) { - $max = intval(date('Y', REQUEST_TIME) + 3); + $max = intval(date('Y', $requestTime) + 3); } $none = ['' => '']; $range = range($min, $max); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php index bd2e129f29c..9e737758ab3 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php @@ -30,7 +30,7 @@ class ChangedItem extends CreatedItem { // Set the timestamp to request time if it is not set. if (!$this->value) { - $this->value = REQUEST_TIME; + $this->value = \Drupal::time()->getRequestTime(); } else { // On an existing entity translation, the changed timestamp will only be @@ -47,7 +47,7 @@ class ChangedItem extends CreatedItem { if (!$entity->isNew() && $original && $original->hasTranslation($langcode)) { $original_value = $original->getTranslation($langcode)->get($this->getFieldDefinition()->getName())->value; if ($this->value == $original_value && $entity->hasTranslationChanges()) { - $this->value = REQUEST_TIME; + $this->value = \Drupal::time()->getRequestTime(); } } } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php index da2e2a74506..78a66a7f849 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php @@ -22,7 +22,7 @@ class CreatedItem extends TimestampItem { public function applyDefaultValue($notify = TRUE) { parent::applyDefaultValue($notify); // Created fields default to the current timestamp. - $this->setValue(['value' => REQUEST_TIME], $notify); + $this->setValue(['value' => \Drupal::time()->getRequestTime()], $notify); return $this; } diff --git a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php index 277bbd2e04e..0d0801b1040 100644 --- a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php +++ b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php @@ -2,6 +2,7 @@ namespace Drupal\Core\KeyValueStore; +use Drupal\Component\Datetime\TimeInterface; use Drupal\Component\Serialization\SerializationInterface; use Drupal\Core\Database\Connection; @@ -22,10 +23,27 @@ class DatabaseStorageExpirable extends DatabaseStorage implements KeyValueStoreE * The serialization class to use. * @param \Drupal\Core\Database\Connection $connection * The database connection to use. + * @param \Drupal\Component\Datetime\TimeInterface|string|null $time + * The time service. * @param string $table * The name of the SQL table to use, defaults to key_value_expire. */ - public function __construct($collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value_expire') { + public function __construct( + $collection, + SerializationInterface $serializer, + Connection $connection, + protected TimeInterface|string|null $time = NULL, + $table = 'key_value_expire', + ) { + if (is_null($time)) { + @trigger_error('Calling ' . __METHOD__ . ' without the $time argument is deprecated in drupal:10.3.0 and it will be required in drupal:11.0.0. See https://www.drupal.org/node/3387233', E_USER_DEPRECATED); + $this->time = \Drupal::time(); + } + elseif (is_string($time)) { + @trigger_error('Calling ' . __METHOD__ . ' with the $table as 4th argument is deprecated in drupal:10.3.0 and it will be the 5th argument in drupal:11.0.0. See https://www.drupal.org/node/3387233', E_USER_DEPRECATED); + $table = $time; + $this->time = \Drupal::time(); + } parent::__construct($collection, $serializer, $connection, $table); } @@ -37,7 +55,7 @@ class DatabaseStorageExpirable extends DatabaseStorage implements KeyValueStoreE return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :key AND [expire] > :now', [ ':collection' => $this->collection, ':key' => $key, - ':now' => REQUEST_TIME, + ':now' => $this->time->getRequestTime(), ])->fetchField(); } catch (\Exception $e) { @@ -54,7 +72,7 @@ class DatabaseStorageExpirable extends DatabaseStorage implements KeyValueStoreE $values = $this->connection->query( 'SELECT [name], [value] FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [expire] > :now AND [name] IN ( :keys[] ) AND [collection] = :collection', [ - ':now' => REQUEST_TIME, + ':now' => $this->time->getRequestTime(), ':keys[]' => $keys, ':collection' => $this->collection, ])->fetchAllKeyed(); @@ -79,7 +97,7 @@ class DatabaseStorageExpirable extends DatabaseStorage implements KeyValueStoreE 'SELECT [name], [value] FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [collection] = :collection AND [expire] > :now', [ ':collection' => $this->collection, - ':now' => REQUEST_TIME, + ':now' => $this->time->getRequestTime(), ])->fetchAllKeyed(); return array_map([$this->serializer, 'decode'], $values); } @@ -109,7 +127,7 @@ class DatabaseStorageExpirable extends DatabaseStorage implements KeyValueStoreE ]) ->fields([ 'value' => $this->serializer->encode($value), - 'expire' => REQUEST_TIME + $expire, + 'expire' => $this->time->getRequestTime() + $expire, ]) ->execute(); } diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseExpirableFactory.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseExpirableFactory.php index 40ec3955518..85eded3c066 100644 --- a/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseExpirableFactory.php +++ b/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseExpirableFactory.php @@ -60,7 +60,7 @@ class KeyValueDatabaseExpirableFactory implements KeyValueExpirableFactoryInterf */ public function get($collection) { if (!isset($this->storages[$collection])) { - $this->storages[$collection] = new DatabaseStorageExpirable($collection, $this->serializer, $this->connection); + $this->storages[$collection] = new DatabaseStorageExpirable($collection, $this->serializer, $this->connection, $this->time); } return $this->storages[$collection]; } diff --git a/core/lib/Drupal/Core/Queue/DatabaseQueue.php b/core/lib/Drupal/Core/Queue/DatabaseQueue.php index 3e2ed4272e3..11a33c602ec 100644 --- a/core/lib/Drupal/Core/Queue/DatabaseQueue.php +++ b/core/lib/Drupal/Core/Queue/DatabaseQueue.php @@ -87,8 +87,9 @@ class DatabaseQueue implements ReliableQueueInterface, QueueGarbageCollectionInt ->fields([ 'name' => $this->name, 'data' => serialize($data), - // We cannot rely on REQUEST_TIME because many items might be created - // by a single request which takes longer than 1 second. + // We cannot rely on \Drupal::time()->getRequestTime() because many + // items might be created by a single request which takes longer than + // 1 second. 'created' => \Drupal::time()->getCurrentTime(), ]); // Return the new serial ID, or FALSE on failure. @@ -133,11 +134,11 @@ class DatabaseQueue implements ReliableQueueInterface, QueueGarbageCollectionInt } // Try to update the item. Only one thread can succeed in UPDATEing the - // same row. We cannot rely on REQUEST_TIME because items might be - // claimed by a single consumer which runs longer than 1 second. If we - // continue to use REQUEST_TIME instead of the current time(), we steal - // time from the lease, and will tend to reset items before the lease - // should really expire. + // same row. We cannot rely on \Drupal::time()->getRequestTime() because + // items might be claimed by a single consumer which runs longer than 1 + // second. If we continue to use ::getRequestTime() instead of + // ::getCurrentTime(), we steal time from the lease, and will tend to + // reset items before the lease should really expire. $update = $this->connection->update(static::TABLE_NAME) ->fields([ 'expire' => \Drupal::time()->getCurrentTime() + $lease_time, diff --git a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php index 23c6decaeb8..336de03794e 100644 --- a/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php +++ b/core/lib/Drupal/Core/Template/TwigPhpStorageCache.php @@ -112,7 +112,7 @@ class TwigPhpStorageCache implements CacheInterface { $this->storage()->save($key, $content); // Save the last mtime. $cid = 'twig:' . $key; - $this->cache->set($cid, REQUEST_TIME); + $this->cache->set($cid, \Drupal::time()->getRequestTime()); } /** diff --git a/core/modules/config_translation/src/FormElement/DateFormat.php b/core/modules/config_translation/src/FormElement/DateFormat.php index 37282c5110c..1f4e6542917 100644 --- a/core/modules/config_translation/src/FormElement/DateFormat.php +++ b/core/modules/config_translation/src/FormElement/DateFormat.php @@ -16,7 +16,7 @@ class DateFormat extends FormElementBase { /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */ $date_formatter = \Drupal::service('date.formatter'); $description = $this->t('A user-defined date format. See the PHP manual for available options.'); - $format = $this->t('Displayed as %date_format', ['%date_format' => $date_formatter->format(REQUEST_TIME, 'custom', $translation_config)]); + $format = $this->t('Displayed as %date_format', ['%date_format' => $date_formatter->format(\Drupal::time()->getRequestTime(), 'custom', $translation_config)]); return [ '#type' => 'textfield', diff --git a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php index 1a5b215a397..ee104a410ed 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php @@ -113,7 +113,7 @@ class DateTimeItem extends FieldItemBase implements DateTimeItemInterface { // Just pick a date in the past year. No guidance is provided by this Field // type. - $timestamp = REQUEST_TIME - mt_rand(0, 86400 * 365); + $timestamp = \Drupal::time()->getRequestTime() - mt_rand(0, 86400 * 365); if ($type == DateTimeItem::DATETIME_TYPE_DATE) { $values['value'] = gmdate(static::DATE_STORAGE_FORMAT, $timestamp); } diff --git a/core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php b/core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php index a9fd662bc24..3020a98de6f 100644 --- a/core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php +++ b/core/modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php @@ -99,7 +99,7 @@ class DateRangeItem extends DateTimeItem { // Just pick a date in the past year. No guidance is provided by this Field // type. - $start = REQUEST_TIME - mt_rand(0, 86400 * 365) - 86400; + $start = \Drupal::time()->getRequestTime() - mt_rand(0, 86400 * 365) - 86400; $end = $start + 86400; if ($type == static::DATETIME_TYPE_DATETIME) { $values['value'] = gmdate(DateTimeItemInterface::DATETIME_STORAGE_FORMAT, $start); diff --git a/core/modules/migrate_drupal_ui/src/Batch/MigrateUpgradeImportBatch.php b/core/modules/migrate_drupal_ui/src/Batch/MigrateUpgradeImportBatch.php index 06ea069f9f3..e7a706783d3 100644 --- a/core/modules/migrate_drupal_ui/src/Batch/MigrateUpgradeImportBatch.php +++ b/core/modules/migrate_drupal_ui/src/Batch/MigrateUpgradeImportBatch.php @@ -293,7 +293,8 @@ class MigrateUpgradeImportBatch { */ public static function onPostRowSave(MigratePostRowSaveEvent $event) { // We want to interrupt this batch and start a fresh one. - if ((time() - REQUEST_TIME) > static::$maxExecTime) { + $time = \Drupal::time(); + if (($time->getCurrentTime() - $time->getRequestTime()) > static::$maxExecTime) { $event->getMigration()->interruptMigration(MigrationInterface::RESULT_INCOMPLETE); } } @@ -324,7 +325,8 @@ class MigrateUpgradeImportBatch { */ public static function onPostRowDelete(MigrateRowDeleteEvent $event) { // We want to interrupt this batch and start a fresh one. - if ((time() - REQUEST_TIME) > static::$maxExecTime) { + $time = \Drupal::time(); + if (($time->getCurrentTime() - $time->getRequestTime()) > static::$maxExecTime) { $event->getMigration()->interruptMigration(MigrationInterface::RESULT_INCOMPLETE); } } diff --git a/core/modules/views/src/ViewExecutable.php b/core/modules/views/src/ViewExecutable.php index 7b04186dbe1..11d76b70691 100644 --- a/core/modules/views/src/ViewExecutable.php +++ b/core/modules/views/src/ViewExecutable.php @@ -1701,7 +1701,7 @@ class ViewExecutable { \Drupal::moduleHandler()->invokeAll('views_pre_view', [$this, $display_id, &$this->args]); // Allow hook_views_pre_view() to set the dom_id, then ensure it is set. - $this->dom_id = !empty($this->dom_id) ? $this->dom_id : hash('sha256', $this->storage->id() . REQUEST_TIME . mt_rand()); + $this->dom_id = !empty($this->dom_id) ? $this->dom_id : hash('sha256', $this->storage->id() . \Drupal::time()->getRequestTime() . mt_rand()); // Allow the display handler to set up for execution $this->display_handler->preExecute(); diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon index 123dd80f249..42dae7ffe32 100644 --- a/core/phpstan-baseline.neon +++ b/core/phpstan-baseline.neon @@ -414,11 +414,6 @@ parameters: count: 1 path: lib/Drupal/Core/Database/Query/Upsert.php - - - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 2 - path: lib/Drupal/Core/Datetime/DateHelper.php - - message: """ #^Usage of deprecated trait Drupal\\\\Component\\\\DependencyInjection\\\\ServiceIdHashTrait in class Drupal\\\\Core\\\\DependencyInjection\\\\ContainerBuilder\\: @@ -637,16 +632,6 @@ parameters: count: 1 path: lib/Drupal/Core/Field/FieldTypePluginManager.php - - - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 2 - path: lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php - - - - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 1 - path: lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php - - message: "#^Method Drupal\\\\Core\\\\Field\\\\Plugin\\\\Field\\\\FieldType\\\\EntityReferenceItem\\:\\:generateSampleValue\\(\\) should return array but return statement is missing\\.$#" count: 1 @@ -712,11 +697,6 @@ parameters: count: 1 path: lib/Drupal/Core/Form/FormValidator.php - - - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 4 - path: lib/Drupal/Core/KeyValueStore/DatabaseStorageExpirable.php - - message: "#^Method Drupal\\\\Core\\\\KeyValueStore\\\\NullStorageExpirable\\:\\:setIfNotExists\\(\\) should return bool but return statement is missing\\.$#" count: 1 @@ -861,11 +841,6 @@ parameters: count: 1 path: lib/Drupal/Core/Template/TwigEnvironment.php - - - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 1 - path: lib/Drupal/Core/Template/TwigPhpStorageCache.php - - message: "#^Constructor of class Drupal\\\\Core\\\\Test\\\\TestRunnerKernel has an unused parameter \\$allow_dumping\\.$#" count: 1 @@ -1048,11 +1023,6 @@ parameters: count: 2 path: modules/config/src/Form/ConfigSingleImportForm.php - - - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 1 - path: modules/config_translation/src/FormElement/DateFormat.php - - message: "#^Method Drupal\\\\contact\\\\ContactFormEditForm\\:\\:save\\(\\) should return int but return statement is missing\\.$#" count: 1 @@ -1118,11 +1088,6 @@ parameters: count: 1 path: modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php - - - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 1 - path: modules/datetime/src/Plugin/Field/FieldType/DateTimeItem.php - - message: "#^Variable \\$date_part_order might not be defined\\.$#" count: 3 @@ -1148,11 +1113,6 @@ parameters: count: 1 path: modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php - - - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 1 - path: modules/datetime_range/src/Plugin/Field/FieldType/DateRangeItem.php - - message: "#^Variable \\$view in isset\\(\\) always exists and is not nullable\\.$#" count: 1 @@ -1605,11 +1565,6 @@ parameters: count: 1 path: modules/migrate_drupal/tests/src/Unit/MigrationConfigurationTraitTest.php - - - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 2 - path: modules/migrate_drupal_ui/src/Batch/MigrateUpgradeImportBatch.php - - message: "#^Variable \\$connection might not be defined\\.$#" count: 2 @@ -2368,11 +2323,6 @@ parameters: count: 1 path: modules/views/src/Plugin/views/wizard/WizardPluginBase.php - - - message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:11\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#" - count: 1 - path: modules/views/src/ViewExecutable.php - - message: "#^Variable \\$view in isset\\(\\) always exists and is not nullable\\.$#" count: 2 diff --git a/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php b/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php index 904eeab89e7..59aa01a4ac6 100644 --- a/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php +++ b/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php @@ -27,7 +27,7 @@ class GarbageCollectionTest extends KernelTestBase { public function testGarbageCollection() { $collection = $this->randomMachineName(); $connection = Database::getConnection(); - $store = new DatabaseStorageExpirable($collection, new PhpSerialize(), $connection); + $store = new DatabaseStorageExpirable($collection, new PhpSerialize(), $connection, \Drupal::time()); // Insert some items and confirm that they're set. for ($i = 0; $i <= 3; $i++) {