DevelDumperManager.php 3.9 KB

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