Skip to content

Commit 069d62a

Browse files
authored
chore: remove dependency on thecodingmachine/cache-utils (#695)
* chore: remove dependency on thecodingmachine/cache-utils The package seems to be unmaintained and prevents usage due to old psr/simple-cache. This removes the dependency on that package. The package provided cache clearance on Classes and ParentClasses when their files changed. This functionality can still be achieved by creating a custom implementation of `TheCodingMachine\GraphQLite\Utils\Cache\ClassBoundCacheContractFactoryInterface`. fixes #693 * fix: correctly escape `|` in phpstan ignoreErrors * fix: sync min symfony/cache version of library to example and solve deprecation This is necessary because symfony/cache is not compatible with every supported version of psr/simple-cache. More info here: symfony/symfony#44738
1 parent 81094e9 commit 069d62a

15 files changed

+131
-25
lines changed

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"psr/simple-cache": "^1.0.1 || ^2 || ^3",
2424
"symfony/cache": "^4.3 || ^5 || ^6 || ^7",
2525
"symfony/expression-language": "^4 || ^5 || ^6 || ^7",
26-
"thecodingmachine/cache-utils": "^1",
2726
"webonyx/graphql-php": "^v15.0",
2827
"kcs/class-finder": "^0.5.0"
2928
},

examples/no-framework/composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"require": {
88
"thecodingmachine/graphqlite": "@dev",
99
"mouf/picotainer": "^1.1",
10-
"symfony/cache": "^4.2"
10+
"symfony/cache": "^4.3",
11+
"psr/simple-cache": "^1.0"
1112
},
1213
"repositories": [
1314
{

examples/no-framework/index.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
use TheCodingMachine\GraphQLite\SchemaFactory;
55
use TheCodingMachine\GraphQLite\Context\Context;
66

7-
use Symfony\Component\Cache\Simple\FilesystemCache;
7+
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
8+
use Symfony\Component\Cache\Psr16Cache;
89
use Mouf\Picotainer\Picotainer;
910
use GraphQL\Utils\SchemaPrinter;
1011
use App\Controllers\MyController;
1112

1213
require_once __DIR__ . '/vendor/autoload.php';
1314

1415
// $cache is any PSR-16 compatible cache.
15-
$cache = new FilesystemCache();
16+
$cache = new Psr16Cache(new FilesystemAdapter());;
1617

1718
// $container is any PSR-11 compatible container which has
1819
// been populated with your controller classes.

phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ parameters:
3434
message: '#Unreachable statement - code above always terminates.#'
3535
path: src/Http/WebonyxGraphqlMiddleware.php
3636
-
37-
message: '#Property TheCodingMachine\\GraphQLite\\Annotations\\Type::\$class \(class-string<object>\\|null\) does not accept string.#'
37+
message: '#Property TheCodingMachine\\GraphQLite\\Annotations\\Type::\$class \(class-string<object>\|null\) does not accept string.#'
3838
path: src/Annotations/Type.php
3939
-
4040
message: '#Method TheCodingMachine\\GraphQLite\\AnnotationReader::(getMethodAnnotations|getPropertyAnnotations)\(\) should return array<int, T of object> but returns array<object>.#'

src/Cache/ClassBoundCacheContract.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\GraphQLite\Cache;
6+
7+
use Psr\SimpleCache\CacheInterface;
8+
use Psr\SimpleCache\InvalidArgumentException;
9+
use ReflectionClass;
10+
11+
use function str_replace;
12+
13+
class ClassBoundCacheContract implements ClassBoundCacheContractInterface
14+
{
15+
private readonly string $cachePrefix;
16+
17+
public function __construct(private readonly CacheInterface $classBoundCache, string $cachePrefix = '')
18+
{
19+
$this->cachePrefix = str_replace(['\\', '{', '}', '(', ')', '/', '@', ':'], '_', $cachePrefix);
20+
}
21+
22+
/**
23+
* @param string $key An optional key to differentiate between cache items attached to the same class.
24+
*
25+
* @throws InvalidArgumentException
26+
*/
27+
public function get(ReflectionClass $reflectionClass, callable $resolver, string $key = '', int|null $ttl = null): mixed
28+
{
29+
$cacheKey = $reflectionClass->getName() . '__' . $key;
30+
$cacheKey = $this->cachePrefix . str_replace(['\\', '{', '}', '(', ')', '/', '@', ':'], '_', $cacheKey);
31+
32+
$item = $this->classBoundCache->get($cacheKey);
33+
if ($item !== null) {
34+
return $item;
35+
}
36+
37+
$item = $resolver();
38+
39+
$this->classBoundCache->set($cacheKey, $item, $ttl);
40+
41+
return $item;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\GraphQLite\Cache;
6+
7+
use Psr\SimpleCache\CacheInterface;
8+
9+
class ClassBoundCacheContractFactory implements ClassBoundCacheContractFactoryInterface
10+
{
11+
public function make(CacheInterface $classBoundCache, string $cachePrefix = ''): ClassBoundCacheContractInterface
12+
{
13+
return new ClassBoundCacheContract($classBoundCache, $cachePrefix);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\GraphQLite\Cache;
6+
7+
use Psr\SimpleCache\CacheInterface;
8+
9+
interface ClassBoundCacheContractFactoryInterface
10+
{
11+
public function make(CacheInterface $classBoundCache, string $cachePrefix = ''): ClassBoundCacheContractInterface;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\GraphQLite\Cache;
6+
7+
use ReflectionClass;
8+
9+
interface ClassBoundCacheContractInterface
10+
{
11+
/** @param string $key An optional key to differentiate between cache items attached to the same class. */
12+
public function get(ReflectionClass $reflectionClass, callable $resolver, string $key = '', int|null $ttl = null): mixed;
13+
}

src/FactoryContext.php

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Psr\Container\ContainerInterface;
88
use Psr\SimpleCache\CacheInterface;
9+
use TheCodingMachine\GraphQLite\Cache\ClassBoundCacheContractFactoryInterface;
910
use TheCodingMachine\GraphQLite\Mappers\RecursiveTypeMapperInterface;
1011
use TheCodingMachine\GraphQLite\Types\InputTypeValidatorInterface;
1112
use TheCodingMachine\GraphQLite\Types\TypeResolver;
@@ -31,6 +32,7 @@ public function __construct(
3132
private readonly InputTypeValidatorInterface|null $inputTypeValidator,
3233
private readonly int|null $globTTL,
3334
private readonly int|null $mapTTL = null,
35+
private readonly ClassBoundCacheContractFactoryInterface|null $classBoundCacheContractFactory = null,
3436
) {
3537
}
3638

@@ -84,6 +86,11 @@ public function getCache(): CacheInterface
8486
return $this->cache;
8587
}
8688

89+
public function getClassBoundCacheContractFactory(): ClassBoundCacheContractFactoryInterface|null
90+
{
91+
return $this->classBoundCacheContractFactory;
92+
}
93+
8794
public function getInputTypeValidator(): InputTypeValidatorInterface|null
8895
{
8996
return $this->inputTypeValidator;

src/Mappers/AbstractTypeMapper.php

+8-18
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
use ReflectionMethod;
1616
use Symfony\Component\Cache\Adapter\Psr16Adapter;
1717
use Symfony\Contracts\Cache\CacheInterface as CacheContractInterface;
18-
use TheCodingMachine\CacheUtils\ClassBoundCache;
19-
use TheCodingMachine\CacheUtils\ClassBoundCacheContract;
20-
use TheCodingMachine\CacheUtils\ClassBoundCacheContractInterface;
21-
use TheCodingMachine\CacheUtils\ClassBoundMemoryAdapter;
22-
use TheCodingMachine\CacheUtils\FileBoundCache;
2318
use TheCodingMachine\GraphQLite\AnnotationReader;
19+
use TheCodingMachine\GraphQLite\Cache\ClassBoundCacheContractFactory;
20+
use TheCodingMachine\GraphQLite\Cache\ClassBoundCacheContractFactoryInterface;
21+
use TheCodingMachine\GraphQLite\Cache\ClassBoundCacheContractInterface;
2422
use TheCodingMachine\GraphQLite\InputTypeGenerator;
2523
use TheCodingMachine\GraphQLite\InputTypeUtils;
2624
use TheCodingMachine\GraphQLite\NamingStrategyInterface;
@@ -66,23 +64,15 @@ public function __construct(
6664
private readonly CacheInterface $cache,
6765
protected int|null $globTTL = 2,
6866
private readonly int|null $mapTTL = null,
67+
ClassBoundCacheContractFactoryInterface|null $classBoundCacheContractFactory = null,
6968
)
7069
{
7170
$this->cacheContract = new Psr16Adapter($this->cache, $cachePrefix, $this->globTTL ?? 0);
7271

73-
$classToAnnotationsCache = new ClassBoundCache(
74-
new FileBoundCache($this->cache, 'classToAnnotations_' . $cachePrefix),
75-
);
76-
$this->mapClassToAnnotationsCache = new ClassBoundCacheContract(
77-
new ClassBoundMemoryAdapter($classToAnnotationsCache),
78-
);
79-
80-
$classToExtendedAnnotationsCache = new ClassBoundCache(
81-
new FileBoundCache($this->cache, 'classToExtendAnnotations_' . $cachePrefix),
82-
);
83-
$this->mapClassToExtendAnnotationsCache = new ClassBoundCacheContract(
84-
new ClassBoundMemoryAdapter($classToExtendedAnnotationsCache),
85-
);
72+
$classBoundCacheContractFactory = $classBoundCacheContractFactory ?? new ClassBoundCacheContractFactory();
73+
74+
$this->mapClassToAnnotationsCache = $classBoundCacheContractFactory->make($cache, 'classToAnnotations_' . $cachePrefix);
75+
$this->mapClassToExtendAnnotationsCache = $classBoundCacheContractFactory->make($cache, 'classToExtendAnnotations_' . $cachePrefix);
8676
}
8777

8878
/**

src/Mappers/GlobTypeMapper.php

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Psr\SimpleCache\CacheInterface;
99
use ReflectionClass;
1010
use TheCodingMachine\GraphQLite\AnnotationReader;
11+
use TheCodingMachine\GraphQLite\Cache\ClassBoundCacheContractFactoryInterface;
1112
use TheCodingMachine\GraphQLite\InputTypeGenerator;
1213
use TheCodingMachine\GraphQLite\InputTypeUtils;
1314
use TheCodingMachine\GraphQLite\NamingStrategyInterface;
@@ -44,6 +45,7 @@ public function __construct(
4445
CacheInterface $cache,
4546
int|null $globTTL = 2,
4647
int|null $mapTTL = null,
48+
ClassBoundCacheContractFactoryInterface|null $classBoundCacheContractFactory = null,
4749
) {
4850
$cachePrefix = str_replace(
4951
['\\', '{', '}', '(', ')', '/', '@', ':'],
@@ -63,6 +65,7 @@ public function __construct(
6365
$cache,
6466
$globTTL,
6567
$mapTTL,
68+
$classBoundCacheContractFactory,
6669
);
6770
}
6871

src/Mappers/StaticClassListTypeMapper.php

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Psr\SimpleCache\CacheInterface;
99
use ReflectionClass;
1010
use TheCodingMachine\GraphQLite\AnnotationReader;
11+
use TheCodingMachine\GraphQLite\Cache\ClassBoundCacheContractFactoryInterface;
1112
use TheCodingMachine\GraphQLite\GraphQLRuntimeException;
1213
use TheCodingMachine\GraphQLite\InputTypeGenerator;
1314
use TheCodingMachine\GraphQLite\InputTypeUtils;
@@ -45,6 +46,7 @@ public function __construct(
4546
CacheInterface $cache,
4647
int|null $globTTL = 2,
4748
int|null $mapTTL = null,
49+
ClassBoundCacheContractFactoryInterface|null $classBoundCacheContractFactory = null,
4850
) {
4951
$cachePrefix = str_replace(
5052
['\\', '{', '}', '(', ')', '/', '@', ':'],
@@ -64,6 +66,7 @@ public function __construct(
6466
$cache,
6567
$globTTL,
6668
$mapTTL,
69+
$classBoundCacheContractFactory,
6770
);
6871
}
6972

src/Mappers/StaticClassListTypeMapperFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function create(FactoryContext $context): TypeMapperInterface
3838
$context->getCache(),
3939
$context->getGlobTTL(),
4040
$context->getMapTTL(),
41+
$context->getClassBoundCacheContractFactory(),
4142
);
4243
}
4344
}

src/SchemaFactory.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Psr\SimpleCache\CacheInterface;
1414
use Symfony\Component\Cache\Adapter\Psr16Adapter;
1515
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
16+
use TheCodingMachine\GraphQLite\Cache\ClassBoundCacheContractFactoryInterface;
1617
use TheCodingMachine\GraphQLite\Mappers\CompositeTypeMapper;
1718
use TheCodingMachine\GraphQLite\Mappers\GlobTypeMapper;
1819
use TheCodingMachine\GraphQLite\Mappers\Parameters\ContainerParameterHandler;
@@ -120,7 +121,7 @@ class SchemaFactory
120121

121122
private string $cacheNamespace;
122123

123-
public function __construct(private readonly CacheInterface $cache, private readonly ContainerInterface $container)
124+
public function __construct(private readonly CacheInterface $cache, private readonly ContainerInterface $container, private ClassBoundCacheContractFactoryInterface|null $classBoundCacheContractFactory = null)
124125
{
125126
$this->cacheNamespace = substr(md5(Versions::getVersion('thecodingmachine/graphqlite')), 0, 8);
126127
}
@@ -259,6 +260,18 @@ public function setGlobTTL(int|null $globTTL): self
259260
return $this;
260261
}
261262

263+
/**
264+
* Set a custom ClassBoundCacheContractFactory.
265+
* This is used to create CacheContracts that store reflection results.
266+
* Set this to "null" to use the default fallback factory.
267+
*/
268+
public function setClassBoundCacheContractFactory(ClassBoundCacheContractFactoryInterface|null $classBoundCacheContractFactory): self
269+
{
270+
$this->classBoundCacheContractFactory = $classBoundCacheContractFactory;
271+
272+
return $this;
273+
}
274+
262275
/**
263276
* Sets GraphQLite in "prod" mode (cache settings optimized for best performance).
264277
*
@@ -430,6 +443,7 @@ public function createSchema(): Schema
430443
$recursiveTypeMapper,
431444
$namespacedCache,
432445
$this->globTTL,
446+
classBoundCacheContractFactory: $this->classBoundCacheContractFactory,
433447
));
434448
}
435449

@@ -447,6 +461,7 @@ public function createSchema(): Schema
447461
$namespacedCache,
448462
$this->inputTypeValidator,
449463
$this->globTTL,
464+
classBoundCacheContractFactory: $this->classBoundCacheContractFactory,
450465
);
451466
}
452467

tests/FactoryContextTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Symfony\Component\Cache\Adapter\ArrayAdapter;
66
use Symfony\Component\Cache\Psr16Cache;
7+
use TheCodingMachine\GraphQLite\Cache\ClassBoundCacheContractFactory;
78
use TheCodingMachine\GraphQLite\Containers\EmptyContainer;
89
use TheCodingMachine\GraphQLite\Fixtures\Inputs\Validator;
910

@@ -16,6 +17,7 @@ public function testContext(): void
1617
$namingStrategy = new NamingStrategy();
1718
$container = new EmptyContainer();
1819
$arrayCache = new Psr16Cache(new ArrayAdapter());
20+
$classBoundCacheContractFactory = new ClassBoundCacheContractFactory();
1921
$validator = new Validator();
2022

2123
$context = new FactoryContext(
@@ -30,7 +32,8 @@ public function testContext(): void
3032
$container,
3133
$arrayCache,
3234
$validator,
33-
self::GLOB_TTL_SECONDS
35+
self::GLOB_TTL_SECONDS,
36+
classBoundCacheContractFactory: $classBoundCacheContractFactory,
3437
);
3538

3639
$this->assertSame($this->getAnnotationReader(), $context->getAnnotationReader());

0 commit comments

Comments
 (0)