Skip to content

Commit

Permalink
Support version 3 of the symfony/cache-contracts package (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
ste93cry committed Jan 4, 2022
1 parent 2d30164 commit 4dd8aa3
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 24 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add support for `symfony/cache-contracts` package version `3.x` (#588)

## 4.2.5 (2021-12-13)

- Add support for Symfony 6 (#566)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"jean85/pretty-package-versions": "^1.5 || ^2.0",
"php-http/discovery": "^1.11",
"sentry/sdk": "^3.1",
"symfony/cache-contracts": "^1.1||^2.4",
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
"symfony/config": "^3.4.44||^4.4.20||^5.0.11||^6.0",
"symfony/console": "^3.4.44||^4.4.20||^5.0.11||^6.0",
"symfony/dependency-injection": "^3.4.44||^4.4.20||^5.0.11||^6.0",
Expand Down
53 changes: 53 additions & 0 deletions src/Tracing/Cache/TraceableCacheAdapterForV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tracing\Cache;

use Sentry\State\HubInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Contracts\Cache\CacheInterface;

/**
* This implementation of a cache adapter supports the distributed tracing
* feature of Sentry.
*
* @internal
*/
final class TraceableCacheAdapterForV2 implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
{
/**
* @phpstan-use TraceableCacheAdapterTrait<AdapterInterface>
*/
use TraceableCacheAdapterTrait;

/**
* @param HubInterface $hub The current hub
* @param AdapterInterface $decoratedAdapter The decorated cache adapter
*/
public function __construct(HubInterface $hub, AdapterInterface $decoratedAdapter)
{
$this->hub = $hub;
$this->decoratedAdapter = $decoratedAdapter;
}

/**
* {@inheritdoc}
*
* @param mixed[] $metadata
*
* @return mixed
*/
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
{
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
if (!$this->decoratedAdapter instanceof CacheInterface) {
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));
}

return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
/**
* This implementation of a cache adapter supports the distributed tracing
* feature of Sentry.
*
* @internal
*/
final class TraceableCacheAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
final class TraceableCacheAdapterForV3 implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
{
/**
* @phpstan-use TraceableCacheAdapterTrait<AdapterInterface>
Expand All @@ -30,4 +32,20 @@ public function __construct(HubInterface $hub, AdapterInterface $decoratedAdapte
$this->hub = $hub;
$this->decoratedAdapter = $decoratedAdapter;
}

/**
* {@inheritdoc}
*
* @param mixed[] $metadata
*/
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null): mixed
{
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
if (!$this->decoratedAdapter instanceof CacheInterface) {
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));
}

return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
});
}
}
24 changes: 3 additions & 21 deletions src/Tracing/Cache/TraceableCacheAdapterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ trait TraceableCacheAdapterTrait
*/
public function getItem($key): CacheItem
{
return $this->traceFunction('cache.get_item', function () use ($key) {
return $this->traceFunction('cache.get_item', function () use ($key): CacheItem {
return $this->decoratedAdapter->getItem($key);
});
}
Expand All @@ -48,7 +48,7 @@ public function getItem($key): CacheItem
*/
public function getItems(array $keys = []): iterable
{
return $this->traceFunction('cache.get_items', function () use ($keys) {
return $this->traceFunction('cache.get_items', function () use ($keys): iterable {
return $this->decoratedAdapter->getItems($keys);
});
}
Expand All @@ -63,30 +63,12 @@ public function clear(string $prefix = ''): bool
});
}

/**
* {@inheritdoc}
*
* @param mixed[] $metadata
*
* @return mixed
*/
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
{
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
if (!$this->decoratedAdapter instanceof CacheInterface) {
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));
}

return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
});
}

/**
* {@inheritdoc}
*/
public function delete(string $key): bool
{
return $this->traceFunction('cache.delete_item', function () use ($key) {
return $this->traceFunction('cache.delete_item', function () use ($key): bool {
if (!$this->decoratedAdapter instanceof CacheInterface) {
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));
}
Expand Down
64 changes: 64 additions & 0 deletions src/Tracing/Cache/TraceableTagAwareCacheAdapterForV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Sentry\SentryBundle\Tracing\Cache;

use Sentry\State\HubInterface;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

/**
* This implementation of a cache adapter aware of cache tags supports the
* distributed tracing feature of Sentry.
*
* @internal
*/
final class TraceableTagAwareCacheAdapterForV2 implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface
{
/**
* @phpstan-use TraceableCacheAdapterTrait<TagAwareAdapterInterface>
*/
use TraceableCacheAdapterTrait;

/**
* @param HubInterface $hub The current hub
* @param TagAwareAdapterInterface $decoratedAdapter The decorated cache adapter
*/
public function __construct(HubInterface $hub, TagAwareAdapterInterface $decoratedAdapter)
{
$this->hub = $hub;
$this->decoratedAdapter = $decoratedAdapter;
}

/**
* {@inheritdoc}
*
* @param mixed[] $metadata
*
* @return mixed
*/
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
{
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
if (!$this->decoratedAdapter instanceof CacheInterface) {
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));
}

return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
});
}

/**
* {@inheritdoc}
*/
public function invalidateTags(array $tags): bool
{
return $this->traceFunction('cache.invalidate_tags', function () use ($tags): bool {
return $this->decoratedAdapter->invalidateTags($tags);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;

/**
* This implementation of a cache adapter aware of cache tags supports the
* distributed tracing feature of Sentry.
*
* @internal
*/
final class TraceableTagAwareCacheAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface
final class TraceableTagAwareCacheAdapterForV3 implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface
{
/**
* @phpstan-use TraceableCacheAdapterTrait<TagAwareAdapterInterface>
Expand All @@ -31,6 +34,22 @@ public function __construct(HubInterface $hub, TagAwareAdapterInterface $decorat
$this->decoratedAdapter = $decoratedAdapter;
}

/**
* {@inheritdoc}
*
* @param mixed[] $metadata
*/
public function get(string $key, callable $callback, float $beta = null, array &$metadata = null): mixed
{
return $this->traceFunction('cache.get_item', function () use ($key, $callback, $beta, &$metadata) {
if (!$this->decoratedAdapter instanceof CacheInterface) {
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));
}

return $this->decoratedAdapter->get($key, $callback, $beta, $metadata);
});
}

/**
* {@inheritdoc}
*/
Expand Down
28 changes: 28 additions & 0 deletions src/aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@
use Sentry\SentryBundle\EventListener\RequestListenerResponseEvent;
use Sentry\SentryBundle\EventListener\RequestListenerTerminateEvent;
use Sentry\SentryBundle\EventListener\SubRequestListenerRequestEvent;
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapter;
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapterForV2;
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapterForV3;
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapter;
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapterForV2;
use Sentry\SentryBundle\Tracing\Cache\TraceableTagAwareCacheAdapterForV3;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\Compatibility\MiddlewareInterface;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverForV2;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverForV3;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingStatementForV2;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingStatementForV3;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\DoctrineProvider;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
Expand Down Expand Up @@ -79,6 +87,26 @@ class_alias(GetResponseEvent::class, SubRequestListenerRequestEvent::class);
}
}

if (interface_exists(AdapterInterface::class)) {
if (!class_exists(DoctrineProvider::class, false) && version_compare(\PHP_VERSION, '8.0.0', '>=')) {
if (!class_exists(TraceableCacheAdapter::class, false)) {
class_alias(TraceableCacheAdapterForV3::class, TraceableCacheAdapter::class);
}

if (!class_exists(TraceableTagAwareCacheAdapter::class, false)) {
class_alias(TraceableTagAwareCacheAdapterForV3::class, TraceableTagAwareCacheAdapter::class);
}
} else {
if (!class_exists(TraceableCacheAdapter::class, false)) {
class_alias(TraceableCacheAdapterForV2::class, TraceableCacheAdapter::class);
}

if (!class_exists(TraceableTagAwareCacheAdapter::class, false)) {
class_alias(TraceableTagAwareCacheAdapterForV2::class, TraceableTagAwareCacheAdapter::class);
}
}
}

if (!interface_exists(DoctrineMiddlewareInterface::class)) {
class_alias(MiddlewareInterface::class, DoctrineMiddlewareInterface::class);
}
Expand Down

0 comments on commit 4dd8aa3

Please sign in to comment.