diff --git a/composer.lock b/composer.lock index 831f747d3aa2fe79ae6405776aaf1978b71b0d92..0d53011af081a107b24424c613fb1cd8b9f77241 100644 --- a/composer.lock +++ b/composer.lock @@ -496,7 +496,7 @@ "dist": { "type": "path", "url": "core", - "reference": "7e8f42a2a16fa8db35c42d6ba0c7bcc9b3508588" + "reference": "21b66fd9624a275df8382db866b35362b38d1537" }, "require": { "asm89/stack-cors": "^2.3", @@ -526,6 +526,7 @@ "pear/archive_tar": "^1.4.14", "php": ">=8.3.0", "php-tuf/composer-stager": "^2.0", + "psr/clock": "^1.0", "psr/log": "^3.0", "revolt/event-loop": "^1.0", "sebastian/diff": "^4|^5", @@ -1580,6 +1581,54 @@ }, "time": "2021-02-03T23:26:27+00:00" }, + { + "name": "psr/clock", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/clock.git", + "reference": "e41a24703d4560fd0acb709162f73b8adfc3aa0d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/clock/zipball/e41a24703d4560fd0acb709162f73b8adfc3aa0d", + "reference": "e41a24703d4560fd0acb709162f73b8adfc3aa0d", + "shasum": "" + }, + "require": { + "php": "^7.0 || ^8.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Psr\\Clock\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for reading the clock.", + "homepage": "https://github.com/php-fig/clock", + "keywords": [ + "clock", + "now", + "psr", + "psr-20", + "time" + ], + "support": { + "issues": "https://github.com/php-fig/clock/issues", + "source": "https://github.com/php-fig/clock/tree/1.0.0" + }, + "time": "2022-11-25T14:36:26+00:00" + }, { "name": "psr/container", "version": "2.0.2", @@ -9856,8 +9905,8 @@ }, "prefer-stable": true, "prefer-lowest": false, - "platform": [], - "platform-dev": [], + "platform": {}, + "platform-dev": {}, "platform-overrides": { "php": "8.3.0" }, diff --git a/composer/Metapackage/CoreRecommended/composer.json b/composer/Metapackage/CoreRecommended/composer.json index 695eec0d8ed5f882a5603fba19b3e56f924ef1ae..fa8c84eee091ed43923981b1edbcc902ffd2c7cf 100644 --- a/composer/Metapackage/CoreRecommended/composer.json +++ b/composer/Metapackage/CoreRecommended/composer.json @@ -25,6 +25,7 @@ "pear/pear_exception": "~v1.0.2", "php-tuf/composer-stager": "~v2.0.0", "psr/cache": "~3.0.0", + "psr/clock": "~1.0.0", "psr/container": "~2.0.2", "psr/event-dispatcher": "~1.0.0", "psr/http-client": "~1.0.3", diff --git a/core/composer.json b/core/composer.json index 2ce89dd0c0e926dccb5fc15b7eb4dd5c11ced944..0796da475ba5408e49555e1e8778fc71d5842194 100644 --- a/core/composer.json +++ b/core/composer.json @@ -47,6 +47,7 @@ "composer/semver": "^3.3", "asm89/stack-cors": "^2.3", "pear/archive_tar": "^1.4.14", + "psr/clock": "^1.0", "psr/log": "^3.0", "mck89/peast": "^1.14", "sebastian/diff": "^4|^5", diff --git a/core/core.services.yml b/core/core.services.yml index 55d8d2ebc9843e32969d54c5a341bd9242a96d86..9015b8e0c70a35ec5f9c829ce52fe4dd04b0332b 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1551,6 +1551,9 @@ services: class: Drupal\Core\Datetime\DateFormatter arguments: ['@entity_type.manager', '@language_manager', '@string_translation', '@config.factory', '@request_stack'] Drupal\Core\Datetime\DateFormatterInterface: '@date.formatter' + datetime.clock: + class: \Drupal\Component\Datetime\Clock + Psr\ClockInterface: '@datetime.clock' theme.manager: class: Drupal\Core\Theme\ThemeManager arguments: ['%app.root%', '@theme.negotiator', '@theme.initialization', '@module_handler'] diff --git a/core/lib/Drupal/Component/Datetime/Clock.php b/core/lib/Drupal/Component/Datetime/Clock.php new file mode 100644 index 0000000000000000000000000000000000000000..da31a15d3beefd7331e2de03dcd99cb8f2028b3a --- /dev/null +++ b/core/lib/Drupal/Component/Datetime/Clock.php @@ -0,0 +1,25 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Component\Datetime; + +use Psr\Clock\ClockInterface; + +/** + * A clock that returns the current system time. + */ +class Clock implements ClockInterface { + + public function __construct( + private readonly ?\DateTimeZone $timezone = NULL, + ) {} + + /** + * {@inheritdoc} + */ + public function now(): \DateTimeImmutable { + return new \DateTimeImmutable('now', $this->timezone); + } + +} diff --git a/core/lib/Drupal/Component/Datetime/LegacyClock.php b/core/lib/Drupal/Component/Datetime/LegacyClock.php new file mode 100644 index 0000000000000000000000000000000000000000..de7e8c01f84d62d873565278591b7a09d8ef91a2 --- /dev/null +++ b/core/lib/Drupal/Component/Datetime/LegacyClock.php @@ -0,0 +1,27 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Component\Datetime; + +use Psr\Clock\ClockInterface; + +/** + * A legacy clock that wraps TimeInterface to return the current system time. + * + * @internal + */ +class LegacyClock implements ClockInterface { + + public function __construct( + private readonly TimeInterface $time, + ) {} + + /** + * {@inheritdoc} + */ + public function now(): \DateTimeImmutable { + return new \DateTimeImmutable('@' . $this->time->getCurrentTime()); + } + +} diff --git a/core/modules/media/media.services.yml b/core/modules/media/media.services.yml index 0f3aeb6822e8c83fe871d4852b888e15651eeb51..61f6b1c7666d9125706c76c93ec6e414ed5448e3 100644 --- a/core/modules/media/media.services.yml +++ b/core/modules/media/media.services.yml @@ -13,7 +13,7 @@ services: Drupal\media\OEmbed\UrlResolverInterface: '@media.oembed.url_resolver' media.oembed.provider_repository: class: Drupal\media\OEmbed\ProviderRepository - arguments: ['@http_client', '@config.factory', '@datetime.time', '@keyvalue', '@logger.factory'] + arguments: ['@http_client', '@config.factory', '@datetime.clock', '@keyvalue', '@logger.factory'] Drupal\media\OEmbed\ProviderRepositoryInterface: '@media.oembed.provider_repository' media.oembed.resource_fetcher: class: Drupal\media\OEmbed\ResourceFetcher diff --git a/core/modules/media/src/OEmbed/ProviderRepository.php b/core/modules/media/src/OEmbed/ProviderRepository.php index 141c59720a1c7cdceaa0215465994ae9576cd4b3..001b6aff11f25e7294baa0bc02f73b52c7811655 100644 --- a/core/modules/media/src/OEmbed/ProviderRepository.php +++ b/core/modules/media/src/OEmbed/ProviderRepository.php @@ -2,12 +2,15 @@ namespace Drupal\media\OEmbed; +use Drupal\Component\Datetime\LegacyClock; use Drupal\Component\Datetime\TimeInterface; use Drupal\Component\Serialization\Json; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; use Drupal\Core\Logger\LoggerChannelFactoryInterface; use GuzzleHttp\ClientInterface; +use Psr\Clock\ClockInterface; use Psr\Http\Client\ClientExceptionInterface; /** @@ -15,6 +18,17 @@ */ class ProviderRepository implements ProviderRepositoryInterface { + use DeprecatedServicePropertyTrait; + + /** + * The deprecated properties. + * + * @var array<string, string> + */ + protected array $deprecatedProperties = [ + 'time' => 'datetime.time', + ]; + /** * How long the provider data should be cached, in seconds. * @@ -37,11 +51,9 @@ class ProviderRepository implements ProviderRepositoryInterface { protected $providersUrl; /** - * The time service. - * - * @var \Drupal\Component\Datetime\TimeInterface + * The clock. */ - protected $time; + protected ClockInterface $clock; /** * The key-value store. @@ -64,7 +76,7 @@ class ProviderRepository implements ProviderRepositoryInterface { * The HTTP client. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config factory service. - * @param \Drupal\Component\Datetime\TimeInterface $time + * @param \Drupal\Component\Datetime\TimeInterface|\Psr\Clock\ClockInterface $clock * The time service. * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory * The key-value store factory. @@ -73,10 +85,14 @@ class ProviderRepository implements ProviderRepositoryInterface { * @param int $max_age * (optional) How long the cache data should be kept. Defaults to a week. */ - public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, TimeInterface $time, KeyValueFactoryInterface $key_value_factory, LoggerChannelFactoryInterface $logger_factory, int $max_age = 604800) { + public function __construct(ClientInterface $http_client, ConfigFactoryInterface $config_factory, TimeInterface|ClockInterface $clock, KeyValueFactoryInterface $key_value_factory, LoggerChannelFactoryInterface $logger_factory, int $max_age = 604800) { $this->httpClient = $http_client; $this->providersUrl = $config_factory->get('media.settings')->get('oembed_providers_url'); - $this->time = $time; + if ($clock instanceof TimeInterface) { + @trigger_error(sprintf('Passing a $clock param as an instance of %s to %s() is deprecated in drupal:11.2.0 and is removed in drupal:12.0.0. Use Psr\Clock\ClockInterface instead. See https://www.drupal.org/node/3513300', TimeInterface::class, __METHOD__), E_USER_DEPRECATED); + $clock = new LegacyClock($clock); + } + $this->clock = $clock; $this->maxAge = $max_age; $this->keyValue = $key_value_factory->get('media'); $this->logger = $logger_factory->get('media'); @@ -86,7 +102,7 @@ public function __construct(ClientInterface $http_client, ConfigFactoryInterface * {@inheritdoc} */ public function getAll() { - $current_time = $this->time->getCurrentTime(); + $current_time = $this->clock->now()->getTimestamp(); $stored = $this->keyValue->get('oembed_providers'); // If we have stored data that hasn't yet expired, return that. We need to // store the data in a key-value store because, if the remote provider diff --git a/core/modules/media/tests/src/Unit/ProviderRepositoryTest.php b/core/modules/media/tests/src/Unit/ProviderRepositoryTest.php index 6070467bc8c04724cb1919bc79d7f10eb1418cc4..a34a3ce8cfee183cd89f5004a7effc8f6b2c2cb4 100644 --- a/core/modules/media/tests/src/Unit/ProviderRepositoryTest.php +++ b/core/modules/media/tests/src/Unit/ProviderRepositoryTest.php @@ -13,6 +13,7 @@ use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; +use Psr\Clock\ClockInterface; /** * @coversDefaultClass \Drupal\media\OEmbed\ProviderRepository @@ -72,8 +73,8 @@ protected function setUp(): void { $this->keyValue = $key_value_factory->get('media'); $this->currentTime = time(); - $time = $this->prophesize('\Drupal\Component\Datetime\TimeInterface'); - $time->getCurrentTime()->willReturn($this->currentTime); + $clock = $this->prophesize(ClockInterface::class); + $clock->now()->willReturn(new \DateTimeImmutable('@' . $this->currentTime)); $this->logger = $this->prophesize('\Psr\Log\LoggerInterface'); $logger_factory = $this->prophesize(LoggerChannelFactoryInterface::class); @@ -86,7 +87,7 @@ protected function setUp(): void { $this->repository = new ProviderRepository( $client, $config_factory, - $time->reveal(), + $clock->reveal(), $key_value_factory, $logger_factory->reveal() );