Skip to content

Commit 4dd8aa3

Browse files
authored
Support version 3 of the symfony/cache-contracts package (#588)
1 parent 2d30164 commit 4dd8aa3

8 files changed

+190
-24
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add support for `symfony/cache-contracts` package version `3.x` (#588)
6+
57
## 4.2.5 (2021-12-13)
68

79
- Add support for Symfony 6 (#566)

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"jean85/pretty-package-versions": "^1.5 || ^2.0",
2424
"php-http/discovery": "^1.11",
2525
"sentry/sdk": "^3.1",
26-
"symfony/cache-contracts": "^1.1||^2.4",
26+
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
2727
"symfony/config": "^3.4.44||^4.4.20||^5.0.11||^6.0",
2828
"symfony/console": "^3.4.44||^4.4.20||^5.0.11||^6.0",
2929
"symfony/dependency-injection": "^3.4.44||^4.4.20||^5.0.11||^6.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tracing\Cache;
6+
7+
use Sentry\State\HubInterface;
8+
use Symfony\Component\Cache\Adapter\AdapterInterface;
9+
use Symfony\Component\Cache\PruneableInterface;
10+
use Symfony\Component\Cache\ResettableInterface;
11+
use Symfony\Contracts\Cache\CacheInterface;
12+
13+
/**
14+
* This implementation of a cache adapter supports the distributed tracing
15+
* feature of Sentry.
16+
*
17+
* @internal
18+
*/
19+
final class TraceableCacheAdapterForV2 implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
20+
{
21+
/**
22+
* @phpstan-use TraceableCacheAdapterTrait<AdapterInterface>
23+
*/
24+
use TraceableCacheAdapterTrait;
25+
26+
/**
27+
* @param HubInterface $hub The current hub
28+
* @param AdapterInterface $decoratedAdapter The decorated cache adapter
29+
*/
30+
public function __construct(HubInterface $hub, AdapterInterface $decoratedAdapter)
31+
{
32+
$this->hub = $hub;
33+
$this->decoratedAdapter = $decoratedAdapter;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*
39+
* @param mixed[] $metadata
40+
*
41+
* @return mixed
42+
*/
43+
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
44+
{
45+
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
46+
if (!$this->decoratedAdapter instanceof CacheInterface) {
47+
throw new \BadMethodCallException(sprintf('The %s::get() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, CacheInterface::class));
48+
}
49+
50+
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
51+
});
52+
}
53+
}

src/Tracing/Cache/TraceableCacheAdapter.php src/Tracing/Cache/TraceableCacheAdapterForV3.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
/**
1414
* This implementation of a cache adapter supports the distributed tracing
1515
* feature of Sentry.
16+
*
17+
* @internal
1618
*/
17-
final class TraceableCacheAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
19+
final class TraceableCacheAdapterForV3 implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
1820
{
1921
/**
2022
* @phpstan-use TraceableCacheAdapterTrait<AdapterInterface>
@@ -30,4 +32,20 @@ public function __construct(HubInterface $hub, AdapterInterface $decoratedAdapte
3032
$this->hub = $hub;
3133
$this->decoratedAdapter = $decoratedAdapter;
3234
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*
39+
* @param mixed[] $metadata
40+
*/
41+
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null): mixed
42+
{
43+
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
44+
if (!$this->decoratedAdapter instanceof CacheInterface) {
45+
throw new \BadMethodCallException(sprintf('The %s::get() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, CacheInterface::class));
46+
}
47+
48+
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
49+
});
50+
}
3351
}

src/Tracing/Cache/TraceableCacheAdapterTrait.php

+3-21
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ trait TraceableCacheAdapterTrait
3838
*/
3939
public function getItem($key): CacheItem
4040
{
41-
return $this->traceFunction('cache.get_item', function () use ($key) {
41+
return $this->traceFunction('cache.get_item', function () use ($key): CacheItem {
4242
return $this->decoratedAdapter->getItem($key);
4343
});
4444
}
@@ -48,7 +48,7 @@ public function getItem($key): CacheItem
4848
*/
4949
public function getItems(array $keys = []): iterable
5050
{
51-
return $this->traceFunction('cache.get_items', function () use ($keys) {
51+
return $this->traceFunction('cache.get_items', function () use ($keys): iterable {
5252
return $this->decoratedAdapter->getItems($keys);
5353
});
5454
}
@@ -63,30 +63,12 @@ public function clear(string $prefix = ''): bool
6363
});
6464
}
6565

66-
/**
67-
* {@inheritdoc}
68-
*
69-
* @param mixed[] $metadata
70-
*
71-
* @return mixed
72-
*/
73-
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
74-
{
75-
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
76-
if (!$this->decoratedAdapter instanceof CacheInterface) {
77-
throw new \BadMethodCallException(sprintf('The %s::get() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, CacheInterface::class));
78-
}
79-
80-
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
81-
});
82-
}
83-
8466
/**
8567
* {@inheritdoc}
8668
*/
8769
public function delete(string $key): bool
8870
{
89-
return $this->traceFunction('cache.delete_item', function () use ($key) {
71+
return $this->traceFunction('cache.delete_item', function () use ($key): bool {
9072
if (!$this->decoratedAdapter instanceof CacheInterface) {
9173
throw new \BadMethodCallException(sprintf('The %s::delete() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, CacheInterface::class));
9274
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tracing\Cache;
6+
7+
use Sentry\State\HubInterface;
8+
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
9+
use Symfony\Component\Cache\PruneableInterface;
10+
use Symfony\Component\Cache\ResettableInterface;
11+
use Symfony\Contracts\Cache\CacheInterface;
12+
use Symfony\Contracts\Cache\TagAwareCacheInterface;
13+
14+
/**
15+
* This implementation of a cache adapter aware of cache tags supports the
16+
* distributed tracing feature of Sentry.
17+
*
18+
* @internal
19+
*/
20+
final class TraceableTagAwareCacheAdapterForV2 implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface
21+
{
22+
/**
23+
* @phpstan-use TraceableCacheAdapterTrait<TagAwareAdapterInterface>
24+
*/
25+
use TraceableCacheAdapterTrait;
26+
27+
/**
28+
* @param HubInterface $hub The current hub
29+
* @param TagAwareAdapterInterface $decoratedAdapter The decorated cache adapter
30+
*/
31+
public function __construct(HubInterface $hub, TagAwareAdapterInterface $decoratedAdapter)
32+
{
33+
$this->hub = $hub;
34+
$this->decoratedAdapter = $decoratedAdapter;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*
40+
* @param mixed[] $metadata
41+
*
42+
* @return mixed
43+
*/
44+
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
45+
{
46+
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
47+
if (!$this->decoratedAdapter instanceof CacheInterface) {
48+
throw new \BadMethodCallException(sprintf('The %s::get() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, CacheInterface::class));
49+
}
50+
51+
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
52+
});
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function invalidateTags(array $tags): bool
59+
{
60+
return $this->traceFunction('cache.invalidate_tags', function () use ($tags): bool {
61+
return $this->decoratedAdapter->invalidateTags($tags);
62+
});
63+
}
64+
}

src/Tracing/Cache/TraceableTagAwareCacheAdapter.php src/Tracing/Cache/TraceableTagAwareCacheAdapterForV3.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
99
use Symfony\Component\Cache\PruneableInterface;
1010
use Symfony\Component\Cache\ResettableInterface;
11+
use Symfony\Contracts\Cache\CacheInterface;
1112
use Symfony\Contracts\Cache\TagAwareCacheInterface;
1213

1314
/**
1415
* This implementation of a cache adapter aware of cache tags supports the
1516
* distributed tracing feature of Sentry.
17+
*
18+
* @internal
1619
*/
17-
final class TraceableTagAwareCacheAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface
20+
final class TraceableTagAwareCacheAdapterForV3 implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface
1821
{
1922
/**
2023
* @phpstan-use TraceableCacheAdapterTrait<TagAwareAdapterInterface>
@@ -31,6 +34,22 @@ public function __construct(HubInterface $hub, TagAwareAdapterInterface $decorat
3134
$this->decoratedAdapter = $decoratedAdapter;
3235
}
3336

37+
/**
38+
* {@inheritdoc}
39+
*
40+
* @param mixed[] $metadata
41+
*/
42+
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null): mixed
43+
{
44+
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
45+
if (!$this->decoratedAdapter instanceof CacheInterface) {
46+
throw new \BadMethodCallException(sprintf('The %s::get() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, CacheInterface::class));
47+
}
48+
49+
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
50+
});
51+
}
52+
3453
/**
3554
* {@inheritdoc}
3655
*/

src/aliases.php

+28
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@
1212
use Sentry\SentryBundle\EventListener\RequestListenerResponseEvent;
1313
use Sentry\SentryBundle\EventListener\RequestListenerTerminateEvent;
1414
use Sentry\SentryBundle\EventListener\SubRequestListenerRequestEvent;
15+
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapter;
16+
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapterForV2;
17+
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapterForV3;
18+
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapter;
19+
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapterForV2;
20+
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapterForV3;
1521
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\Compatibility\MiddlewareInterface;
1622
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverForV2;
1723
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverForV3;
1824
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingStatementForV2;
1925
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingStatementForV3;
26+
use Symfony\Component\Cache\Adapter\AdapterInterface;
27+
use Symfony\Component\Cache\DoctrineProvider;
2028
use Symfony\Component\HttpKernel\Event\ControllerEvent;
2129
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
2230
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
@@ -79,6 +87,26 @@ class_alias(GetResponseEvent::class, SubRequestListenerRequestEvent::class);
7987
}
8088
}
8189

90+
if (interface_exists(AdapterInterface::class)) {
91+
if (!class_exists(DoctrineProvider::class, false) && version_compare(\PHP_VERSION, '8.0.0', '>=')) {
92+
if (!class_exists(TraceableCacheAdapter::class, false)) {
93+
class_alias(TraceableCacheAdapterForV3::class, TraceableCacheAdapter::class);
94+
}
95+
96+
if (!class_exists(TraceableTagAwareCacheAdapter::class, false)) {
97+
class_alias(TraceableTagAwareCacheAdapterForV3::class, TraceableTagAwareCacheAdapter::class);
98+
}
99+
} else {
100+
if (!class_exists(TraceableCacheAdapter::class, false)) {
101+
class_alias(TraceableCacheAdapterForV2::class, TraceableCacheAdapter::class);
102+
}
103+
104+
if (!class_exists(TraceableTagAwareCacheAdapter::class, false)) {
105+
class_alias(TraceableTagAwareCacheAdapterForV2::class, TraceableTagAwareCacheAdapter::class);
106+
}
107+
}
108+
}
109+
82110
if (!interface_exists(DoctrineMiddlewareInterface::class)) {
83111
class_alias(MiddlewareInterface::class, DoctrineMiddlewareInterface::class);
84112
}

0 commit comments

Comments
 (0)