vendor/sentry/sentry-symfony/src/Tracing/Cache/TraceableCacheAdapterTrait.php line 113

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\SentryBundle\Tracing\Cache;
  4. use Psr\Cache\CacheItemInterface;
  5. use Sentry\State\HubInterface;
  6. use Sentry\Tracing\SpanContext;
  7. use Symfony\Component\Cache\Adapter\AdapterInterface;
  8. use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
  9. use Symfony\Component\Cache\CacheItem;
  10. use Symfony\Component\Cache\PruneableInterface;
  11. use Symfony\Component\Cache\ResettableInterface;
  12. use Symfony\Contracts\Cache\CacheInterface;
  13. /**
  14.  * @internal
  15.  *
  16.  * @phpstan-template T of AdapterInterface
  17.  */
  18. trait TraceableCacheAdapterTrait
  19. {
  20.     /**
  21.      * @var HubInterface The current hub
  22.      */
  23.     private $hub;
  24.     /**
  25.      * @var AdapterInterface|TagAwareAdapterInterface The decorated adapter
  26.      *
  27.      * @phpstan-var T
  28.      */
  29.     private $decoratedAdapter;
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function getItem($key): CacheItem
  34.     {
  35.         return $this->traceFunction('cache.get_item', function () use ($key): CacheItem {
  36.             return $this->decoratedAdapter->getItem($key);
  37.         }, $key);
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function getItems(array $keys = []): iterable
  43.     {
  44.         return $this->traceFunction('cache.get_items', function () use ($keys): iterable {
  45.             return $this->decoratedAdapter->getItems($keys);
  46.         });
  47.     }
  48.     /**
  49.      * {@inheritdoc}
  50.      */
  51.     public function clear(string $prefix ''): bool
  52.     {
  53.         return $this->traceFunction('cache.clear', function () use ($prefix): bool {
  54.             return $this->decoratedAdapter->clear($prefix);
  55.         }, $prefix);
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function delete(string $key): bool
  61.     {
  62.         return $this->traceFunction('cache.delete_item', function () use ($key): bool {
  63.             if (!$this->decoratedAdapter instanceof CacheInterface) {
  64.                 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));
  65.             }
  66.             return $this->decoratedAdapter->delete($key);
  67.         }, $key);
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public function hasItem($key): bool
  73.     {
  74.         return $this->traceFunction('cache.has_item', function () use ($key): bool {
  75.             return $this->decoratedAdapter->hasItem($key);
  76.         }, $key);
  77.     }
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     public function deleteItem($key): bool
  82.     {
  83.         return $this->traceFunction('cache.delete_item', function () use ($key): bool {
  84.             return $this->decoratedAdapter->deleteItem($key);
  85.         }, $key);
  86.     }
  87.     /**
  88.      * {@inheritdoc}
  89.      */
  90.     public function deleteItems(array $keys): bool
  91.     {
  92.         return $this->traceFunction('cache.delete_items', function () use ($keys): bool {
  93.             return $this->decoratedAdapter->deleteItems($keys);
  94.         });
  95.     }
  96.     /**
  97.      * {@inheritdoc}
  98.      */
  99.     public function save(CacheItemInterface $item): bool
  100.     {
  101.         return $this->traceFunction('cache.save', function () use ($item): bool {
  102.             return $this->decoratedAdapter->save($item);
  103.         });
  104.     }
  105.     /**
  106.      * {@inheritdoc}
  107.      */
  108.     public function saveDeferred(CacheItemInterface $item): bool
  109.     {
  110.         return $this->traceFunction('cache.save_deferred', function () use ($item): bool {
  111.             return $this->decoratedAdapter->saveDeferred($item);
  112.         });
  113.     }
  114.     /**
  115.      * {@inheritdoc}
  116.      */
  117.     public function commit(): bool
  118.     {
  119.         return $this->traceFunction('cache.commit', function (): bool {
  120.             return $this->decoratedAdapter->commit();
  121.         });
  122.     }
  123.     /**
  124.      * {@inheritdoc}
  125.      */
  126.     public function prune(): bool
  127.     {
  128.         return $this->traceFunction('cache.prune', function (): bool {
  129.             if (!$this->decoratedAdapter instanceof PruneableInterface) {
  130.                 return false;
  131.             }
  132.             return $this->decoratedAdapter->prune();
  133.         });
  134.     }
  135.     /**
  136.      * {@inheritdoc}
  137.      */
  138.     public function reset(): void
  139.     {
  140.         if ($this->decoratedAdapter instanceof ResettableInterface) {
  141.             $this->decoratedAdapter->reset();
  142.         }
  143.     }
  144.     /**
  145.      * @phpstan-template TResult
  146.      *
  147.      * @phpstan-param \Closure(): TResult $callback
  148.      *
  149.      * @phpstan-return TResult
  150.      */
  151.     private function traceFunction(string $spanOperation, \Closure $callbackstring $spanDescription null)
  152.     {
  153.         $span $this->hub->getSpan();
  154.         if (null !== $span) {
  155.             $spanContext = new SpanContext();
  156.             $spanContext->setOp($spanOperation);
  157.             if (null !== $spanDescription) {
  158.                 $spanContext->setDescription(urldecode($spanDescription));
  159.             }
  160.             $span $span->startChild($spanContext);
  161.         }
  162.         try {
  163.             return $callback();
  164.         } finally {
  165.             if (null !== $span) {
  166.                 $span->finish();
  167.             }
  168.         }
  169.     }
  170. }