Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / DataCollector / ThemeDataCollector.php
1 <?php
2
3 namespace Drupal\webprofiler\DataCollector;
4
5 use Drupal\Core\Theme\ThemeManagerInterface;
6 use Drupal\Core\Theme\ThemeNegotiatorInterface;
7 use Drupal\webprofiler\DrupalDataCollectorInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\webprofiler\Theme\ThemeNegotiatorWrapper;
10 use Drupal\webprofiler\Twig\Dumper\HtmlDumper;
11 use Symfony\Component\HttpFoundation\Request;
12 use Symfony\Component\HttpFoundation\Response;
13 use Symfony\Component\HttpKernel\DataCollector\DataCollector;
14 use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
15
16 /**
17  * Class ThemeDataCollector
18  */
19 class ThemeDataCollector extends DataCollector implements DrupalDataCollectorInterface, LateDataCollectorInterface {
20
21   use StringTranslationTrait, DrupalDataCollectorTrait;
22
23   /**
24    * @var \Drupal\Core\Theme\ThemeManagerInterface
25    */
26   private $themeManager;
27
28   /**
29    * @var \Drupal\Core\Theme\ThemeNegotiatorInterface
30    */
31   private $themeNegotiator;
32
33   /**
34    * @var \Twig_Profiler_Profile
35    */
36   private $profile;
37
38   /**
39    * @var
40    */
41   private $computed;
42
43   /**
44    * @param \Drupal\Core\Theme\ThemeManagerInterface $themeManager
45    * @param \Drupal\Core\Theme\ThemeNegotiatorInterface $themeNegotiator
46    * @param \Twig_Profiler_Profile $profile
47    */
48   public function __construct(ThemeManagerInterface $themeManager, ThemeNegotiatorInterface $themeNegotiator, \Twig_Profiler_Profile $profile) {
49     $this->themeManager = $themeManager;
50     $this->themeNegotiator = $themeNegotiator;
51     $this->profile = $profile;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function collect(Request $request, Response $response, \Exception $exception = NULL) {
58     $activeTheme = $this->themeManager->getActiveTheme();
59
60     $this->data['activeTheme'] = [
61       'name' => $activeTheme->getName(),
62       'path' => $activeTheme->getPath(),
63       'engine' => $activeTheme->getEngine(),
64       'owner' => $activeTheme->getOwner(),
65       'baseThemes' => $activeTheme->getBaseThemes(),
66       'extension' => $activeTheme->getExtension(),
67       'styleSheetsRemove' => $activeTheme->getStyleSheetsRemove(),
68       'libraries' => $activeTheme->getLibraries(),
69       'regions' => $activeTheme->getRegions(),
70     ];
71
72     if ($this->themeNegotiator instanceof ThemeNegotiatorWrapper) {
73       $this->data['negotiator'] = [
74         'class' => $this->getMethodData($this->themeNegotiator->getNegotiator(), 'determineActiveTheme'),
75         'id' => $this->themeNegotiator->getNegotiator()->_serviceId,
76       ];
77     }
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function lateCollect() {
84     $this->data['twig'] = serialize($this->profile);
85   }
86
87   /**
88    * @return string
89    */
90   public function getActiveTheme() {
91     return $this->data['activeTheme'];
92   }
93
94   /**
95    * @return array
96    */
97   public function getThemeNegotiator() {
98     return $this->data['negotiator'];
99   }
100
101   /**
102    * @return int
103    */
104   public function getTime() {
105     return $this->getProfile()->getDuration() * 1000;
106   }
107
108   /**
109    * @return mixed
110    */
111   public function getTemplateCount() {
112     return $this->getComputedData('template_count');
113   }
114
115   /**
116    * @return mixed
117    */
118   public function getTemplates() {
119     return $this->getComputedData('templates');
120   }
121
122   /**
123    * @return mixed
124    */
125   public function getBlockCount() {
126     return $this->getComputedData('block_count');
127   }
128
129   /**
130    * @return mixed
131    */
132   public function getMacroCount() {
133     return $this->getComputedData('macro_count');
134   }
135
136   /**
137    * @return \Twig_Markup
138    */
139   public function getHtmlCallGraph() {
140     $dumper = new HtmlDumper();
141
142     return new \Twig_Markup($dumper->dump($this->getProfile()), 'UTF-8');
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function getName() {
149     return 'theme';
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function getTitle() {
156     return $this->t('Theme');
157   }
158
159   /**
160    * {@inheritdoc}
161    */
162   public function getPanelSummary() {
163     return $this->t('Name: @name', ['@name' => $this->getActiveTheme()['name']]);
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function getIcon() {
170     return 'iVBORw0KGgoAAAANSUhEUgAAABUAAAAcCAYAAACOGPReAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAlVJREFUeNrMlk1IVFEUx53K6AOjUBQri6SihQiRziaRQegDlKJFBCpSiZtoE4hWtFFRwcCVBqEWfaAE7SoXajXLQoRcFAUSaBgUJQxKYUrT78D/wevx3psaE7rw49x377ln7vl4500kmUxm/OuxJmMVxrqwzVgsth5xHKogCnthCd7BU7gdj8envOciQe5j8CyiFQq0tAgDsA+OaG0eLmF4INQoxrYg+iEXhqEGiqGBw/3SGUeU6MgC61mBMUU5GzEKJ6ES5S5knbbXui/jmv8MjCkGMxEPFbsFyAeL1y6pXEenSO4fctlIhCXqqtmGj5ADcblZoX1z8aLn/Es475soDhcyfwt3LPDK8jPYFlIcZrCCEH0LqlP7NXN/CCUL/CvmkynKcczPoNv9Y5LXuHVC2Y6mMFoetOG4/0kl9LfDquMyN076ue/I11ANHSqVH0pgLUz7GK2HjUHuf1DGm/jVYZVYJeILz5163oy46Tn/GL5r396yc+hXOzcckiyVQp7CsVNvmI2DHoODcMFcR6eZ+RO4747pJuYjcFilssMM6vB7+Or8IGMWWjDWx7ntzHvglCWZtfbf3n0UtiJ6FVO/8QbuQh+H59C37nUD9sAj1k6ENZQylctuJWsGJswDDibY32C3gitKsCUwyt7nlK0vpMc2Wh/Q4zIcxeDztDu/QjSoPmujzWswZef3GLRXuV2xPa3u/yDwjfoDgxE1nP1aquOG91b64StWlp0xtaKvKbfMUS1maukWvEj7a6pxBg6oL1iddnsbSFox/S/+TKyK0V8CDABrCdI/1oTqiQAAAABJRU5ErkJggg==';
171   }
172
173   /**
174    * @return array
175    */
176   public function getData() {
177     $data = $this->data;
178
179     $data['twig'] = [
180       'callgraph' => (string) $this->getHtmlCallGraph(),
181       'render_time' => $this->getTime(),
182       'template_count' => $this->getTemplateCount(),
183       'templates' => $this->getTemplates(),
184       'block_count' => $this->getBlockCount(),
185       'macro_count' => $this->getMacroCount(),
186     ];
187
188     return $data;
189   }
190
191   /**
192    * @return mixed|\Twig_Profiler_Profile
193    */
194   private function getProfile() {
195     if (NULL === $this->profile) {
196       $this->profile = unserialize($this->data['twig']);
197     }
198
199     return $this->profile;
200   }
201
202   /**
203    * @param $index
204    *
205    * @return mixed
206    */
207   private function getComputedData($index) {
208     if (NULL === $this->computed) {
209       $this->computed = $this->computeData($this->getProfile());
210     }
211
212     return $this->computed[$index];
213   }
214
215   /**
216    * @param \Twig_Profiler_Profile $profile
217    *
218    * @return array
219    */
220   private function computeData(\Twig_Profiler_Profile $profile) {
221     $data = [
222       'template_count' => 0,
223       'block_count' => 0,
224       'macro_count' => 0,
225     ];
226
227     $templates = [];
228     foreach ($profile as $p) {
229       $d = $this->computeData($p);
230
231       $data['template_count'] += ($p->isTemplate() ? 1 : 0) + $d['template_count'];
232       $data['block_count'] += ($p->isBlock() ? 1 : 0) + $d['block_count'];
233       $data['macro_count'] += ($p->isMacro() ? 1 : 0) + $d['macro_count'];
234
235       if ($p->isTemplate()) {
236         if (!isset($templates[$p->getTemplate()])) {
237           $templates[$p->getTemplate()] = 1;
238         }
239         else {
240           $templates[$p->getTemplate()]++;
241         }
242       }
243
244       foreach ($d['templates'] as $template => $count) {
245         if (!isset($templates[$template])) {
246           $templates[$template] = $count;
247         }
248         else {
249           $templates[$template] += $count;
250         }
251       }
252     }
253     $data['templates'] = $templates;
254
255     return $data;
256   }
257 }