DevelDumperManager.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Drupal\devel;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. use Drupal\Core\Session\AccountProxyInterface;
  5. use Drupal\Core\StringTranslation\StringTranslationTrait;
  6. /**
  7. * Class DevelDumperManager.
  8. */
  9. class DevelDumperManager implements DevelDumperManagerInterface {
  10. use StringTranslationTrait;
  11. /**
  12. * The devel config.
  13. *
  14. * @var \Drupal\Core\Config\ImmutableConfig
  15. */
  16. protected $config;
  17. /**
  18. * The current account.
  19. *
  20. * @var \Drupal\Core\Session\AccountProxyInterface
  21. */
  22. protected $account;
  23. /**
  24. * The devel dumper plugin manager.
  25. *
  26. * @var \Drupal\devel\DevelDumperPluginManagerInterface
  27. */
  28. protected $dumperManager;
  29. /**
  30. * Constructs a DevelDumperPluginManager object.
  31. *
  32. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  33. * The config factory service.
  34. * @param \Drupal\Core\Session\AccountProxyInterface $account
  35. * The current account.
  36. * @param \Drupal\devel\DevelDumperPluginManagerInterface $dumper_manager
  37. * The devel dumper plugin manager.
  38. */
  39. public function __construct(ConfigFactoryInterface $config_factory, AccountProxyInterface $account, DevelDumperPluginManagerInterface $dumper_manager) {
  40. $this->config = $config_factory->get('devel.settings');
  41. $this->account = $account;
  42. $this->dumperManager = $dumper_manager;
  43. }
  44. /**
  45. * Instances a new dumper plugin.
  46. *
  47. * @param string $plugin_id
  48. * (optional) The plugin ID, defaults to NULL.
  49. *
  50. * @return \Drupal\devel\DevelDumperInterface
  51. * Returns the devel dumper plugin instance.
  52. */
  53. protected function createInstance($plugin_id = NULL) {
  54. if (!$plugin_id || !$this->dumperManager->isPluginSupported($plugin_id)) {
  55. $plugin_id = $this->config->get('devel_dumper');
  56. }
  57. return $this->dumperManager->createInstance($plugin_id);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function dump($input, $name = NULL, $plugin_id = NULL) {
  63. if ($this->hasAccessToDevelInformation()) {
  64. $this->createInstance($plugin_id)->dump($input, $name);
  65. }
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function export($input, $name = NULL, $plugin_id = NULL) {
  71. if ($this->hasAccessToDevelInformation()) {
  72. return $this->createInstance($plugin_id)->export($input, $name);
  73. }
  74. return NULL;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function message($input, $name = NULL, $type = 'status', $plugin_id = NULL) {
  80. if ($this->hasAccessToDevelInformation()) {
  81. $output = $this->export($input, $name, $plugin_id);
  82. drupal_set_message($output, $type, TRUE);
  83. }
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function debug($input, $name = NULL, $plugin_id = NULL) {
  89. $output = $this->createInstance($plugin_id)->export($input, $name) . "\n";
  90. // The temp directory does vary across multiple simpletest instances.
  91. $file = file_directory_temp() . '/drupal_debug.txt';
  92. if (file_put_contents($file, $output, FILE_APPEND) === FALSE && $this->hasAccessToDevelInformation()) {
  93. drupal_set_message($this->t('Devel was unable to write to %file.', ['%file' => $file]), 'error');
  94. return FALSE;
  95. }
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function dumpOrExport($input, $name = NULL, $export = TRUE, $plugin_id = NULL) {
  101. if ($this->hasAccessToDevelInformation()) {
  102. $dumper = $this->createInstance($plugin_id);
  103. if ($export) {
  104. return $dumper->export($input, $name);
  105. }
  106. $dumper->dump($input, $name);
  107. }
  108. return NULL;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function exportAsRenderable($input, $name = NULL, $plugin_id = NULL) {
  114. if ($this->hasAccessToDevelInformation()) {
  115. return $this->createInstance($plugin_id)->exportAsRenderable($input, $name);
  116. }
  117. return [];
  118. }
  119. /**
  120. * Checks whether a user has access to devel information.
  121. *
  122. * @return bool
  123. * TRUE if the user has the permission, FALSE otherwise.
  124. */
  125. protected function hasAccessToDevelInformation() {
  126. return $this->account && $this->account->hasPermission('access devel information');
  127. }
  128. }