PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/devel/src/Controller/ContainerInfoController.php

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