Skip to content
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

Switch default cache config to make use of laminas-cache, when doctrine/cache ^2.0 is installed #796

Merged
merged 5 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@
"homepage": "http://www.doctrine-project.org/",
"require": {
"php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0",
"composer-runtime-api": "^2.0",
"doctrine/annotations": "^1.13.3",
"doctrine/cache": "^1.13.0",
"doctrine/cache": "^1.13.0 || ^2.1.0",
"doctrine/collections": "^1.8.0",
"doctrine/doctrine-laminas-hydrator": "^3.2.0",
"doctrine/event-manager": "^1.2.0",
"doctrine/inflector": "^2.0.6",
"doctrine/persistence": "^2.5.5 || ^3.1.0",
"laminas/laminas-authentication": "^2.12.0",
"laminas/laminas-cache": "^3.6.0",
"laminas/laminas-cache-storage-adapter-filesystem": "^2.2.0",
"laminas/laminas-cache-storage-adapter-memory": "^2.1.0",
"laminas/laminas-eventmanager": "^3.5.0",
"laminas/laminas-form": "^3.4.1",
"laminas/laminas-modulemanager": "^2.12.0",
Expand All @@ -62,8 +65,6 @@
"doctrine/mongodb-odm": "^2.4.2",
"doctrine/orm": "^2.13.4",
"jangregor/phpstan-prophecy": "^1.0.0",
"laminas/laminas-cache-storage-adapter-blackhole": "^2.0.0",
"laminas/laminas-cache-storage-adapter-memory": "^2.1.0",
"laminas/laminas-i18n": "^2.17.0",
"laminas/laminas-log": "^2.15.3",
"laminas/laminas-serializer": "^2.13.0",
Expand Down
2 changes: 2 additions & 0 deletions data/DoctrineModule/cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
9 changes: 9 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ parameters:
-
message: '#internal(G|S)etItem\(\) has parameter \$(casToken|value) with no type specified#'
path: src/Cache/DoctrineCacheStorage.php
- '#(Class|Instantiated class) Doctrine\\Common\\Cache\\(.*) not found#'
- '#Call to method (.*) on an unknown class Doctrine\\Common\\Cache\\(.*)#'
-
message: '#class DoctrineModule\\Cache\\DoctrineCacheStorage constructor expects#'
path: tests/Cache/DoctrineCacheStorageTest.php
-
message: '#mock an undefined method save\(\) on class Doctrine\\Common\\Cache\\ArrayCache#'
path: tests/Cache/DoctrineCacheStorageTest.php

includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-phpunit/rules.neon
Expand Down
25 changes: 25 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
<file src="src/ConfigProvider.php">
<UndefinedClass occurrences="11">
<code>DoctrineCache\ApcCache</code>
<code>DoctrineCache\ApcuCache</code>
<code>DoctrineCache\ArrayCache</code>
<code>DoctrineCache\FilesystemCache</code>
<code>DoctrineCache\MemcacheCache</code>
<code>DoctrineCache\MemcachedCache</code>
<code>DoctrineCache\PredisCache</code>
<code>DoctrineCache\RedisCache</code>
<code>DoctrineCache\WinCacheCache</code>
<code>DoctrineCache\XcacheCache</code>
<code>DoctrineCache\ZendDataCache</code>
</UndefinedClass>
</file>
<file src="src/Service/CacheFactory.php">
<UndefinedClass occurrences="3">
<code>Cache\FilesystemCache</code>
<code>Cache\MemcacheCache</code>
<code>Cache\PredisCache</code>
</UndefinedClass>
</file>
</files>
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src" />
Expand Down
202 changes: 142 additions & 60 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace DoctrineModule;

use Doctrine\Common\Cache;
use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Doctrine\Common\Cache as DoctrineCache;
use DoctrineModule\Cache\LaminasStorageCache;
use Laminas\Authentication\Storage\Session as LaminasSessionStorage;
use Laminas\Cache\Storage\Adapter\Memory;

/**
* Config provider for DoctrineORMModule config
Expand All @@ -18,6 +22,7 @@ final class ConfigProvider
public function __invoke(): array
{
return [
'caches' => $this->getCachesConfig(),
'doctrine' => $this->getDoctrineConfig(),
'doctrine_factories' => $this->getDoctrineFactoryConfig(),
'dependencies' => $this->getDependencyConfig(),
Expand Down Expand Up @@ -47,57 +52,7 @@ public function getDependencyConfig(): array
public function getDoctrineConfig(): array
{
return [
'cache' => [
'apc' => [
'class' => Cache\ApcCache::class,
'namespace' => 'DoctrineModule',
],
'apcu' => [
'class' => Cache\ApcuCache::class,
'namespace' => 'DoctrineModule',
],
'array' => [
'class' => Cache\ArrayCache::class,
'namespace' => 'DoctrineModule',
],
'filesystem' => [
'class' => Cache\FilesystemCache::class,
'directory' => 'data/DoctrineModule/cache',
'namespace' => 'DoctrineModule',
],
'memcache' => [
'class' => Cache\MemcacheCache::class,
'instance' => 'my_memcache_alias',
'namespace' => 'DoctrineModule',
],
'memcached' => [
'class' => Cache\MemcachedCache::class,
'instance' => 'my_memcached_alias',
'namespace' => 'DoctrineModule',
],
'predis' => [
'class' => Cache\PredisCache::class,
'instance' => 'my_predis_alias',
'namespace' => 'DoctrineModule',
],
'redis' => [
'class' => Cache\RedisCache::class,
'instance' => 'my_redis_alias',
'namespace' => 'DoctrineModule',
],
'wincache' => [
'class' => Cache\WinCacheCache::class,
'namespace' => 'DoctrineModule',
],
'xcache' => [
'class' => Cache\XcacheCache::class,
'namespace' => 'DoctrineModule',
],
'zenddata' => [
'class' => Cache\ZendDataCache::class,
'namespace' => 'DoctrineModule',
],
],
'cache' => $this->getDoctrineCacheConfig(),

//These authentication settings are a hack to tide things over until version 1.0
//Normall doctrineModule should have no mention of odm or orm
Expand Down Expand Up @@ -129,9 +84,9 @@ public function getDoctrineConfig(): array
public function getDoctrineFactoryConfig(): array
{
return [
'cache' => Service\CacheFactory::class,
'eventmanager' => Service\EventManagerFactory::class,
'driver' => Service\DriverFactory::class,
'cache' => Service\CacheFactory::class,
'eventmanager' => Service\EventManagerFactory::class,
'driver' => Service\DriverFactory::class,
'authenticationadapter' => Service\Authentication\AdapterFactory::class,
'authenticationstorage' => Service\Authentication\StorageFactory::class,
'authenticationservice' => Service\Authentication\AuthenticationServiceFactory::class,
Expand All @@ -144,15 +99,142 @@ public function getDoctrineFactoryConfig(): array
public function getValidatorConfig(): array
{
return [
'aliases' => [
'aliases' => [
'DoctrineNoObjectExists' => Validator\NoObjectExists::class,
'DoctrineObjectExists' => Validator\ObjectExists::class,
'DoctrineUniqueObject' => Validator\UniqueObject::class,
'DoctrineObjectExists' => Validator\ObjectExists::class,
'DoctrineUniqueObject' => Validator\UniqueObject::class,
],
'factories' => [
Validator\NoObjectExists::class => Validator\Service\NoObjectExistsFactory::class,
Validator\ObjectExists::class => Validator\Service\ObjectExistsFactory::class,
Validator\UniqueObject::class => Validator\Service\UniqueObjectFactory::class,
Validator\ObjectExists::class => Validator\Service\ObjectExistsFactory::class,
Validator\UniqueObject::class => Validator\Service\UniqueObjectFactory::class,
],
];
}

/**
* @return mixed[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type should probably be array<non-empty-string, array{adapter: non-empty-string, options?: mixed[]}>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added more specific types for all configurations with 2c915c6

*/
public function getCachesConfig(): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This moving out made it a bit hard to figure out what's going on.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we need this extraction for usage in Module.php, where the laminas cache config is exposed to the service manager. In the next major release, we will drop doctrine/cache completely and so will the method getDoctrineCacheConfig be removed.

{
return [
'doctrinemodule.cache.apcu' => [
'adapter' => 'apcu',
'options' => ['namespace' => 'DoctrineModule'],
],
'doctrinemodule.cache.array' => [
'adapter' => Memory::class,
'options' => ['namespace' => 'DoctrineModule'],
],
'doctrinemodule.cache.filesystem' => [
'adapter' => 'filesystem',
'options' => [
'namespace' => 'DoctrineModule',
'cache_dir' => 'data/DoctrineModule/cache',
],
],
'doctrinemodule.cache.memcached' => [
'adapter' => 'memcached',
'options' => [
'namespace' => 'DoctrineModule',
'servers' => [],
],
],
'doctrinemodule.cache.redis' => [
'adapter' => 'redis',
'options' => [
'namespace' => 'DoctrineModule',
'server' => [
'host' => 'localhost',
'post' => 6379,
],
],
],
];
}

/**
* Use doctrine/cache config, when doctrine/cache:^1.0 is installed, and use laminas/laminas-cache,
* when doctrine/cache:^2.0 is installed, as the latter does not include any cache adapters anymore
*
* @return mixed[]
*/
private function getDoctrineCacheConfig(): array
{
if (InstalledVersions::satisfies(new VersionParser(), 'doctrine/cache', '^1.0')) {
return [
'apc' => [
'class' => DoctrineCache\ApcCache::class,
'namespace' => 'DoctrineModule',
],
'apcu' => [
'class' => DoctrineCache\ApcuCache::class,
'namespace' => 'DoctrineModule',
],
'array' => [
'class' => DoctrineCache\ArrayCache::class,
'namespace' => 'DoctrineModule',
],
'filesystem' => [
'class' => DoctrineCache\FilesystemCache::class,
'directory' => 'data/DoctrineModule/cache',
'namespace' => 'DoctrineModule',
],
'memcache' => [
'class' => DoctrineCache\MemcacheCache::class,
'instance' => 'my_memcache_alias',
'namespace' => 'DoctrineModule',
],
'memcached' => [
'class' => DoctrineCache\MemcachedCache::class,
'instance' => 'my_memcached_alias',
'namespace' => 'DoctrineModule',
],
'predis' => [
'class' => DoctrineCache\PredisCache::class,
'instance' => 'my_predis_alias',
'namespace' => 'DoctrineModule',
],
'redis' => [
'class' => DoctrineCache\RedisCache::class,
'instance' => 'my_redis_alias',
'namespace' => 'DoctrineModule',
],
'wincache' => [
'class' => DoctrineCache\WinCacheCache::class,
'namespace' => 'DoctrineModule',
],
'xcache' => [
'class' => DoctrineCache\XcacheCache::class,
'namespace' => 'DoctrineModule',
],
'zenddata' => [
'class' => DoctrineCache\ZendDataCache::class,
'namespace' => 'DoctrineModule',
],
];
}

return [
'apcu' => [
'class' => LaminasStorageCache::class,
'instance' => 'doctrinemodule.cache.apcu',
],
'array' => [
'class' => LaminasStorageCache::class,
'instance' => 'doctrinemodule.cache.array',
],
'filesystem' => [
'class' => LaminasStorageCache::class,
'instance' => 'doctrinemodule.cache.filesystem',
],
'memcached' => [
'class' => LaminasStorageCache::class,
'instance' => 'doctrinemodule.cache.memcached',
],
'redis' => [
'class' => LaminasStorageCache::class,
'instance' => 'doctrinemodule.cache.redis',
],
];
}
Expand Down
1 change: 1 addition & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function getConfig(): array
$provider = new ConfigProvider();

return [
'caches' => $provider->getCachesConfig(),
'doctrine' => $provider->getDoctrineConfig(),
'doctrine_factories' => $provider->getDoctrineFactoryConfig(),
'service_manager' => $provider->getDependencyConfig(),
Expand Down
4 changes: 2 additions & 2 deletions src/Options/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace DoctrineModule\Options;

use Doctrine\Common\Cache\ArrayCache;
use DoctrineModule\Cache\LaminasStorageCache;
use Laminas\Stdlib\AbstractOptions;

/**
Expand All @@ -15,7 +15,7 @@ final class Cache extends AbstractOptions
/**
* Class used to instantiate the cache.
*/
protected string $class = ArrayCache::class;
protected string $class = LaminasStorageCache::class;

/**
* Namespace to prefix all cache ids with.
Expand Down
6 changes: 6 additions & 0 deletions tests/Cache/DoctrineCacheStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace DoctrineModuleTest\Cache;

use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Doctrine\Common\Cache\ArrayCache as DoctrineArrayCache;
use DoctrineModule\Cache\DoctrineCacheStorage;
use Laminas\Cache\Storage\Adapter\AdapterOptions;
Expand Down Expand Up @@ -48,6 +50,10 @@ class DoctrineCacheStorageTest extends TestCase

protected function setUp(): void
{
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/cache', '^1.0.0')) {
$this->markTestSkipped('This test requires doctrine/cache:^1.0, which is not installed.');
}

$this->options = new AdapterOptions();
$this->storage = new DoctrineCacheStorage($this->options, new DoctrineArrayCache());

Expand Down
1 change: 1 addition & 0 deletions tests/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testInvokeHasCorrectKeys(): void

self::assertIsArray($config);

self::assertArrayHasKey('caches', $config, 'Expected config to have "caches" array key');
self::assertArrayHasKey('doctrine', $config, 'Expected config to have "doctrine" array key');
self::assertArrayHasKey(
'doctrine_factories',
Expand Down