Skip to content

Commit

Permalink
Make doctrine/cache optional (#6740)
Browse files Browse the repository at this point in the history
|      Q       |   A
|------------- | -----------
| Type         | improvement
| Fixed issues | N/A

#### Summary

We'd like to flag the the doctrine/cache package as abandoned which
means it needs to disappear from the list of mandatory dependencies of
DBAL. In DBAL 3.9, the doctrine/cache classes are used for deprecated
code paths only.

Note: This change could break downstream projects if they rely on DBAL
installing the Cache package for them.
  • Loading branch information
derrabus authored Jan 28, 2025
1 parent d154c50 commit 818eae9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
11 changes: 8 additions & 3 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ jobs:
dependencies:
- "highest"
extension:
- "sqlite3"
- "pdo_sqlite"
include:
- os: "ubuntu-20.04"
php-version: "7.4"
dependencies: "lowest"
extension: "pdo_sqlite"
- os: "ubuntu-22.04"
php-version: "7.4"
dependencies: "highest"
php-version: "8.4"
dependencies: "minimal"
extension: "sqlite3"

steps:
Expand All @@ -60,6 +61,10 @@ jobs:
with:
fetch-depth: 2

- name: "Remove optional dependencies"
run: "composer remove --no-update --dev doctrine/cache"
if: "${{ matrix.dependencies == 'minimal' }}"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
Expand All @@ -71,7 +76,7 @@ jobs:
uses: "ramsey/composer-install@v3"
with:
composer-options: "--ignore-platform-req=php+"
dependency-versions: "${{ matrix.dependencies }}"
dependency-versions: "${{ matrix.dependencies == 'minimal' && 'highest' || matrix.dependencies }}"

- name: "Print SQLite version"
run: >
Expand Down
6 changes: 6 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ awareness about deprecated code.
- Use of our low-overhead runtime deprecation API, details:
https://github.com/doctrine/deprecations/

# Upgrade to 3.10

The `doctrine/cache` package is now an optional dependency. If you are using the
`Doctrine\DBAL\Cache` classes, you need to require the `doctrine/cache` package
explicitly.

# Upgrade to 3.8

## Deprecated lock-related `AbstractPlatform` methods
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
"require": {
"php": "^7.4 || ^8.0",
"composer-runtime-api": "^2",
"doctrine/cache": "^1.11|^2.0",
"doctrine/deprecations": "^0.5.3|^1",
"doctrine/event-manager": "^1|^2",
"psr/cache": "^1|^2|^3",
"psr/log": "^1|^2|^3"
},
"require-dev": {
"doctrine/cache": "^1.11|^2.0",
"doctrine/coding-standard": "12.0.0",
"fig/log-test": "^1",
"jetbrains/phpstorm-stubs": "2023.1",
Expand All @@ -51,6 +51,9 @@
"symfony/cache": "^5.4|^6.0|^7.0",
"symfony/console": "^4.4|^5.4|^6.0|^7.0"
},
"conflict": {
"doctrine/cache": "< 1.11"
},
"suggest": {
"symfony/console": "For helpful console commands such as SQL execution and import of files."
},
Expand Down
16 changes: 15 additions & 1 deletion src/Cache/QueryCacheProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;
use Psr\Cache\CacheItemPoolInterface;
use RuntimeException;
use TypeError;

use function class_exists;
use function get_class;
use function hash;
use function serialize;
Expand Down Expand Up @@ -82,7 +84,19 @@ public function getResultCacheDriver()
__METHOD__,
);

return $this->resultCache !== null ? DoctrineProvider::wrap($this->resultCache) : null;
if ($this->resultCache === null) {
return null;
}

if (! class_exists(DoctrineProvider::class)) {
throw new RuntimeException(sprintf(
'Calling %s() is not supported if the doctrine/cache package is not installed. '
. 'Try running "composer require doctrine/cache" or migrate cache access to PSR-6.',
__METHOD__,
));
}

return DoctrineProvider::wrap($this->resultCache);
}

/** @return int */
Expand Down
19 changes: 17 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
use Doctrine\DBAL\Schema\SchemaManagerFactory;
use Doctrine\Deprecations\Deprecation;
use Psr\Cache\CacheItemPoolInterface;
use RuntimeException;

use function class_exists;
use function func_num_args;
use function interface_exists;
use function sprintf;

/**
* Configuration container for the Doctrine DBAL.
Expand Down Expand Up @@ -129,6 +133,14 @@ public function getResultCacheImpl(): ?Cache
__METHOD__,
);

if ($this->resultCache !== null && ! interface_exists(Cache::class)) {
throw new RuntimeException(sprintf(
'Calling %s() is not supported if the doctrine/cache package is not installed. '
. 'Try running "composer require doctrine/cache" or migrate cache access to PSR-6.',
__METHOD__,
));
}

return $this->resultCacheImpl;
}

Expand All @@ -137,8 +149,11 @@ public function getResultCacheImpl(): ?Cache
*/
public function setResultCache(CacheItemPoolInterface $cache): void
{
$this->resultCacheImpl = DoctrineProvider::wrap($cache);
$this->resultCache = $cache;
if (class_exists(DoctrineProvider::class)) {
$this->resultCacheImpl = DoctrineProvider::wrap($cache);
}

$this->resultCache = $cache;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/Connection/CachedQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function testCachedQueryLegacy(): void

public function testCachedQueryLegacyWrapped(): void
{
if (! class_exists(DoctrineProvider::class)) {
self::markTestSkipped('This test requires the doctrine/cache package.');
}

$cache = new ArrayAdapter();
$legacy = DoctrineProvider::wrap($cache);
$this->assertCachedQueryIsExecutedOnceAndYieldsTheSameResult($legacy, __FUNCTION__);
Expand Down

0 comments on commit 818eae9

Please sign in to comment.