3 namespace Drupal\devel;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Session\AccountProxyInterface;
9 * Class DevelDumperManager
11 class DevelDumperManager implements DevelDumperManagerInterface {
16 * @var \Drupal\Core\Config\ImmutableConfig
21 * The current account.
23 * @var \Drupal\Core\Session\AccountProxyInterface
28 * The devel dumper plugin manager.
30 * @var \Drupal\devel\DevelDumperPluginManagerInterface
32 protected $dumperManager;
35 * Constructs a DevelDumperPluginManager object.
37 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
38 * The config factory service.
39 * @param \Drupal\Core\Session\AccountProxyInterface $account
40 * The current account.
41 * @param \Drupal\devel\DevelDumperPluginManagerInterface $dumper_manager
42 * The devel dumper plugin manager.
44 public function __construct(ConfigFactoryInterface $config_factory, AccountProxyInterface $account, DevelDumperPluginManagerInterface $dumper_manager) {
45 $this->config = $config_factory->get('devel.settings');
46 $this->account = $account;
47 $this->dumperManager = $dumper_manager;
51 * Instances a new dumper plugin.
53 * @param string $plugin_id
54 * (optional) The plugin ID, defaults to NULL.
56 * @return \Drupal\devel\DevelDumperInterface
57 * Returns the devel dumper plugin instance.
59 protected function createInstance($plugin_id = NULL) {
60 if (!$plugin_id || !$this->dumperManager->isPluginSupported($plugin_id)) {
61 $plugin_id = $this->config->get('devel_dumper');
63 return $this->dumperManager->createInstance($plugin_id);
69 public function dump($input, $name = NULL, $plugin_id = NULL) {
70 if ($this->hasAccessToDevelInformation()) {
71 $this->createInstance($plugin_id)->dump($input, $name);
78 public function export($input, $name = NULL, $plugin_id = NULL) {
79 if ($this->hasAccessToDevelInformation()) {
80 return $this->createInstance($plugin_id)->export($input, $name);
88 public function message($input, $name = NULL, $type = 'status', $plugin_id = NULL) {
89 if ($this->hasAccessToDevelInformation()) {
90 $output = $this->export($input, $name, $plugin_id);
91 drupal_set_message($output, $type, TRUE);
98 public function debug($input, $name = NULL, $plugin_id = NULL) {
99 $output = $this->createInstance($plugin_id)->export($input, $name) . "\n";
100 // The temp directory does vary across multiple simpletest instances.
101 $file = file_directory_temp() . '/drupal_debug.txt';
102 if (file_put_contents($file, $output, FILE_APPEND) === FALSE && $this->hasAccessToDevelInformation()) {
103 drupal_set_message(t('Devel was unable to write to %file.', ['%file' => $file]), 'error');
111 public function dumpOrExport($input, $name = NULL, $export = TRUE, $plugin_id = NULL) {
112 if ($this->hasAccessToDevelInformation()) {
113 $dumper = $this->createInstance($plugin_id);
115 return $dumper->export($input, $name);
117 $dumper->dump($input, $name);
125 public function exportAsRenderable($input, $name = NULL, $plugin_id = NULL) {
126 if ($this->hasAccessToDevelInformation()) {
127 return $this->createInstance($plugin_id)->exportAsRenderable($input, $name);
133 * Checks whether a user has access to devel information.
136 * TRUE if the user has the permission, FALSE otherwise.
138 protected function hasAccessToDevelInformation() {
139 return $this->account && $this->account->hasPermission('access devel information');