57371e879f20f8e14d632afcc596cc00e1eff4e9
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / DataCollector / CacheDataCollector.php
1 <?php
2
3 namespace Drupal\webprofiler\DataCollector;
4
5 use Drupal\webprofiler\DrupalDataCollectorInterface;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
9 use Symfony\Component\HttpKernel\DataCollector\DataCollector;
10
11 /**
12  * Collects the used cache bins and cache CIDs.
13  */
14 class CacheDataCollector extends DataCollector implements DrupalDataCollectorInterface {
15
16   use StringTranslationTrait, DrupalDataCollectorTrait;
17
18   const WEBPROFILER_CACHE_HIT = 'bin_cids_hit';
19   const WEBPROFILER_CACHE_MISS = 'bin_cids_miss';
20
21   /**
22    * {@inheritdoc}
23    */
24   public function collect(Request $request, Response $response, \Exception $exception = NULL) {
25   }
26
27   /**
28    *
29    */
30   public function __construct() {
31     $this->data['total'][CacheDataCollector::WEBPROFILER_CACHE_HIT] = 0;
32     $this->data['total'][CacheDataCollector::WEBPROFILER_CACHE_MISS] = 0;
33     $this->data['cache'] = [];
34   }
35
36   /**
37    * Registers a cache get on a specific cache bin.
38    *
39    * @param $cache
40    */
41   public function registerCacheHit($bin, $cache) {
42     $current = isset($this->data['cache'][$bin][$cache->cid]) ? $this->data['cache'][$bin][$cache->cid] : NULL;
43
44     if (!$current) {
45       $current = $cache;
46       $current->{CacheDataCollector::WEBPROFILER_CACHE_HIT} = 0;
47       $current->{CacheDataCollector::WEBPROFILER_CACHE_MISS} = 0;
48       $this->data['cache'][$bin][$cache->cid] = $current;
49     }
50
51     $current->{CacheDataCollector::WEBPROFILER_CACHE_HIT}++;
52     $this->data['total'][CacheDataCollector::WEBPROFILER_CACHE_HIT]++;
53   }
54
55   /**
56    * Registers a cache get on a specific cache bin.
57    *
58    * @param $bin
59    * @param $cid
60    */
61   public function registerCacheMiss($bin, $cid) {
62     $current = isset($this->data['cache'][$bin][$cid]) ?
63       $this->data['cache'][$bin][$cid] : NULL;
64
65     if (!$current) {
66       $current = new \StdClass();
67       $current->{CacheDataCollector::WEBPROFILER_CACHE_HIT} = 0;
68       $current->{CacheDataCollector::WEBPROFILER_CACHE_MISS} = 0;
69       $this->data['cache'][$bin][$cid] = $current;
70     }
71
72     $current->{CacheDataCollector::WEBPROFILER_CACHE_MISS}++;
73     $this->data['total'][CacheDataCollector::WEBPROFILER_CACHE_MISS]++;
74   }
75
76   /**
77    * Callback to return the total amount of requested cache CIDS.
78    *
79    * @param string $type
80    *
81    * @return int
82    */
83   public function getCacheCidsCount($type) {
84     return $this->data['total'][$type];
85   }
86
87   /**
88    * Callback to return the total amount of hit cache CIDS.
89    *
90    * @return int
91    */
92   public function getCacheHitsCount() {
93     return $this->getCacheCidsCount(CacheDataCollector::WEBPROFILER_CACHE_HIT);
94   }
95
96   /**
97    * Callback to return the total amount of miss cache CIDS.
98    *
99    * @return int
100    */
101   public function getCacheMissesCount() {
102     return $this->getCacheCidsCount(CacheDataCollector::WEBPROFILER_CACHE_MISS);
103   }
104
105   /**
106    * Callback to return the total amount of hit cache CIDs keyed by bin.
107    *
108    * @param $type
109    *
110    * @return array
111    */
112   public function cacheCids($type) {
113     $hits = [];
114     foreach ($this->data['cache'] as $bin => $caches) {
115       $hits[$bin] = 0;
116       foreach ($caches as $cid => $cache) {
117         $hits[$bin] += $cache->{$type};
118       }
119     }
120
121     return $hits;
122   }
123
124   /**
125    * Callback to return hit cache CIDs keyed by bin.
126    *
127    * @return array
128    */
129   public function getCacheHits() {
130     return $this->cacheCids(CacheDataCollector::WEBPROFILER_CACHE_HIT);
131   }
132
133   /**
134    * Callback to return miss cache CIDs keyed by bin.
135    *
136    * @return array
137    */
138   public function getCacheMisses() {
139     return $this->cacheCids(CacheDataCollector::WEBPROFILER_CACHE_MISS);
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public function getName() {
146     return 'cache';
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function getTitle() {
153     return $this->t('Cache');
154   }
155
156   /**
157    * {@inheritdoc}
158    */
159   public function getPanelSummary() {
160     return $this->t('Hit: @cache_hit, miss: @cache_miss', [
161       '@cache_hit' => $this->getCacheCidsCount(CacheDataCollector::WEBPROFILER_CACHE_HIT),
162       '@cache_miss' => $this->getCacheCidsCount(CacheDataCollector::WEBPROFILER_CACHE_MISS),
163     ]);
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function getIcon() {
170     return 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAcCAYAAABh2p9gAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYxIDY0LjE0MDk0OSwgMjAxMC8xMi8wNy0xMDo1NzowMSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNS4xIE1hY2ludG9zaCIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDo2Njc3QTVEQTkxNkMxMUUzQjA3OU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