Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Controller / DashboardController.php
1 <?php
2
3 namespace Drupal\webprofiler\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Datetime\DateFormatter;
7 use Drupal\Core\Url;
8 use Drupal\webprofiler\DrupalDataCollectorInterface;
9 use Drupal\webprofiler\Profiler\ProfilerStorageManager;
10 use Drupal\webprofiler\Profiler\TemplateManager;
11 use Symfony\Component\HttpFoundation\JsonResponse;
12 use Drupal\webprofiler\Profiler\Profiler;
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Symfony\Component\HttpKernel\Profiler\Profile;
16 use Symfony\Component\Routing\RouterInterface;
17
18 /**
19  * Class DashboardController
20  */
21 class DashboardController extends ControllerBase {
22
23   /**
24    * @var \Drupal\webprofiler\Profiler\Profiler
25    */
26   private $profiler;
27
28   /**
29    * @var \Symfony\Cmf\Component\Routing\ChainRouter
30    */
31   private $router;
32
33   /**
34    * @var \Drupal\webprofiler\Profiler\TemplateManager
35    */
36   private $templateManager;
37
38   /**
39    * @var \Drupal\Core\Datetime\DateFormatter
40    */
41   private $date;
42
43   /**
44    * @var \Drupal\webprofiler\Profiler\ProfilerStorageManager
45    */
46   private $storageManager;
47
48   /**
49    * {@inheritdoc}
50    */
51   public static function create(ContainerInterface $container) {
52     return new static(
53       $container->get('profiler'),
54       $container->get('router'),
55       $container->get('template_manager'),
56       $container->get('date.formatter'),
57       $container->get('profiler.storage_manager')
58     );
59   }
60
61   /**
62    * Constructs a new WebprofilerController.
63    *
64    * @param \Drupal\webprofiler\Profiler\Profiler $profiler
65    * @param \Symfony\Component\Routing\RouterInterface $router
66    * @param \Drupal\webprofiler\Profiler\TemplateManager $templateManager
67    * @param \Drupal\Core\Datetime\DateFormatter $date
68    * @param \Drupal\webprofiler\Profiler\ProfilerStorageManager $storageManager
69    */
70   public function __construct(Profiler $profiler, RouterInterface $router, TemplateManager $templateManager, DateFormatter $date, ProfilerStorageManager $storageManager) {
71     $this->profiler = $profiler;
72     $this->router = $router;
73     $this->templateManager = $templateManager;
74     $this->date = $date;
75     $this->storageManager = $storageManager;
76   }
77
78   /**
79    * Generates the dashboard page.
80    *
81    * @param Profile $profile
82    *
83    * @return array
84    */
85   public function dashboardAction(Profile $profile) {
86     $this->profiler->disable();
87
88     $templateManager = $this->templateManager;
89     $templates = $templateManager->getTemplates($profile);
90
91     $panels = [];
92     $libraries = ['webprofiler/dashboard'];
93     $drupalSettings = [
94       'webprofiler' => [
95         'token' => $profile->getToken(),
96         'ide_link' => $this->config('webprofiler.config')->get('ide_link'),
97         'ide_link_remote' => $this->config('webprofiler.config')->get('ide_link_remote'),
98         'ide_link_local' => $this->config('webprofiler.config')->get('ide_link_local'),
99         'collectors' => [],
100       ],
101     ];
102
103     foreach ($templates as $name => $template) {
104       /** @var DrupalDataCollectorInterface $collector */
105       $collector = $profile->getCollector($name);
106
107       if ($collector->hasPanel()) {
108         $rendered = $template->renderBlock('panel', [
109           'token' => $profile->getToken(),
110           'name' => $name,
111         ]);
112
113         $panels[] = [
114           '#theme' => 'webprofiler_panel',
115           '#panel' => $rendered,
116         ];
117
118         $drupalSettings['webprofiler']['collectors'][] = [
119           'id' => $name,
120           'name' => $name,
121           'label' => $collector->getTitle(),
122           'summary' => $collector->getPanelSummary(),
123           'icon' => $collector->getIcon(),
124         ];
125
126         $libraries = array_merge($libraries, $collector->getLibraries());
127         $drupalSettings['webprofiler'] += $collector->getDrupalSettings();
128       }
129     }
130
131     $build = [];
132     $build['panels'] = [
133       '#theme' => 'webprofiler_dashboard',
134       '#profile' => $profile,
135       '#panels' => $panels,
136       '#spinner_path' => '/' . $this->moduleHandler()
137           ->getModule('webprofiler')
138           ->getPath() . '/images/searching.gif',
139       '#attached' => [
140         'drupalSettings' => $drupalSettings,
141         'library' => $libraries,
142       ],
143     ];
144
145     return $build;
146   }
147
148   /**
149    * Generates the list page.
150    *
151    * @param \Symfony\Component\HttpFoundation\Request $request
152    *
153    * @return array
154    */
155   public function listAction(Request $request) {
156     $limit = $request->get('limit', 10);
157     $this->profiler->disable();
158
159     $ip = $request->query->get('ip');
160     $method = $request->query->get('method');
161     $url = $request->query->get('url');
162
163     $profiles = $this->profiler->find($ip, $url, $limit, $method, '', '');
164
165     $rows = [];
166     if (count($profiles)) {
167       foreach ($profiles as $profile) {
168         $row = [];
169         $row[] = $this->l($profile['token'], new Url('webprofiler.dashboard', ['profile' => $profile['token']]));
170         $row[] = $profile['ip'];
171         $row[] = $profile['method'];
172         $row[] = $profile['url'];
173         $row[] = $this->date->format($profile['time']);
174
175         $rows[] = $row;
176       }
177     }
178     else {
179       $rows[] = [
180         [
181           'data' => $this->t('No profiles found'),
182           'colspan' => 6,
183         ],
184       ];
185     }
186
187     $build = [];
188
189     $storage_id = $this->config('webprofiler.config')->get('storage');
190     $storage = $this->storageManager->getStorage($storage_id);
191
192     $build['resume'] = [
193       '#type' => 'inline_template',
194       '#template' => '<p>{{ message }}</p>',
195       '#context' => [
196         'message' => $this->t('Profiles stored with %storage service.', ['%storage' => $storage['title']]),
197       ],
198     ];
199
200     $build['filters'] = $this->formBuilder()
201       ->getForm('Drupal\\webprofiler\\Form\\ProfilesFilterForm');
202
203     $build['table'] = [
204       '#type' => 'table',
205       '#rows' => $rows,
206       '#header' => [
207         $this->t('Token'),
208         [
209           'data' => $this->t('Ip'),
210           'class' => [RESPONSIVE_PRIORITY_LOW],
211         ],
212         [
213           'data' => $this->t('Method'),
214           'class' => [RESPONSIVE_PRIORITY_LOW],
215         ],
216         $this->t('Url'),
217         [
218           'data' => $this->t('Time'),
219           'class' => [RESPONSIVE_PRIORITY_MEDIUM],
220         ],
221       ],
222       '#sticky' => TRUE,
223     ];
224
225     return $build;
226   }
227
228   /**
229    * Exposes collector's data as JSON.
230    *
231    * @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
232    * @param $collector
233    *
234    * @return \Symfony\Component\HttpFoundation\JsonResponse
235    */
236   public function restCollectorAction(Profile $profile, $collector) {
237     $this->profiler->disable();
238
239     $data = $profile->getCollector($collector)->getData();
240
241     return new JsonResponse(['data' => $data]);
242   }
243 }