Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Cache / CacheFactoryWrapper.php
1 <?php
2
3 namespace Drupal\webprofiler\Cache;
4
5 use Drupal\Core\Cache\CacheFactoryInterface;
6 use Drupal\webprofiler\DataCollector\CacheDataCollector;
7 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
8 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
9
10 /**
11  * Wraps a cache factory to register all calls to the cache system.
12  */
13 class CacheFactoryWrapper implements CacheFactoryInterface, ContainerAwareInterface {
14
15   use ContainerAwareTrait;
16
17   /**
18    * The cache factory.
19    *
20    * @var \Drupal\Core\Cache\CacheFactoryInterface
21    */
22   protected $cacheFactory;
23
24   /**
25    * The cache data collector.
26    *
27    * @var \Drupal\webprofiler\DataCollector\CacheDataCollector
28    */
29   protected $cacheDataCollector;
30
31   /**
32    * All wrapped cache backends.
33    *
34    * @var \Drupal\webprofiler\Cache\CacheBackendWrapper[]
35    */
36   protected $cacheBackends = [];
37
38   /**
39    * Creates a new CacheFactoryWrapper instance.
40    *
41    * @param \Drupal\Core\Cache\CacheFactoryInterface $cache_factory
42    *   The cache factory.
43    * @param \Drupal\webprofiler\DataCollector\CacheDataCollector $cacheDataCollector
44    *   The cache data collector.
45    */
46   public function __construct(CacheFactoryInterface $cache_factory, CacheDataCollector $cacheDataCollector) {
47     $this->cacheFactory = $cache_factory;
48     $this->cacheDataCollector = $cacheDataCollector;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public function get($bin) {
55     if (!isset($this->cacheBackends[$bin])) {
56       $cache_backend = $this->cacheFactory->get($bin);
57       $this->cacheBackends[$bin] = new CacheBackendWrapper($this->cacheDataCollector, $cache_backend, $bin);
58     }
59     return $this->cacheBackends[$bin];
60   }
61
62 }