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 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
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 || ^2",
"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.5.0",
"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
212 changes: 147 additions & 65 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@

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
*/
final class ConfigProvider
{
/**
* @return mixed[]
* @return array<non-empty-string, mixed[]>
*/
public function __invoke(): array
{
return [
'caches' => $this->getCachesConfig(),
'doctrine' => $this->getDoctrineConfig(),
'doctrine_factories' => $this->getDoctrineFactoryConfig(),
'dependencies' => $this->getDependencyConfig(),
Expand All @@ -28,7 +33,7 @@ public function __invoke(): array
/**
* Return application-level dependency configuration
*
* @return mixed[]
* @return array<non-empty-string, array<non-empty-string, class-string>>
*/
public function getDependencyConfig(): array
{
Expand All @@ -42,62 +47,12 @@ public function getDependencyConfig(): array
/**
* Default configuration for Doctrine module
*
* @return mixed[]
* @return array<non-empty-string, mixed[]>
*/
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 All @@ -124,35 +79,162 @@ public function getDoctrineConfig(): array
/**
* Factory mappings - used to define which factory to use to instantiate a particular doctrine service type
*
* @return mixed[]
* @return array<non-empty-string, class-string>
*/
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,
];
}

/**
* @return mixed[]
* @return array<non-empty-string, mixed[]>
*/
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 array<non-empty-string, array{adapter: string, options?: mixed[]}>
*/
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 array<non-empty-string,array{class:class-string,instance?:string,namespace?:string,directory?:string}>
*/
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 @@ -19,6 +19,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
Loading