diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index 69bbb922e5..da7334f914 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -82,9 +82,6 @@ class DatabaseBackend implements CacheBackendInterface { * table. * @param \Drupal\Component\Serialization\ObjectAwareSerializationInterface $serializer * The serializer to use. - * - * @todo Make $serializer parameter mandatory in Drupal 9.x, in - * https://www.drupal.org/project/drupal/issues/3014548. */ public function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider, $bin, $max_rows = NULL, ObjectAwareSerializationInterface $serializer = NULL) { // All cache tables should be prefixed with 'cache_'. @@ -94,7 +91,13 @@ public function __construct(Connection $connection, CacheTagsChecksumInterface $ $this->connection = $connection; $this->checksumProvider = $checksum_provider; $this->maxRows = $max_rows === NULL ? static::DEFAULT_MAX_ROWS : $max_rows; - $this->serializer = $serializer ?: \Drupal::service('serialization.phpserialize'); + if ($serializer) { + $this->serializer = $serializer; + } + else { + @trigger_error('The serializer service will be a mandatory parameter in Drupal 9.0.x. See https://www.drupal.org/node/3014688.', E_USER_DEPRECATED); + $this->serializer = \Drupal::service('serialization.phpserialize'); + } } /** diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php index 80a068a6e6..ec1639ecea 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackendFactory.php @@ -49,15 +49,18 @@ class DatabaseBackendFactory implements CacheFactoryInterface { * The serializer to use. * * @throws \BadMethodCallException - * - * @todo Make $serializer parameter mandatory in Drupal 9.x, in - * https://www.drupal.org/project/drupal/issues/3014548. */ public function __construct(Connection $connection, CacheTagsChecksumInterface $checksum_provider, Settings $settings = NULL, ObjectAwareSerializationInterface $serializer = NULL) { $this->connection = $connection; $this->checksumProvider = $checksum_provider; $this->settings = $settings ?: Settings::getInstance(); - $this->serializer = $serializer ?: \Drupal::service('serialization.phpserialize'); + if ($serializer) { + $this->serializer = $serializer; + } + else { + @trigger_error('The serializer service will be a mandatory parameter in Drupal 9.0.x. See https://www.drupal.org/node/3014688.', E_USER_DEPRECATED); + $this->serializer = \Drupal::service('serialization.phpserialize'); + } } /** diff --git a/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php b/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php index 35a7d4f982..393ceab562 100644 --- a/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php +++ b/core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php @@ -89,6 +89,16 @@ public function testGarbageCollection() { $this->assertFalse($backend->get('test0')); } + /** + * Tests the deprecation error triggering when the serializer param is missed. + * + * @group legacy + * @expectedDeprecation The serializer service will be a mandatory parameter in Drupal 9.0.x. See https://www.drupal.org/node/3014688. + */ + public function testDeprecationErrorTriggering() { + new DatabaseBackend($this->container->get('database'), $this->container->get('cache_tags.invalidator.checksum'), 'default', static::$maxRows); + } + /** * Gets the number of rows in the test cache bin database table. * diff --git a/core/tests/Drupal/Tests/Core/Cache/DatabaseBackendFactoryTest.php b/core/tests/Drupal/Tests/Core/Cache/DatabaseBackendFactoryTest.php index a720fc70ea..77823a66a7 100644 --- a/core/tests/Drupal/Tests/Core/Cache/DatabaseBackendFactoryTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/DatabaseBackendFactoryTest.php @@ -7,8 +7,10 @@ use Drupal\Core\Cache\DatabaseBackend; use Drupal\Core\Cache\DatabaseBackendFactory; use Drupal\Core\Database\Connection; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Site\Settings; use Drupal\Tests\UnitTestCase; +use Symfony\Component\DependencyInjection\Definition; /** * @coversDefaultClass \Drupal\Core\Cache\DatabaseBackendFactory @@ -33,6 +35,23 @@ public function testGet(array $settings, $expected_max_rows_foo, $expected_max_r $this->assertSame($expected_max_rows_bar, $database_backend_factory->get('bar')->getMaxRows()); } + /** + * Tests the deprecation error triggering when the serializer param is missed. + * + * @group legacy + * @expectedDeprecation The serializer service will be a mandatory parameter in Drupal 9.0.x. See https://www.drupal.org/node/3014688. + */ + public function testDeprecationErrorTriggering() { + $container = new ContainerBuilder(); + $container->setDefinition('serialization.phpserialize', new Definition(new PhpSerialize())); + \Drupal::setContainer($container); + new DatabaseBackendFactory( + $this->prophesize(Connection::class)->reveal(), + $this->prophesize(CacheTagsChecksumInterface::class)->reveal(), + new Settings([]) + ); + } + public function getProvider() { return [ 'default' => [