Skip to content
Snippets Groups Projects
Commit ddb29450 authored by catch's avatar catch
Browse files

Issue #3112295 by mondrake, Taran2L, acbramley, cliddell, mpdonadio,...

Issue #3112295 by mondrake, Taran2L, acbramley, cliddell, mpdonadio, smustgrave, Hardik_Patel_12, AkashKumar07, ravi.shankar, alexpott, andypost, daffie, JeroenT, vladbo, pifagor, voleger: Replace REQUEST_TIME in rest of OO code (except for tests)
parent 93b747d5
No related branches found
No related tags found
No related merge requests found
Showing
with 48 additions and 76 deletions
......@@ -290,11 +290,12 @@ public static function weekDaysOrdered($weekdays) {
*/
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);
......
......@@ -30,7 +30,7 @@ public function preSave() {
// 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 @@ public function preSave() {
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();
}
}
}
......
......@@ -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;
}
......
......@@ -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 @@ public function has($key) {
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 @@ public function getMultiple(array $keys) {
$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 @@ public function getAll() {
'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 @@ protected function doSetWithExpire($key, $value, $expire) {
])
->fields([
'value' => $this->serializer->encode($value),
'expire' => REQUEST_TIME + $expire,
'expire' => $this->time->getRequestTime() + $expire,
])
->execute();
}
......
......@@ -60,7 +60,7 @@ public function __construct(
*/
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];
}
......
......@@ -87,8 +87,9 @@ protected function doCreateItem($data) {
->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 @@ public function claimItem($lease_time = 30) {
}
// 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,
......
......@@ -112,7 +112,7 @@ public function write(string $key, string $content): void {
$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());
}
/**
......
......@@ -16,7 +16,7 @@ public function getTranslationElement(LanguageInterface $translation_language, $
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = \Drupal::service('date.formatter');
$description = $this->t('A user-defined date format. See the <a href="https://www.php.net/manual/datetime.format.php#refsect1-datetime.format-parameters">PHP manual</a> 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',
......
......@@ -113,7 +113,7 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
// 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);
}
......
......@@ -99,7 +99,7 @@ public static function generateSampleValue(FieldDefinitionInterface $field_defin
// 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);
......
......@@ -293,7 +293,8 @@ public static function finished($success, $results, $operations, $elapsed) {
*/
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 @@ public static function onPostImport(MigrateImportEvent $event) {
*/
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);
}
}
......
......@@ -1701,7 +1701,7 @@ public function preExecute($args = []) {
\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();
......
......@@ -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
......
......@@ -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++) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment