Skip to content

Commit fbbdc5c

Browse files
Rework
1 parent c9fd7dc commit fbbdc5c

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"guzzlehttp/psr7": "^2.1.1",
1717
"jean85/pretty-package-versions": "^1.5||^2.0",
1818
"sentry/sentry": "^4.14.1",
19-
"symfony/cache-contracts": "^1.1||^2.4||^3.6",
19+
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
2020
"symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0",
2121
"symfony/console": "^4.4.20||^5.0.11||^6.0||^7.0",
2222
"symfony/dependency-injection": "^4.4.20||^5.0.11||^6.0||^7.0",
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
use Symfony\Contracts\Cache\NamespacedPoolInterface;
13+
14+
/**
15+
* This implementation of a cache adapter supports the distributed tracing
16+
* feature of Sentry.
17+
*
18+
* @internal
19+
*/
20+
final class TraceableCacheAdapterForV3WithNamespace implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
21+
{
22+
/**
23+
* @phpstan-use TraceableCacheAdapterTrait<AdapterInterface>
24+
*/
25+
use TraceableCacheAdapterTrait;
26+
27+
/**
28+
* @param HubInterface $hub The current hub
29+
* @param AdapterInterface $decoratedAdapter The decorated cache adapter
30+
*/
31+
public function __construct(HubInterface $hub, AdapterInterface $decoratedAdapter)
32+
{
33+
$this->hub = $hub;
34+
$this->decoratedAdapter = $decoratedAdapter;
35+
}
36+
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+
}, $key);
51+
}
52+
}

src/aliases.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapter;
1010
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapterForV2;
1111
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapterForV3;
12+
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapterForV3WithNamespace;
1213
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapter;
1314
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapterForV2;
1415
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapterForV3;
@@ -38,12 +39,17 @@
3839
use Symfony\Component\Cache\DoctrineProvider;
3940
use Symfony\Component\HttpClient\HttpClient;
4041
use Symfony\Component\HttpClient\Response\StreamableInterface;
42+
use Symfony\Contracts\Cache\NamespacedPoolInterface;
4143
use Symfony\Contracts\HttpClient\HttpClientInterface;
4244

4345
if (interface_exists(AdapterInterface::class)) {
4446
if (!class_exists(DoctrineProvider::class, false) && version_compare(\PHP_VERSION, '8.0.0', '>=')) {
4547
if (!class_exists(TraceableCacheAdapter::class, false)) {
46-
class_alias(TraceableCacheAdapterForV3::class, TraceableCacheAdapter::class);
48+
if (class_exists(NamespacedPoolInterface::class, false)) {
49+
class_alias(TraceableCacheAdapterForV3WithNamespace::class, TraceableCacheAdapter::class);
50+
} else {
51+
class_alias(TraceableCacheAdapterForV3::class, TraceableCacheAdapter::class);
52+
}
4753
}
4854

4955
if (!class_exists(TraceableTagAwareCacheAdapter::class, false)) {

0 commit comments

Comments
 (0)