Skip to content

Make doctrine/cache optional #6740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
__METHOD__,
);

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

Check warning on line 88 in src/Cache/QueryCacheProfile.php

View check run for this annotation

Codecov / codecov/patch

src/Cache/QueryCacheProfile.php#L87-L88

Added lines #L87 - L88 were not covered by tests
}

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__,
));

Check warning on line 96 in src/Cache/QueryCacheProfile.php

View check run for this annotation

Codecov / codecov/patch

src/Cache/QueryCacheProfile.php#L91-L96

Added lines #L91 - L96 were not covered by tests
}

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

Check warning on line 99 in src/Cache/QueryCacheProfile.php

View check run for this annotation

Codecov / codecov/patch

src/Cache/QueryCacheProfile.php#L99

Added line #L99 was not covered by tests
}

/** @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 @@
__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__,
));

Check warning on line 141 in src/Configuration.php

View check run for this annotation

Codecov / codecov/patch

src/Configuration.php#L136-L141

Added lines #L136 - L141 were not covered by tests
}

return $this->resultCacheImpl;
}

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

Check warning on line 153 in src/Configuration.php

View check run for this annotation

Codecov / codecov/patch

src/Configuration.php#L152-L153

Added lines #L152 - L153 were not covered by tests
}

$this->resultCache = $cache;

Check warning on line 156 in src/Configuration.php

View check run for this annotation

Codecov / codecov/patch

src/Configuration.php#L156

Added line #L156 was not covered by tests
}

/**
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