3 namespace Drupal\webprofiler\Controller;
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Datetime\DateFormatter;
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;
19 * Class DashboardController
21 class DashboardController extends ControllerBase {
24 * @var \Drupal\webprofiler\Profiler\Profiler
29 * @var \Symfony\Cmf\Component\Routing\ChainRouter
34 * @var \Drupal\webprofiler\Profiler\TemplateManager
36 private $templateManager;
39 * @var \Drupal\Core\Datetime\DateFormatter
44 * @var \Drupal\webprofiler\Profiler\ProfilerStorageManager
46 private $storageManager;
51 public static function create(ContainerInterface $container) {
53 $container->get('profiler'),
54 $container->get('router'),
55 $container->get('template_manager'),
56 $container->get('date.formatter'),
57 $container->get('profiler.storage_manager')
62 * Constructs a new WebprofilerController.
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
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;
75 $this->storageManager = $storageManager;
79 * Generates the dashboard page.
81 * @param Profile $profile
85 public function dashboardAction(Profile $profile) {
86 $this->profiler->disable();
88 $templateManager = $this->templateManager;
89 $templates = $templateManager->getTemplates($profile);
92 $libraries = ['webprofiler/dashboard'];
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'),
103 foreach ($templates as $name => $template) {
104 /** @var DrupalDataCollectorInterface $collector */
105 $collector = $profile->getCollector($name);
107 if ($collector->hasPanel()) {
108 $rendered = $template->renderBlock('panel', [
109 'token' => $profile->getToken(),
114 '#theme' => 'webprofiler_panel',
115 '#panel' => $rendered,
118 $drupalSettings['webprofiler']['collectors'][] = [
121 'label' => $collector->getTitle(),
122 'summary' => $collector->getPanelSummary(),
123 'icon' => $collector->getIcon(),
126 $libraries = array_merge($libraries, $collector->getLibraries());
127 $drupalSettings['webprofiler'] += $collector->getDrupalSettings();
133 '#theme' => 'webprofiler_dashboard',
134 '#profile' => $profile,
135 '#panels' => $panels,
136 '#spinner_path' => '/' . $this->moduleHandler()
137 ->getModule('webprofiler')
138 ->getPath() . '/images/searching.gif',
140 'drupalSettings' => $drupalSettings,
141 'library' => $libraries,
149 * Generates the list page.
151 * @param \Symfony\Component\HttpFoundation\Request $request
155 public function listAction(Request $request) {
156 $limit = $request->get('limit', 10);
157 $this->profiler->disable();
159 $ip = $request->query->get('ip');
160 $method = $request->query->get('method');
161 $url = $request->query->get('url');
163 $profiles = $this->profiler->find($ip, $url, $limit, $method, '', '');
166 if (count($profiles)) {
167 foreach ($profiles as $profile) {
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']);
181 'data' => $this->t('No profiles found'),
189 $storage_id = $this->config('webprofiler.config')->get('storage');
190 $storage = $this->storageManager->getStorage($storage_id);
193 '#type' => 'inline_template',
194 '#template' => '<p>{{ message }}</p>',
196 'message' => $this->t('Profiles stored with %storage service.', ['%storage' => $storage['title']]),
200 $build['filters'] = $this->formBuilder()
201 ->getForm('Drupal\\webprofiler\\Form\\ProfilesFilterForm');
209 'data' => $this->t('Ip'),
210 'class' => [RESPONSIVE_PRIORITY_LOW],
213 'data' => $this->t('Method'),
214 'class' => [RESPONSIVE_PRIORITY_LOW],
218 'data' => $this->t('Time'),
219 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
229 * Exposes collector's data as JSON.
231 * @param \Symfony\Component\HttpKernel\Profiler\Profile $profile
234 * @return \Symfony\Component\HttpFoundation\JsonResponse
236 public function restCollectorAction(Profile $profile, $collector) {
237 $this->profiler->disable();
239 $data = $profile->getCollector($collector)->getData();
241 return new JsonResponse(['data' => $data]);