Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / devel / src / DevelDumperManager.php
1 <?php
2
3 namespace Drupal\devel;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Session\AccountProxyInterface;
7
8 /**
9  * Class DevelDumperManager
10  */
11 class DevelDumperManager implements DevelDumperManagerInterface {
12
13   /**
14    * The devel config.
15    *
16    * @var \Drupal\Core\Config\ImmutableConfig
17    */
18   protected $config;
19
20   /**
21    * The current account.
22    *
23    * @var \Drupal\Core\Session\AccountProxyInterface
24    */
25   protected $account;
26
27   /**
28    * The devel dumper plugin manager.
29    *
30    * @var \Drupal\devel\DevelDumperPluginManagerInterface
31    */
32   protected $dumperManager;
33
34   /**
35    * Constructs a DevelDumperPluginManager object.
36    *
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.
43    */
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;
48   }
49
50   /**
51    * Instances a new dumper plugin.
52    *
53    * @param string $plugin_id
54    *   (optional) The plugin ID, defaults to NULL.
55    *
56    * @return \Drupal\devel\DevelDumperInterface
57    *   Returns the devel dumper plugin instance.
58    */
59   protected function createInstance($plugin_id = NULL) {
60     if (!$plugin_id || !$this->dumperManager->isPluginSupported($plugin_id)) {
61       $plugin_id = $this->config->get('devel_dumper');
62     }
63     return $this->dumperManager->createInstance($plugin_id);
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function dump($input, $name = NULL, $plugin_id = NULL) {
70     if ($this->hasAccessToDevelInformation()) {
71       $this->createInstance($plugin_id)->dump($input, $name);
72     }
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function export($input, $name = NULL, $plugin_id = NULL) {
79     if ($this->hasAccessToDevelInformation()) {
80       return $this->createInstance($plugin_id)->export($input, $name);
81     }
82     return NULL;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
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);
92     }
93   }
94
95   /**
96    * {@inheritdoc}
97    */
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');
104       return FALSE;
105     }
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function dumpOrExport($input, $name = NULL, $export = TRUE, $plugin_id = NULL) {
112     if ($this->hasAccessToDevelInformation()) {
113       $dumper = $this->createInstance($plugin_id);
114       if ($export) {
115         return $dumper->export($input, $name);
116       }
117       $dumper->dump($input, $name);
118     }
119     return NULL;
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function exportAsRenderable($input, $name = NULL, $plugin_id = NULL) {
126     if ($this->hasAccessToDevelInformation()) {
127       return $this->createInstance($plugin_id)->exportAsRenderable($input, $name);
128     }
129     return [];
130   }
131
132   /**
133    * Checks whether a user has access to devel information.
134    *
135    * @return bool
136    *   TRUE if the user has the permission, FALSE otherwise.
137    */
138   protected function hasAccessToDevelInformation() {
139     return $this->account && $this->account->hasPermission('access devel information');
140   }
141
142 }