eba5e2c881fa493e4b9ba4a4f877d93ffd090eeb
[yaffs-website] / web / modules / contrib / devel / src / Controller / ContainerInfoController.php
1 <?php
2
3 namespace Drupal\devel\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\DrupalKernelInterface;
7 use Drupal\Core\Url;
8 use Drupal\devel\DevelDumperManagerInterface;
9 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
13 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
15 /**
16  * Provides route responses for the container info pages.
17  */
18 class ContainerInfoController extends ControllerBase implements ContainerAwareInterface {
19
20   use ContainerAwareTrait;
21
22   /**
23    * The drupal kernel.
24    *
25    * @var \Drupal\Core\DrupalKernelInterface
26    */
27   protected $kernel;
28
29   /**
30    * The dumper manager service.
31    *
32    * @var \Drupal\devel\DevelDumperManagerInterface
33    */
34   protected $dumper;
35
36   /**
37    * ServiceInfoController constructor.
38    *
39    * @param \Drupal\Core\DrupalKernelInterface $drupalKernel
40    *   The drupal kernel.
41    * @param \Drupal\devel\DevelDumperManagerInterface $dumper
42    *   The dumper manager service.
43    */
44   public function __construct(DrupalKernelInterface $drupalKernel, DevelDumperManagerInterface $dumper) {
45     $this->kernel = $drupalKernel;
46     $this->dumper = $dumper;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public static function create(ContainerInterface $container) {
53     return new static(
54       $container->get('kernel'),
55       $container->get('devel.dumper')
56     );
57   }
58
59   /**
60    * Builds the services overview page.
61    *
62    * @return array
63    *   A render array as expected by the renderer.
64    */
65   public function serviceList() {
66     $headers = [
67       $this->t('ID'),
68       $this->t('Class'),
69       $this->t('Alias'),
70       $this->t('Operations'),
71     ];
72
73     $rows = [];
74
75     if ($container = $this->kernel->getCachedContainerDefinition()) {
76       foreach ($container['services'] as $service_id => $definition) {
77         $service = unserialize($definition);
78
79         $row['id'] = [
80           'data' => $service_id,
81           'class' => 'table-filter-text-source',
82         ];
83         $row['class'] = [
84           'data' => isset($service['class']) ? $service['class'] : '',
85           'class' => 'table-filter-text-source',
86         ];
87         $row['alias'] = [
88           'data' => array_search($service_id, $container['aliases']) ?: '',
89           'class' => 'table-filter-text-source',
90         ];
91         $row['operations']['data'] = [
92           '#type' => 'operations',
93           '#links' => [
94             'devel' => [
95               'title' => $this->t('Devel'),
96               'url' => Url::fromRoute('devel.container_info.service.detail', ['service_id' => $service_id]),
97             ],
98           ],
99         ];
100
101         $rows[$service_id] = $row;
102       }
103
104       ksort($rows);
105     }
106
107     $output['#attached']['library'][] = 'system/drupal.system.modules';
108
109     $output['filters'] = [
110       '#type' => 'container',
111       '#attributes' => [
112         'class' => ['table-filter', 'js-show'],
113       ],
114     ];
115     $output['filters']['text'] = [
116       '#type' => 'search',
117       '#title' => $this->t('Search'),
118       '#size' => 30,
119       '#placeholder' => $this->t('Enter service id, alias or class'),
120       '#attributes' => [
121         'class' => ['table-filter-text'],
122         'data-table' => '.devel-filter-text',
123         'autocomplete' => 'off',
124         'title' => $this->t('Enter a part of the service id, service alias or class to filter by.'),
125       ],
126     ];
127     $output['services'] = [
128       '#type' => 'table',
129       '#header' => $headers,
130       '#rows' => $rows,
131       '#empty' => $this->t('No services found.'),
132       '#sticky' => TRUE,
133       '#attributes' => [
134         'class' => ['devel-service-list', 'devel-filter-text'],
135       ],
136     ];
137
138     return $output;
139   }
140
141   /**
142    * Returns a render array representation of the service.
143    *
144    * @param string $service_id
145    *   The ID of the service to retrieve.
146    *
147    * @return array
148    *   A render array containing the service detail.
149    *
150    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
151    *   If the requested service is not defined.
152    */
153   public function serviceDetail($service_id) {
154     $instance = $this->container->get($service_id, ContainerInterface::NULL_ON_INVALID_REFERENCE);
155     if ($instance === NULL) {
156       throw new NotFoundHttpException();
157     }
158
159     $output = [];
160
161     if ($cached_definitions = $this->kernel->getCachedContainerDefinition()) {
162       // Tries to retrieve the service definition from the kernel's cached
163       // container definition.
164       if (isset($cached_definitions['services'][$service_id])) {
165         $definition = unserialize($cached_definitions['services'][$service_id]);
166
167         // If the service has an alias add it to the definition.
168         if ($alias = array_search($service_id, $cached_definitions['aliases'])) {
169           $definition['alias'] = $alias;
170         }
171
172         $output['definition'] = $this->dumper->exportAsRenderable($definition, $this->t('Computed Definition'));
173       }
174     }
175
176     $output['instance'] = $this->dumper->exportAsRenderable($instance, $this->t('Instance'));
177
178     return $output;
179   }
180
181   /**
182    * Builds the parameters overview page.
183    *
184    * @return array
185    *   A render array as expected by the renderer.
186    */
187   public function parameterList() {
188     $headers = [
189       $this->t('Name'),
190       $this->t('Operations'),
191     ];
192
193     $rows = [];
194
195     if ($container = $this->kernel->getCachedContainerDefinition()) {
196       foreach ($container['parameters'] as $parameter_name => $definition) {
197         $row['name'] = [
198           'data' => $parameter_name,
199           'class' => 'table-filter-text-source',
200         ];
201         $row['operations']['data'] = [
202           '#type' => 'operations',
203           '#links' => [
204             'devel' => [
205               'title' => $this->t('Devel'),
206               'url' => Url::fromRoute('devel.container_info.parameter.detail', ['parameter_name' => $parameter_name]),
207             ],
208           ],
209         ];
210
211         $rows[$parameter_name] = $row;
212       }
213
214       ksort($rows);
215     }
216
217     $output['#attached']['library'][] = 'system/drupal.system.modules';
218
219     $output['filters'] = [
220       '#type' => 'container',
221       '#attributes' => [
222         'class' => ['table-filter', 'js-show'],
223       ],
224     ];
225     $output['filters']['text'] = [
226       '#type' => 'search',
227       '#title' => $this->t('Search'),
228       '#size' => 30,
229       '#placeholder' => $this->t('Enter parameter name'),
230       '#attributes' => [
231         'class' => ['table-filter-text'],
232         'data-table' => '.devel-filter-text',
233         'autocomplete' => 'off',
234         'title' => $this->t('Enter a part of the parameter name to filter by.'),
235       ],
236     ];
237     $output['parameters'] = [
238       '#type' => 'table',
239       '#header' => $headers,
240       '#rows' => $rows,
241       '#empty' => $this->t('No parameters found.'),
242       '#sticky' => TRUE,
243       '#attributes' => [
244         'class' => ['devel-parameter-list', 'devel-filter-text'],
245       ],
246     ];
247
248     return $output;
249   }
250
251   /**
252    * Returns a render array representation of the parameter value.
253    *
254    * @param string $parameter_name
255    *   The name of the parameter to retrieve.
256    *
257    * @return array
258    *   A render array containing the parameter value.
259    *
260    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
261    *   If the requested parameter is not defined.
262    */
263   public function parameterDetail($parameter_name) {
264     try {
265       $parameter = $this->container->getParameter($parameter_name);
266     }
267     catch (ParameterNotFoundException $e) {
268       throw new NotFoundHttpException();
269     }
270
271     return $this->dumper->exportAsRenderable($parameter);
272   }
273
274 }