Skip to content

Commit 760895e

Browse files
Rework
1 parent c9fd7dc commit 760895e

File tree

4 files changed

+73
-16
lines changed

4 files changed

+73
-16
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",

src/Tracing/Cache/TraceableCacheAdapterForV3.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@
99
use Symfony\Component\Cache\PruneableInterface;
1010
use Symfony\Component\Cache\ResettableInterface;
1111
use Symfony\Contracts\Cache\CacheInterface;
12-
use Symfony\Contracts\Cache\NamespacedPoolInterface;
1312

1413
/**
1514
* This implementation of a cache adapter supports the distributed tracing
1615
* feature of Sentry.
1716
*
1817
* @internal
1918
*/
20-
final class TraceableCacheAdapterForV3 implements AdapterInterface, NamespacedPoolInterface, CacheInterface, PruneableInterface, ResettableInterface
19+
final class TraceableCacheAdapterForV3 implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
2120
{
2221
/**
2322
* @phpstan-use TraceableCacheAdapterTrait<AdapterInterface>
@@ -49,16 +48,4 @@ public function get(string $key, callable $callback, ?float $beta = null, ?array
4948
return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
5049
}, $key);
5150
}
52-
53-
public function withSubNamespace(string $namespace): static
54-
{
55-
if (!$this->decoratedAdapter instanceof NamespacedPoolInterface) {
56-
throw new \BadMethodCallException(\sprintf('The %s::withSubNamespace() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, NamespacedPoolInterface::class));
57-
}
58-
59-
$clone = clone $this;
60-
$clone->decoratedAdapter = $this->decoratedAdapter->withSubNamespace($namespace);
61-
62-
return $clone;
63-
}
6451
}
Lines changed: 64 additions & 0 deletions
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\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, NamespacedPoolInterface, 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+
53+
public function withSubNamespace(string $namespace): static
54+
{
55+
if (!$this->decoratedAdapter instanceof NamespacedPoolInterface) {
56+
throw new \BadMethodCallException(\sprintf('The %s::withSubNamespace() method is not supported because the decorated adapter does not implement the "%s" interface.', self::class, NamespacedPoolInterface::class));
57+
}
58+
59+
$clone = clone $this;
60+
$clone->decoratedAdapter = $this->decoratedAdapter->withSubNamespace($namespace);
61+
62+
return $clone;
63+
}
64+
}

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)