From c0afd1267c455ab38f870935ec4dec02275938a1 Mon Sep 17 00:00:00 2001 From: bidi Date: Tue, 10 Nov 2026 00:43:45 +0200 Subject: [PATCH 1/2] update composer, docs, workflow, tests Signed-off-by: bidi --- .github/workflows/codecov.yml | 8 +- .github/workflows/static-analysis.yml | 12 +- .gitignore | 1 + CHANGELOG.md | 356 ++++++++++++++++-- README.md | 50 +-- SECURITY.md | 8 +- composer.json | 8 +- docs/book/v4/overview.md | 3 + docs/book/v4/usage.md | 3 +- docs/book/v5/adapters/filesystem.md | 44 +++ docs/book/v5/adapters/in-memory.md | 29 ++ docs/book/v5/configuration.md | 74 ++++ docs/book/v5/installation.md | 26 ++ docs/book/v5/overview.md | 24 ++ docs/book/v5/upgrading.md | 50 +++ docs/book/v5/usage.md | 14 + mkdocs.yml | 12 +- phpstan.neon | 2 +- phpunit.xml | 38 +- src/ConfigProvider.php | 14 +- src/Exception/InvalidConfigException.php | 51 +++ src/Factory/FilesystemAdapterFactory.php | 117 +++++- test/ArrayAdapterTest.php | 11 +- test/ConfigProviderTest.php | 35 +- test/Factory/FilesystemAdapterFactoryTest.php | 221 +++++++++++ test/FilesystemAdapterTest.php | 27 +- test/Fixture/ArrayContainer.php | 37 ++ test/Fixture/ServiceNotFoundException.php | 12 + test/Fixture/TemporaryDirectory.php | 51 +++ 29 files changed, 1223 insertions(+), 115 deletions(-) create mode 100644 docs/book/v5/adapters/filesystem.md create mode 100644 docs/book/v5/adapters/in-memory.md create mode 100644 docs/book/v5/configuration.md create mode 100644 docs/book/v5/installation.md create mode 100644 docs/book/v5/overview.md create mode 100644 docs/book/v5/upgrading.md create mode 100644 docs/book/v5/usage.md create mode 100644 src/Exception/InvalidConfigException.php create mode 100644 test/Factory/FilesystemAdapterFactoryTest.php create mode 100644 test/Fixture/ArrayContainer.php create mode 100644 test/Fixture/ServiceNotFoundException.php create mode 100644 test/Fixture/TemporaryDirectory.php diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index c0fbc53..9652006 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -15,14 +15,14 @@ jobs: - ubuntu-latest php: - - "8.2" - "8.3" - "8.4" - "8.5" + - "8.6" steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v7 - name: Install PHP uses: shivammathur/setup-php@v2 @@ -36,7 +36,7 @@ jobs: run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV - name: Cache dependencies installed with composer - uses: actions/cache@v3 + uses: actions/cache@v6 with: path: ${{ env.COMPOSER_CACHE_DIR }} key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} @@ -50,6 +50,6 @@ jobs: run: vendor/bin/phpunit --colors=always --coverage-clover clover.xml - name: Send code coverage report to Codecov.io - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v7 with: token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml index 9976515..b1cb7d5 100644 --- a/.github/workflows/static-analysis.yml +++ b/.github/workflows/static-analysis.yml @@ -4,7 +4,7 @@ on: name: Run PHPStan checks jobs: - mutation: + static-analysis: name: PHPStan ${{ matrix.php }}-${{ matrix.os }} runs-on: ${{ matrix.os }} @@ -15,20 +15,20 @@ jobs: - ubuntu-latest php: - - "8.2" - "8.3" - "8.4" - "8.5" + - "8.6" steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v7 - name: Install PHP uses: shivammathur/setup-php@v2 with: php-version: "${{ matrix.php }}" - coverage: pcov + coverage: none ini-values: assert.exception=1, zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On tools: composer:v2, cs2pr @@ -36,7 +36,7 @@ jobs: run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV - name: Cache dependencies installed with composer - uses: actions/cache@v4 + uses: actions/cache@v6 with: path: ${{ env.COMPOSER_CACHE_DIR }} key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }} @@ -47,4 +47,4 @@ jobs: run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi - name: Run static analysis with PHPStan - run: vendor/bin/phpstan analyse + run: vendor/bin/phpstan analyse diff --git a/.gitignore b/.gitignore index 89c8bf0..2d743d7 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,4 @@ composer.phar composer.lock .phpcs-cache .phpunit.result.cache +.phpunit.cache diff --git a/CHANGELOG.md b/CHANGELOG.md index 65a8b08..445dd6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,84 +1,392 @@ +# Changelog + +## 5.0.0 - TBD + +### Changed + +- PHP requirement is now `~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0` +- `symfony/cache` requirement is now `^7.4 || ^8.1` (Symfony `8.0.x` is skipped) +- `FilesystemAdapterFactory` throws `Dot\Cache\Exception\InvalidConfigException` when `doctrine.cache.filesystem.directory` is missing, instead of silently caching in the system temp directory +- `FilesystemAdapterFactory` validates `namespace`, `default_lifetime` and `marshaller` and throws `InvalidConfigException` naming the key, instead of a PHP `TypeError` +- `ConfigProvider` and `FilesystemAdapterFactory` are now `final` +- PHPUnit `^12`, PHPStan level 8, PHPUnit fails on deprecations, notices, warnings and risky tests +- CI: GitHub Actions bumped to `actions/checkout@v7`, `actions/cache@v6`, `codecov/codecov-action@v7` + +### Added + +- Support for PHP 8.6 +- Support for `symfony/cache` 8 +- `default_lifetime` config key for the filesystem adapter +- `marshaller` config key accepts a container service name +- `Dot\Cache\Exception\InvalidConfigException` +- Explicit `psr/container` requirement +- `v5` documentation and the "Upgrading to 5.x" guide + +### Deprecated + +- `default_life_time` config key, use `default_lifetime` + +### Removed + +- Support for PHP 8.2 +- Support for `symfony/cache` 7.0 to 7.3 + +### Fixed + +- `default_lifetime` was silently ignored by `FilesystemAdapterFactory` +- `TypeError` when `default_lifetime` is a numeric string, or `marshaller` is a service name +- README configuration example did not parse +- Test namespace now matches the `autoload-dev` PSR-4 prefix (`DotTest\Cache`) + +## 4.4.0 - 2025-10-30 + +### Changed + +- Nothing + +### Added + +- Support for PHP 8.5 + +### Deprecated + +- Nothing + +### Removed + +- Nothing + +### Fixed + +- Nothing + +## 4.3.0 - 2025-02-28 + +### Changed + +- Replaced Psalm with PHPStan + +### Added + +- Nothing + +### Deprecated + +- Nothing + +### Removed + +- Nothing + +### Fixed + +- Nothing + +## 4.2.1 - 2025-01-27 + +### Changed + +- Bumped Psalm to 6.x +- Codecov also runs on PHP 8.4 + +### Added + +- Nothing + +### Deprecated + +- Nothing + +### Removed + +- Nothing + +### Fixed + +- Nothing + +## 4.2.0 - 2025-01-23 + +### Changed + +- `laminas/laminas-coding-standard` `^3.0` +- Documentation updates + +### Added + +- Support for PHP 8.4 + +### Deprecated + +- Nothing + +### Removed + +- Nothing + +### Fixed + +- Nothing + +## 4.1.0 - 2024-05-03 + +### Changed + +- Documentation linting + +### Added + +- `mkdocs.yml` and the documentation site + +### Deprecated + +- Nothing + +### Removed + +- Nothing + +### Fixed + +- Nothing + ## 4.0.0 - 2024-02-22 ### Changed -* removed support for PHP 8.1 -* removed support for laminas/laminas-cache + +- Replaced `laminas/laminas-cache` with `symfony/cache`; `3.x` is the last version that uses `laminas/laminas-cache` +- Removed support for PHP 8.1 ### Added -* added support for PHP 8.3 + +- Support for PHP 8.3 ### Deprecated -* laminas/laminas-cache + +- Nothing ### Removed -* Nothing + +- `laminas/laminas-cache` ### Fixed -* Nothing + +- Nothing + +## 3.1.3 - 2024-01-10 + +### Changed + +- Updated dependencies + +### Added + +- Support for PHP 8.3 + +### Deprecated + +- Nothing + +### Removed + +- Support for PHP 8.0 + +### Fixed + +- Nothing + +## 3.1.2 - 2023-08-18 + +### Changed + +- PHPUnit `^10.3.2` +- `laminas/laminas-servicemanager` `^3.21.0` + +### Added + +- Support for PHP 8.2 + +### Deprecated + +- Nothing + +### Removed + +- Support for PHP 7.4 + +### Fixed + +- Nothing + +## 3.1.1 - 2023-04-10 + +### Changed + +- Nothing + +### Added + +- Suggests `phpfastcache/phpfastcache` + +### Deprecated + +- Package marked as abandoned + +### Removed + +- Nothing + +### Fixed + +- Nothing + +## 3.1.0 - 2023-03-29 + +### Changed + +- Replaced the `laminas/laminas-cache` requirement with the `laminas-cache` filesystem, session and memory storage adapters (still built on `laminas/laminas-cache`) + +### Added + +- Nothing + +### Deprecated + +- Nothing + +### Removed + +- `laminas/laminas-dependency-plugin` + +### Fixed + +- Nothing + +## 3.0.0 - 2022-04-06 + +### Changed + +- PHP requirement is now `~7.4.0 || ~8.0.0 || ~8.1.0` +- `laminas/laminas-cache` `^3.1`, `laminas/laminas-servicemanager` `^3.10` +- PHPUnit `^9.5` + +### Added + +- Nothing + +### Deprecated + +- Nothing + +### Removed + +- Nothing + +### Fixed + +- Nothing + +## 2.0.0 - 2020-01-30 + +### Changed + +- Migrated from Zend Framework to Laminas and from Expressive to Mezzio +- PHP requirement is now `^7.2` + +### Added + +- Nothing + +### Deprecated + +- Nothing + +### Removed + +- Nothing + +### Fixed + +- Nothing ## 1.1.1 - 2018-05-03 ### Changed -* updated required dependencies + +- Updated required dependencies ### Added -* Nothing + +- Nothing ### Deprecated -* Nothing + +- Nothing ### Removed -* Nothing + +- Nothing ### Fixed -* Nothing +- Nothing ## 1.1.0 - 2017-03-15 ### Changed -* Updated factories to use PSR11 container + +- Updated factories to use PSR-11 container ### Added -* Nothing + +- Nothing ### Deprecated -* Nothing + +- Nothing ### Removed -* Nothing + +- Nothing ### Fixed -* Nothing +- Nothing ## 1.0.1 - 2017-03-10 ### Added -* Updated php file headers doc blocks + +- Updated PHP file header docblocks ### Deprecated -* Nothing + +- Nothing ### Removed -* Nothing + +- Nothing ### Fixed -* Nothing +- Nothing ## 1.0.0 - 2017-03-08 Initial tagged release ### Added -* Everything + +- Everything ### Deprecated -* Nothing + +- Nothing ### Removed -* Nothing + +- Nothing ### Fixed -* Nothing + +- Nothing diff --git a/README.md b/README.md index 6b5efcb..0991a6c 100644 --- a/README.md +++ b/README.md @@ -4,23 +4,36 @@ Dotkernel cache component based on symfony-cache. > dot-cache is a wrapper on top of [symfony/cache](https://github.com/symfony/cache) +> `dot-cache` uses [symfony/cache](https://github.com/symfony/cache) since version `4.0`. +> Version `3.x` of `dot-cache` is the last version that uses [laminas/laminas-cache](https://github.com/laminas/laminas-cache). + ## Documentation Documentation is available at: https://docs.dotkernel.org/dot-cache/. +## Versions + +| Version | Supported | PHP | Cache backend | +| --- | --- | --- | --- | +| 5.x | :white_check_mark: | 8.3 to 8.6 | `symfony/cache` 7.4 or 8.1+ | +| 4.x | Security fixes only | 8.2 to 8.5 | `symfony/cache` 7.x | +| <= 3.x | :x: | | `laminas/laminas-cache` | + +Upgrading from `4.x`? Read the [upgrade guide](https://docs.dotkernel.org/dot-cache/v5/upgrading/). + ## Badges ![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-cache) -![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-cache/4.4.0) +![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-cache/5.0.0) [![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-cache)](https://github.com/dotkernel/dot-cache/issues) [![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-cache)](https://github.com/dotkernel/dot-cache/network) [![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-cache)](https://github.com/dotkernel/dot-cache/stargazers) -[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-cache)](https://github.com/dotkernel/dot-cache/blob/4.0/LICENSE.md) +[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-cache)](https://github.com/dotkernel/dot-cache/blob/5.0/LICENSE.md) -[![Build Static](https://github.com/dotkernel/dot-cache/actions/workflows/continuous-integration.yml/badge.svg?branch=4.0)](https://github.com/dotkernel/dot-cache/actions/workflows/continuous-integration.yml) +[![Build Static](https://github.com/dotkernel/dot-cache/actions/workflows/continuous-integration.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/dot-cache/actions/workflows/continuous-integration.yml) [![codecov](https://codecov.io/gh/dotkernel/dot-cache/graph/badge.svg?token=FAN1MXKKS9)](https://codecov.io/gh/dotkernel/dot-cache) -[![PHPStan](https://github.com/dotkernel/dot-cache/actions/workflows/static-analysis.yml/badge.svg?branch=4.0)](https://github.com/dotkernel/dot-cache/actions/workflows/static-analysis.yml) +[![PHPStan](https://github.com/dotkernel/dot-cache/actions/workflows/static-analysis.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/dot-cache/actions/workflows/static-analysis.yml) > This package supports only `array` and `filesystem` adapters, you can use multiple adapters at once. @@ -34,7 +47,7 @@ composer require dotkernel/dot-cache After installing, add the `Dot\Cache\ConfigProvider::class` class to your configuration aggregate. -## Configuration for Doctrine in_array +## Configuration for Doctrine in-memory cache In `config\autoload\doctrine.global.php` you need to add the following configurations: @@ -72,13 +85,15 @@ If you want to store the cache into files on your local disk, you will need to u **The `filesystem` adapter needs some extra configurations:** -* directory (folder path) -* namespace (directory name) +- `directory` (required): folder path +- `namespace`: directory name +- `default_lifetime`: item lifetime in seconds, `0` for no expiry +- `marshaller`: service name or instance of a Symfony marshaller ```php 'cache' => [ - 'array' => [ - 'class' => \Dot\Cache\Adapter\ArrayAdapter::class, + 'array' => [ + 'class' => \Dot\Cache\Adapter\ArrayAdapter::class, ], 'filesystem' => [ 'class' => \Dot\Cache\Adapter\FilesystemAdapter::class, @@ -92,18 +107,11 @@ You can store `result_cache`, `metadata_cache`, `query_cache`, `hydration_cache` ## Configuration example for both in-memory and filesystem adapters -Configuration sample for `config\autoload\doctrine.global.php` file: +Configuration sample for `config\autoload\doctrine.global.php` file. The `dependencies` are registered by `Dot\Cache\ConfigProvider`, so they are not repeated here. ```php return [ - 'dependencies' => [ - 'factories' => [ - \Dot\Cache\Adapter\FilesystemAdapter::class => \Dot\Cache\Factory\FilesystemAdapterFactory::class, - 'aliases' => [ - \Symfony\Component\Cache\Adapter\FilesystemAdapter::class => \Dot\Cache\Adapter\FilesystemAdapter::class - ], - ], - 'doctrine' => [ + 'doctrine' => [ 'configuration' => [ 'orm_default' => [ 'result_cache' => 'array', @@ -119,9 +127,9 @@ return [ ], ], ], - 'cache' => [ - 'array' => [ - 'class' => \Symfony\Component\Cache\Adapter\ArrayAdapter::class, + 'cache' => [ + 'array' => [ + 'class' => \Dot\Cache\Adapter\ArrayAdapter::class, ], 'filesystem' => [ 'class' => \Dot\Cache\Adapter\FilesystemAdapter::class, diff --git a/SECURITY.md b/SECURITY.md index 30c7e55..4046cae 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -2,13 +2,12 @@ ## Supported Versions - | Version | Supported | PHP Version | |---------|--------------------|-----------------------------------------------------------------------------------------------------------| -| 4.x | :white_check_mark: | ![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-cache/4.4.0) | +| 5.x | :white_check_mark: | ![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-cache/5.0.0) | +| 4.x | Security fixes | ![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-cache/4.4.0) | | <= 3.x | :x: | | - ## Reporting Potential Security Issues If you have encountered a potential security vulnerability in this project, @@ -27,7 +26,6 @@ release prior to any public exposure; this helps protect the project's users, and provides them with a chance to upgrade and/or update in order to protect their applications. - ## Policy If we verify a reported security vulnerability, our policy is: @@ -36,4 +34,4 @@ If we verify a reported security vulnerability, our policy is: release branch. - After patching the release branches, we will immediately issue new security - fix releases for each patched release branch. \ No newline at end of file + fix releases for each patched release branch. diff --git a/composer.json b/composer.json index 7dcfed9..b9a7968 100644 --- a/composer.json +++ b/composer.json @@ -15,18 +15,18 @@ "cache", "services", "doctrine", - "doctrine-cache", "symfony-cache" ], "require": { - "php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0", - "symfony/cache": "^7.0" + "php": "~8.3.0 || ~8.4.0 || ~8.5.0 || ~8.6.0", + "symfony/cache": "^7.4 || ^8.1", + "psr/container": "^1.1 || ^2.0" }, "require-dev": { "laminas/laminas-coding-standard": "^3.0", "phpstan/phpstan": "^2.1", "phpstan/phpstan-phpunit": "^2.0", - "phpunit/phpunit": "^10.5.5" + "phpunit/phpunit": "^12" }, "autoload": { "psr-4": { diff --git a/docs/book/v4/overview.md b/docs/book/v4/overview.md index 6592ca8..5e3f76b 100644 --- a/docs/book/v4/overview.md +++ b/docs/book/v4/overview.md @@ -4,6 +4,9 @@ Dotkernel cache component based on symfony-cache. > dot-cache is a wrapper on top of [symfony/cache](https://github.com/symfony/cache) +> `dot-cache` uses [symfony/cache](https://github.com/symfony/cache) since version `4.0`. +> Version `3.x` of `dot-cache` is the last version that uses [laminas/laminas-cache](https://github.com/laminas/laminas-cache). + Using the in-memory and/or filesystem cache adapters greatly improves overall application response times. ## Badges diff --git a/docs/book/v4/usage.md b/docs/book/v4/usage.md index e47ddb1..3cb0155 100644 --- a/docs/book/v4/usage.md +++ b/docs/book/v4/usage.md @@ -1,6 +1,7 @@ # Usage -Version `4.x` of `dot-cache` is the last version that uses [symfony/cache](https://github.com/symfony/cache) to store cached data. +> `dot-cache` uses [symfony/cache](https://github.com/symfony/cache) since version `4.0`. +> Version `3.x` of `dot-cache` is the last version that uses [laminas/laminas-cache](https://github.com/laminas/laminas-cache). Using cache when querying the database: diff --git a/docs/book/v5/adapters/filesystem.md b/docs/book/v5/adapters/filesystem.md new file mode 100644 index 0000000..0643fed --- /dev/null +++ b/docs/book/v5/adapters/filesystem.md @@ -0,0 +1,44 @@ +# Configuring the filesystem adapter + +**The `filesystem` adapter stores the cache on the hard disk.** + +In `config\autoload\doctrine.global.php` you need to add the following configurations, under the `doctrine` key: + +```php +'cache' => [ + 'filesystem' => [ + 'class' => \Dot\Cache\Adapter\FilesystemAdapter::class, + 'directory' => getcwd() . '/data/cache', + 'namespace' => 'doctrine', + ], +], +``` + +The factory reads its settings from the `doctrine.cache.filesystem` entry, so keep that name. + +| Key | Required | Default | Description | +| --- | --- | --- | --- | +| `directory` | Yes | none | Path where cache files are stored | +| `namespace` | No | `''` | Sub-directory inside `directory` | +| `default_lifetime` | No | `0` | Lifetime of cache items in seconds; `0` means no expiry. Integer or numeric string | +| `marshaller` | No | Symfony's `DefaultMarshaller` | Service name or instance of `Symfony\Component\Cache\Marshaller\MarshallerInterface` | + +> The key `default_life_time` is still read for backwards compatibility, but it is deprecated. Use `default_lifetime`. + +If `directory` is missing or empty, or any key has the wrong type, the factory throws `Dot\Cache\Exception\InvalidConfigException` naming the key. + +Under the `doctrine.configuration.orm_default` key add the following config: + +```php +'result_cache' => 'filesystem', +'metadata_cache' => 'filesystem', +'query_cache' => 'filesystem', +'hydration_cache' => 'filesystem', +'second_level_cache' => [ + 'enabled' => true, + 'default_lifetime' => 3600, + 'default_lock_lifetime' => 60, + 'file_lock_region_directory' => '', + 'regions' => [], +], +``` diff --git a/docs/book/v5/adapters/in-memory.md b/docs/book/v5/adapters/in-memory.md new file mode 100644 index 0000000..7e5b8ee --- /dev/null +++ b/docs/book/v5/adapters/in-memory.md @@ -0,0 +1,29 @@ +# Configuring the in-memory adapter + +**The `array` adapter stores the cache in-memory.** The cache lives only for the current PHP process, so it is not shared between requests. + +In `config\autoload\doctrine.global.php` you need to add the following configurations, under the `doctrine` key: + +```php +'cache' => [ + 'array' => [ + 'class' => \Dot\Cache\Adapter\ArrayAdapter::class, + ], +], +``` + +Under the `doctrine.configuration.orm_default` key add the following config: + +```php +'result_cache' => 'array', +'metadata_cache' => 'array', +'query_cache' => 'array', +'hydration_cache' => 'array', +'second_level_cache' => [ + 'enabled' => true, + 'default_lifetime' => 3600, + 'default_lock_lifetime' => 60, + 'file_lock_region_directory' => '', + 'regions' => [], +], +``` diff --git a/docs/book/v5/configuration.md b/docs/book/v5/configuration.md new file mode 100644 index 0000000..685772e --- /dev/null +++ b/docs/book/v5/configuration.md @@ -0,0 +1,74 @@ +# Configuration + +After installation, register `dot-cache` package in your project by adding the below line to your configuration aggregator (usually: `config/config.php`): + +```php +Dot\Cache\ConfigProvider::class, +``` + +In `config\autoload\doctrine.global.php` you need to add the following configurations: + +Under the `doctrine.configuration.orm_default` key add the following config: + +```php +'result_cache' => 'array', +'metadata_cache' => 'array', +'query_cache' => 'array', +'hydration_cache' => 'array', +'second_level_cache' => [ + 'enabled' => true, + 'default_lifetime' => 3600, + 'default_lock_lifetime' => 60, + 'file_lock_region_directory' => '', + 'regions' => [], +], +``` + +Next, under the `doctrine` key add the following config: + +```php +'cache' => [ + 'array' => [ + 'class' => \Dot\Cache\Adapter\ArrayAdapter::class, + ], +], +``` + +> The above configuration is just a sample and will use the in-memory adapter, it should be adapted to the app's needs. + +> To disable the cache make sure the `enabled` key is `false`. + +## Using both adapters at the same time + +You can use both adapters at the same time. Register both under the `doctrine.cache` key: + +```php +'cache' => [ + 'array' => [ + 'class' => \Dot\Cache\Adapter\ArrayAdapter::class, + ], + 'filesystem' => [ + 'class' => \Dot\Cache\Adapter\FilesystemAdapter::class, + 'directory' => getcwd() . '/data/cache', + 'namespace' => 'doctrine', + ], +], +``` + +Then pick one per cache under `doctrine.configuration.orm_default`: + +```php +'result_cache' => 'filesystem', +'metadata_cache' => 'array', +'query_cache' => 'filesystem', +'hydration_cache' => 'array', +'second_level_cache' => [ + 'enabled' => true, + 'default_lifetime' => 3600, + 'default_lock_lifetime' => 60, + 'file_lock_region_directory' => '', + 'regions' => [], +], +``` + +The filesystem adapter accepts more keys, see [Configuring the filesystem adapter](adapters/filesystem.md). diff --git a/docs/book/v5/installation.md b/docs/book/v5/installation.md new file mode 100644 index 0000000..790da19 --- /dev/null +++ b/docs/book/v5/installation.md @@ -0,0 +1,26 @@ +# Installation + +## Requirements + +- PHP `8.3`, `8.4`, `8.5` or `8.6` +- [symfony/cache](https://github.com/symfony/cache) `7.4` or `8.1+`, installed automatically: Composer picks `8.x` on PHP 8.4 and newer, and `7.4` on PHP 8.3 + +## Install the package + +Install `dotkernel/dot-cache` by executing the following Composer command: + +```shell +composer require dotkernel/dot-cache +``` + +## Register the ConfigProvider + +Add `Dot\Cache\ConfigProvider::class` to your configuration aggregator (usually `config/config.php`): + +```php +Dot\Cache\ConfigProvider::class, +``` + +It registers the factory for `Dot\Cache\Adapter\FilesystemAdapter` and aliases Symfony's `FilesystemAdapter` to it. + +Upgrading from `4.x`? Read [Upgrading to 5.x](upgrading.md). diff --git a/docs/book/v5/overview.md b/docs/book/v5/overview.md new file mode 100644 index 0000000..a598498 --- /dev/null +++ b/docs/book/v5/overview.md @@ -0,0 +1,24 @@ +# Overview + +Dotkernel cache component based on symfony-cache. + +> dot-cache is a wrapper on top of [symfony/cache](https://github.com/symfony/cache) + +> `dot-cache` uses [symfony/cache](https://github.com/symfony/cache) since version `4.0`. +> Version `3.x` of `dot-cache` is the last version that uses [laminas/laminas-cache](https://github.com/laminas/laminas-cache). + +Using the in-memory and/or filesystem cache adapters greatly improves overall application response times. + +## Badges + +![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-cache) +![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-cache/5.0.0) + +[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-cache)](https://github.com/dotkernel/dot-cache/issues) +[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-cache)](https://github.com/dotkernel/dot-cache/network) +[![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-cache)](https://github.com/dotkernel/dot-cache/stargazers) +[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-cache)](https://github.com/dotkernel/dot-cache/blob/5.0/LICENSE.md) + +[![Build Static](https://github.com/dotkernel/dot-cache/actions/workflows/continuous-integration.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/dot-cache/actions/workflows/continuous-integration.yml) +[![codecov](https://codecov.io/gh/dotkernel/dot-cache/graph/badge.svg?token=FAN1MXKKS9)](https://codecov.io/gh/dotkernel/dot-cache) +[![PHPStan](https://github.com/dotkernel/dot-cache/actions/workflows/static-analysis.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/dot-cache/actions/workflows/static-analysis.yml) diff --git a/docs/book/v5/upgrading.md b/docs/book/v5/upgrading.md new file mode 100644 index 0000000..eece4c5 --- /dev/null +++ b/docs/book/v5/upgrading.md @@ -0,0 +1,50 @@ +# Upgrading to 5.x + +`dot-cache` 5.x still uses [symfony/cache](https://github.com/symfony/cache), like 4.x. Most applications only need to bump the version constraint and check their `doctrine.cache.filesystem` config. + +```shell +composer require dotkernel/dot-cache:^5.0 +``` + +## Changes that can break your code + +| Change | Symptom | Fix | +| --- | --- | --- | +| PHP 8.2 support dropped | Composer refuses to install | Upgrade to PHP 8.3 or newer | +| `symfony/cache` 7.0 to 7.3 no longer allowed | Composer conflict with another package pinned to an old Symfony 7 minor | Update to Symfony 7.4, or 8.1+ on PHP 8.4+ | +| Symfony 8 is installed on PHP 8.4+ | `Error: Unknown named parameter $storeSerialized` when creating `ArrayAdapter` | Symfony 8 renamed the argument to `deepClone`. Use `deepClone:` or a positional argument | +| `directory` is now required for the filesystem adapter | `Dot\Cache\Exception\InvalidConfigException` while the container builds the adapter | Set `doctrine.cache.filesystem.directory`. In 4.x a missing directory silently stored the cache in the system temp directory | +| Config values are validated | `InvalidConfigException` instead of a PHP `TypeError` | Fix the key named in the message | +| `ConfigProvider` and `FilesystemAdapterFactory` are `final` | `Class ... cannot extend final class` | Wrap or decorate them instead of extending. The adapters are still not `final` | + +## Changes in behaviour + +| Change | Effect | What to do | +| --- | --- | --- | +| `default_lifetime` is now read | In 4.x only `default_life_time` worked, and `default_lifetime` was silently ignored | Use `default_lifetime`. If both are set, `default_lifetime` wins | +| `default_life_time` is deprecated | Still works in 5.x | Rename it to `default_lifetime` before 6.0 | +| Numeric strings are accepted for `default_lifetime` | `'3600'` (for example from an environment variable) now works | Nothing | +| `marshaller` can be a service name | It is fetched from the container | Nothing | + +## Example + +Before (4.x): + +```php +'filesystem' => [ + 'class' => \Dot\Cache\Adapter\FilesystemAdapter::class, + 'namespace' => 'doctrine', + 'default_life_time' => 3600, +], +``` + +After (5.x): + +```php +'filesystem' => [ + 'class' => \Dot\Cache\Adapter\FilesystemAdapter::class, + 'directory' => getcwd() . '/data/cache', + 'namespace' => 'doctrine', + 'default_lifetime' => 3600, +], +``` diff --git a/docs/book/v5/usage.md b/docs/book/v5/usage.md new file mode 100644 index 0000000..1a774ed --- /dev/null +++ b/docs/book/v5/usage.md @@ -0,0 +1,14 @@ +# Usage + +> `dot-cache` uses [symfony/cache](https://github.com/symfony/cache) since version `4.0`. +> Version `3.x` of `dot-cache` is the last version that uses [laminas/laminas-cache](https://github.com/laminas/laminas-cache). + +Using cache when querying the database: + +```php +$queryBuilder->select('users')->from(User::class, 'users'); + +$result = $queryBuilder->setCacheable(true)->getQuery()->getResult(); +``` + +> `setCacheable(true)` uses Doctrine's second-level cache, so `doctrine.configuration.orm_default.second_level_cache.enabled` must be `true`. diff --git a/mkdocs.yml b/mkdocs.yml index 2fa3e1f..3418adb 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -2,11 +2,21 @@ docs_dir: docs/book site_dir: docs/html extra: project: Packages - current_version: v4 + current_version: v5 versions: + - v5 - v4 nav: - Home: index.md + - v5: + - Overview: v5/overview.md + - Installation: v5/installation.md + - Configuration: v5/configuration.md + - Usage: v5/usage.md + - Adapters: + - "In-memory adapter": v5/adapters/in-memory.md + - "Filesystem adapter": v5/adapters/filesystem.md + - "Upgrading to 5.x": v5/upgrading.md - v4: - Overview: v4/overview.md - Installation: v4/installation.md diff --git a/phpstan.neon b/phpstan.neon index 349be25..08d6acd 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,7 @@ includes: - vendor/phpstan/phpstan-phpunit/extension.neon parameters: - level: 5 + level: 8 paths: - src - test diff --git a/phpunit.xml b/phpunit.xml index 30b901e..3e1423d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,14 +1,24 @@ - - - - - ./test - - - - - - ./src - - - + + + + + test + + + + + src + + + diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index c1e834e..0a90e60 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -8,8 +8,17 @@ use Dot\Cache\Factory\FilesystemAdapterFactory; use Symfony\Component\Cache\Adapter\FilesystemAdapter as SymfonyFilesystemAdapter; -class ConfigProvider +/** + * @phpstan-type DependenciesConfig array{ + * factories: array, + * aliases: array, + * } + */ +final class ConfigProvider { + /** + * @return array{dependencies: DependenciesConfig} + */ public function __invoke(): array { return [ @@ -17,6 +26,9 @@ public function __invoke(): array ]; } + /** + * @return DependenciesConfig + */ public function getDependenciesConfig(): array { return [ diff --git a/src/Exception/InvalidConfigException.php b/src/Exception/InvalidConfigException.php new file mode 100644 index 0000000..c5cc737 --- /dev/null +++ b/src/Exception/InvalidConfigException.php @@ -0,0 +1,51 @@ +get('config')['doctrine']['cache']['filesystem'] ?? []; + $config = $this->filesystemConfig($container); return new FilesystemAdapter( - namespace: $config['namespace'] ?? '', - defaultLifetime: $config['default_life_time'] ?? 0, - directory: $config['directory'] ?? null, - marshaller: $config['marshaller'] ?? null, + namespace: $this->namespace($config), + defaultLifetime: $this->defaultLifetime($config), + directory: $this->directory($config), + marshaller: $this->marshaller($container, $config), ); } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + private function filesystemConfig(ContainerInterface $container): array + { + $config = $container->has('config') ? $container->get('config') : []; + foreach (['doctrine', 'cache', 'filesystem'] as $key) { + if (! is_array($config)) { + return []; + } + $config = $config[$key] ?? []; + } + + if (! is_array($config)) { + throw InvalidConfigException::notAnArray(self::CONFIG_KEY, $config); + } + + return $config; + } + + /** + * @param array $config + */ + private function namespace(array $config): string + { + $namespace = $config['namespace'] ?? ''; + if (! is_string($namespace)) { + throw InvalidConfigException::notAString(self::CONFIG_KEY . '.namespace', $namespace); + } + + return $namespace; + } + + /** + * @param array $config + */ + private function defaultLifetime(array $config): int + { + $lifetime = array_key_exists('default_lifetime', $config) + ? $config['default_lifetime'] + : $config['default_life_time'] ?? 0; + + $seconds = is_int($lifetime) || is_string($lifetime) + ? filter_var($lifetime, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]) + : false; + if ($seconds === false) { + throw InvalidConfigException::invalidLifetime(self::CONFIG_KEY . '.default_lifetime', $lifetime); + } + + return $seconds; + } + + /** + * @param array $config + */ + private function directory(array $config): string + { + $directory = $config['directory'] ?? null; + if (! is_string($directory) || $directory === '') { + throw InvalidConfigException::missingDirectory(self::CONFIG_KEY . '.directory'); + } + + return $directory; + } + + /** + * @param array $config + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + private function marshaller(ContainerInterface $container, array $config): ?MarshallerInterface + { + $marshaller = $config['marshaller'] ?? null; + if (is_string($marshaller)) { + $marshaller = $container->get($marshaller); + } + + if ($marshaller !== null && ! $marshaller instanceof MarshallerInterface) { + throw InvalidConfigException::invalidMarshaller(self::CONFIG_KEY . '.marshaller', $marshaller); + } + + return $marshaller; + } } diff --git a/test/ArrayAdapterTest.php b/test/ArrayAdapterTest.php index 248a0df..7f5afd5 100644 --- a/test/ArrayAdapterTest.php +++ b/test/ArrayAdapterTest.php @@ -2,18 +2,21 @@ declare(strict_types=1); -namespace DotTest\DotCache; +namespace DotTest\Cache; use Dot\Cache\Adapter\ArrayAdapter; use PHPUnit\Framework\TestCase; -use Symfony\Component\Cache\Adapter\ArrayAdapter as SymfonyArrayAdapter; class ArrayAdapterTest extends TestCase { - public function testInstantiate() + public function testStoresAndReadsItem(): void { $adapter = new ArrayAdapter(); + $adapter->save($adapter->getItem('key')->set('value')); - $this->assertContainsOnlyInstancesOf(SymfonyArrayAdapter::class, [$adapter]); + $item = $adapter->getItem('key'); + + $this->assertTrue($item->isHit()); + $this->assertSame('value', $item->get()); } } diff --git a/test/ConfigProviderTest.php b/test/ConfigProviderTest.php index 3d5673f..bfdc336 100644 --- a/test/ConfigProviderTest.php +++ b/test/ConfigProviderTest.php @@ -2,36 +2,35 @@ declare(strict_types=1); -namespace DotTest\DotCache; +namespace DotTest\Cache; use Dot\Cache\Adapter\FilesystemAdapter; use Dot\Cache\ConfigProvider; +use Dot\Cache\Factory\FilesystemAdapterFactory; use PHPUnit\Framework\TestCase; use Symfony\Component\Cache\Adapter\FilesystemAdapter as SymfonyFilesystemAdapter; class ConfigProviderTest extends TestCase { - protected array $config; - - protected function setup(): void - { - $this->config = (new ConfigProvider())(); - } - - public function testHasDependencies(): void + public function testInvokeReturnsDependencies(): void { - $this->assertArrayHasKey('dependencies', $this->config); - } + $provider = new ConfigProvider(); - public function testDependenciesHasFactories(): void - { - $this->assertArrayHasKey('factories', $this->config['dependencies']); - $this->assertArrayHasKey(FilesystemAdapter::class, $this->config['dependencies']['factories']); + $this->assertSame(['dependencies' => $provider->getDependenciesConfig()], $provider()); } - public function testDependenciesHasAliases(): void + public function testDependenciesConfig(): void { - $this->assertArrayHasKey('aliases', $this->config['dependencies']); - $this->assertArrayHasKey(SymfonyFilesystemAdapter::class, $this->config['dependencies']['aliases']); + $this->assertSame( + [ + 'factories' => [ + FilesystemAdapter::class => FilesystemAdapterFactory::class, + ], + 'aliases' => [ + SymfonyFilesystemAdapter::class => FilesystemAdapter::class, + ], + ], + (new ConfigProvider())->getDependenciesConfig() + ); } } diff --git a/test/Factory/FilesystemAdapterFactoryTest.php b/test/Factory/FilesystemAdapterFactoryTest.php new file mode 100644 index 0000000..61ad979 --- /dev/null +++ b/test/Factory/FilesystemAdapterFactoryTest.php @@ -0,0 +1,221 @@ +directory = new TemporaryDirectory(); + } + + protected function tearDown(): void + { + $this->directory->remove(); + } + + public function testCreatesWorkingAdapterFromConfig(): void + { + $adapter = $this->create([ + 'namespace' => 'doctrine', + 'directory' => $this->directory->path, + 'default_lifetime' => 60, + ]); + $adapter->save($adapter->getItem('key')->set('value')); + + $this->assertSame(60, $this->defaultLifetime($adapter)); + $this->assertDirectoryExists($this->directory->path . '/doctrine'); + $this->assertSame('value', $adapter->getItem('key')->get()); + } + + public function testDefaultLifetimeDefaultsToZero(): void + { + $adapter = $this->create(['directory' => $this->directory->path]); + + $this->assertSame(0, $this->defaultLifetime($adapter)); + } + + public function testReadsDeprecatedDefaultLifeTimeKey(): void + { + $adapter = $this->create(['directory' => $this->directory->path, 'default_life_time' => 30]); + + $this->assertSame(30, $this->defaultLifetime($adapter)); + } + + public function testDefaultLifetimeWinsOverDeprecatedKey(): void + { + $adapter = $this->create([ + 'directory' => $this->directory->path, + 'default_lifetime' => 60, + 'default_life_time' => 30, + ]); + + $this->assertSame(60, $this->defaultLifetime($adapter)); + } + + public function testAcceptsNumericStringLifetime(): void + { + $adapter = $this->create(['directory' => $this->directory->path, 'default_lifetime' => '3600']); + + $this->assertSame(3600, $this->defaultLifetime($adapter)); + } + + /** + * @return iterable + */ + public static function invalidLifetimeProvider(): iterable + { + yield 'negative' => [-1]; + yield 'non-numeric' => ['an hour']; + yield 'float' => [1.5]; + yield 'array' => [[60]]; + } + + #[DataProvider('invalidLifetimeProvider')] + public function testRejectsInvalidLifetime(mixed $lifetime): void + { + $this->expectException(InvalidConfigException::class); + $this->expectExceptionMessage('doctrine.cache.filesystem.default_lifetime'); + + $this->create(['directory' => $this->directory->path, 'default_lifetime' => $lifetime]); + } + + public function testResolvesMarshallerServiceName(): void + { + $marshaller = new DefaultMarshaller(); + $config = ['directory' => $this->directory->path, 'marshaller' => 'my.marshaller']; + + $adapter = (new FilesystemAdapterFactory())( + new ArrayContainer(['config' => $this->appConfig($config), 'my.marshaller' => $marshaller]) + ); + + $this->assertSame($marshaller, $this->marshaller($adapter)); + } + + public function testUsesMarshallerInstance(): void + { + $marshaller = new DefaultMarshaller(); + + $adapter = $this->create(['directory' => $this->directory->path, 'marshaller' => $marshaller]); + + $this->assertSame($marshaller, $this->marshaller($adapter)); + } + + public function testRejectsMarshallerOfWrongType(): void + { + $this->expectException(InvalidConfigException::class); + $this->expectExceptionMessage('doctrine.cache.filesystem.marshaller'); + + $this->create(['directory' => $this->directory->path, 'marshaller' => new stdClass()]); + } + + public function testMissingMarshallerServiceIsReported(): void + { + $this->expectException(NotFoundExceptionInterface::class); + + $this->create(['directory' => $this->directory->path, 'marshaller' => 'missing.marshaller']); + } + + public function testRejectsMissingDirectory(): void + { + $this->expectException(InvalidConfigException::class); + $this->expectExceptionMessage('doctrine.cache.filesystem.directory'); + + $this->create(['namespace' => 'doctrine']); + } + + public function testRejectsEmptyDirectory(): void + { + $this->expectException(InvalidConfigException::class); + $this->expectExceptionMessage('doctrine.cache.filesystem.directory'); + + $this->create(['directory' => '']); + } + + public function testRejectsNonStringNamespace(): void + { + $this->expectException(InvalidConfigException::class); + $this->expectExceptionMessage('doctrine.cache.filesystem.namespace'); + + $this->create(['directory' => $this->directory->path, 'namespace' => 42]); + } + + public function testRejectsMissingFilesystemEntry(): void + { + $this->expectException(InvalidConfigException::class); + $this->expectExceptionMessage('doctrine.cache.filesystem.directory'); + + (new FilesystemAdapterFactory())( + new ArrayContainer(['config' => ['doctrine' => ['cache' => ['file' => ['directory' => '/tmp']]]]]) + ); + } + + public function testRejectsMissingConfigService(): void + { + $this->expectException(InvalidConfigException::class); + $this->expectExceptionMessage('doctrine.cache.filesystem.directory'); + + (new FilesystemAdapterFactory())(new ArrayContainer()); + } + + public function testRejectsFilesystemEntryThatIsNotAnArray(): void + { + $this->expectException(InvalidConfigException::class); + $this->expectExceptionMessage('doctrine.cache.filesystem'); + + (new FilesystemAdapterFactory())( + new ArrayContainer(['config' => ['doctrine' => ['cache' => ['filesystem' => 'data/cache']]]]) + ); + } + + /** + * @param array $filesystemConfig + */ + private function create(array $filesystemConfig): FilesystemAdapter + { + return (new FilesystemAdapterFactory())( + new ArrayContainer(['config' => $this->appConfig($filesystemConfig)]) + ); + } + + /** + * @param array $filesystemConfig + * @return array{doctrine: array{cache: array{filesystem: array}}} + */ + private function appConfig(array $filesystemConfig): array + { + return ['doctrine' => ['cache' => ['filesystem' => $filesystemConfig]]]; + } + + private function defaultLifetime(FilesystemAdapter $adapter): mixed + { + return (new ReflectionProperty(AbstractAdapter::class, 'defaultLifetime'))->getValue($adapter); + } + + private function marshaller(FilesystemAdapter $adapter): mixed + { + $value = (new ReflectionProperty(SymfonyFilesystemAdapter::class, 'marshaller'))->getValue($adapter); + $this->assertInstanceOf(MarshallerInterface::class, $value); + + return $value; + } +} diff --git a/test/FilesystemAdapterTest.php b/test/FilesystemAdapterTest.php index 848590a..f6a3100 100644 --- a/test/FilesystemAdapterTest.php +++ b/test/FilesystemAdapterTest.php @@ -2,18 +2,35 @@ declare(strict_types=1); -namespace DotTest\DotCache; +namespace DotTest\Cache; use Dot\Cache\Adapter\FilesystemAdapter; +use DotTest\Cache\Fixture\TemporaryDirectory; use PHPUnit\Framework\TestCase; -use Symfony\Component\Cache\Adapter\FilesystemAdapter as SymfonyFilesystemAdapter; class FilesystemAdapterTest extends TestCase { - public function testInstantiate() + private TemporaryDirectory $directory; + + protected function setUp(): void + { + $this->directory = new TemporaryDirectory(); + } + + protected function tearDown(): void { - $adapter = new FilesystemAdapter(); + $this->directory->remove(); + } + + public function testStoresItemOnDisk(): void + { + $adapter = new FilesystemAdapter('test', 0, $this->directory->path); + $adapter->save($adapter->getItem('key')->set('value')); + + $reader = new FilesystemAdapter('test', 0, $this->directory->path); + $item = $reader->getItem('key'); - $this->assertContainsOnlyInstancesOf(SymfonyFilesystemAdapter::class, [$adapter]); + $this->assertTrue($item->isHit()); + $this->assertSame('value', $item->get()); } } diff --git a/test/Fixture/ArrayContainer.php b/test/Fixture/ArrayContainer.php new file mode 100644 index 0000000..1fadc66 --- /dev/null +++ b/test/Fixture/ArrayContainer.php @@ -0,0 +1,37 @@ + $services + */ + public function __construct( + private array $services = [], + ) { + } + + public function get(string $id): mixed + { + if (! $this->has($id)) { + throw new ServiceNotFoundException('Service not found: ' . $id); + } + + return $this->services[$id]; + } + + public function has(string $id): bool + { + return array_key_exists($id, $this->services); + } +} diff --git a/test/Fixture/ServiceNotFoundException.php b/test/Fixture/ServiceNotFoundException.php new file mode 100644 index 0000000..d4f746b --- /dev/null +++ b/test/Fixture/ServiceNotFoundException.php @@ -0,0 +1,12 @@ +path = sys_get_temp_dir() . '/dot-cache-test-' . bin2hex(random_bytes(8)); + mkdir($this->path, 0700, true); + } + + public function remove(): void + { + if (! is_dir($this->path)) { + return; + } + + $entries = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($this->path, FilesystemIterator::SKIP_DOTS), + RecursiveIteratorIterator::CHILD_FIRST + ); + + /** @var SplFileInfo $entry */ + foreach ($entries as $entry) { + $entry->isDir() ? rmdir($entry->getPathname()) : unlink($entry->getPathname()); + } + + rmdir($this->path); + } +} From 9fedf9dc88d061c5fcce7abc822cd8e4f557008e Mon Sep 17 00:00:00 2001 From: bidi Date: Wed, 11 Nov 2026 13:51:12 +0200 Subject: [PATCH 2/2] updates based on review Signed-off-by: bidi --- composer.json | 3 +- src/Factory/FilesystemAdapterFactory.php | 14 +++++---- test/Factory/FilesystemAdapterFactoryTest.php | 30 ++++++++++++++++++- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index b9a7968..5c5b540 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,7 @@ "cache", "services", "doctrine", + "doctrine-cache", "symfony-cache" ], "require": { @@ -26,7 +27,7 @@ "laminas/laminas-coding-standard": "^3.0", "phpstan/phpstan": "^2.1", "phpstan/phpstan-phpunit": "^2.0", - "phpunit/phpunit": "^12" + "phpunit/phpunit": "^12.5" }, "autoload": { "psr-4": { diff --git a/src/Factory/FilesystemAdapterFactory.php b/src/Factory/FilesystemAdapterFactory.php index c801bad..301e850 100644 --- a/src/Factory/FilesystemAdapterFactory.php +++ b/src/Factory/FilesystemAdapterFactory.php @@ -10,12 +10,14 @@ use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; use Symfony\Component\Cache\Marshaller\MarshallerInterface; +use Traversable; use function array_key_exists; use function filter_var; use function is_array; use function is_int; use function is_string; +use function iterator_to_array; use const FILTER_VALIDATE_INT; @@ -54,11 +56,13 @@ public function __invoke(ContainerInterface $container): FilesystemAdapter private function filesystemConfig(ContainerInterface $container): array { $config = $container->has('config') ? $container->get('config') : []; - foreach (['doctrine', 'cache', 'filesystem'] as $key) { - if (! is_array($config)) { - return []; - } - $config = $config[$key] ?? []; + if (! isset($config['doctrine']['cache']['filesystem'])) { + return []; + } + + $config = $config['doctrine']['cache']['filesystem']; + if ($config instanceof Traversable) { + $config = iterator_to_array($config); } if (! is_array($config)) { diff --git a/test/Factory/FilesystemAdapterFactoryTest.php b/test/Factory/FilesystemAdapterFactoryTest.php index 61ad979..37a9aab 100644 --- a/test/Factory/FilesystemAdapterFactoryTest.php +++ b/test/Factory/FilesystemAdapterFactoryTest.php @@ -4,6 +4,7 @@ namespace DotTest\Cache\Factory; +use ArrayObject; use Dot\Cache\Adapter\FilesystemAdapter; use Dot\Cache\Exception\InvalidConfigException; use Dot\Cache\Factory\FilesystemAdapterFactory; @@ -177,10 +178,37 @@ public function testRejectsMissingConfigService(): void (new FilesystemAdapterFactory())(new ArrayContainer()); } + public function testReadsConfigServiceThatIsArrayObject(): void + { + $config = new ArrayObject($this->appConfig(['directory' => $this->directory->path, 'default_lifetime' => 60])); + + $adapter = (new FilesystemAdapterFactory())(new ArrayContainer(['config' => $config])); + + $this->assertSame(60, $this->defaultLifetime($adapter)); + } + + public function testReadsNestedArrayObjectLevels(): void + { + $config = [ + 'doctrine' => new ArrayObject([ + 'cache' => new ArrayObject([ + 'filesystem' => new ArrayObject([ + 'directory' => $this->directory->path, + 'default_lifetime' => 60, + ]), + ]), + ]), + ]; + + $adapter = (new FilesystemAdapterFactory())(new ArrayContainer(['config' => $config])); + + $this->assertSame(60, $this->defaultLifetime($adapter)); + } + public function testRejectsFilesystemEntryThatIsNotAnArray(): void { $this->expectException(InvalidConfigException::class); - $this->expectExceptionMessage('doctrine.cache.filesystem'); + $this->expectExceptionMessage('Config key "doctrine.cache.filesystem" must be an array, string given.'); (new FilesystemAdapterFactory())( new ArrayContainer(['config' => ['doctrine' => ['cache' => ['filesystem' => 'data/cache']]]])